943,846 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1765
  • Python RSS
Feb 19th, 2007
0

monthly calendar

Expand Post »
hi , thanks for the code , I have changed it a little bit , I have also added two buttons (+,-)
I have written the code for (+) but it does not work , can you tell me what the problem is , the last lines which are commented out are supposed to show the following months but I keep getting error s .
thanks
#------------------------------------------------------------------------

from Tkinter import *
from calendar import *
y=2007
m=1
str1=month(y,m)
class MyApp:
def __init__(self,myparent):

self.mycontainer = Frame(myparent)
self.mycontainer.pack()
root.title("monthly calendar")


l2=Label(self.mycontainer,text= str1,justify="left",font=('courier',14,'bold'),bg='yellow')
l2.pack()
self.b1=Button(self.mycontainer,text='-',bg="green",padx=5,pady=5)
self.b1.pack(side=LEFT,ipadx=3,ipady=1)
self.b2=Button(self.mycontainer,text='+',bg="red",padx=5,pady=5)
self.b2.pack(side=RIGHT,ipadx=3,ipady=1)
self.b2.bind("<Button-1>",self.increase)
# def increase(self):

# if self.m==12:
# self.m=1
# self.y=self.y + 1
# else:
# self.m=self.m+1

if __name__ == '__main__':
root=Tk()
myapp=MyApp(root)
root.mainloop()
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
fonzali is offline Offline
25 posts
since Jan 2007
Feb 19th, 2007
0

Re: monthly calendar

Several things:
Try to give Tkinter and Calendar a namespace, so you know where the functions come from.
Give variables names that don't confuse, like 'l1' looks a lot like '11' in many editors, switching to 'lb1' helps readability.

Please use the [code=python] and [/code] tag pair to enclose your python code.


Here is my suggestion:
python Syntax (Toggle Plain Text)
  1. # display a monthly calendar in Tkinter
  2.  
  3. import Tkinter as tk
  4. import calendar as cd
  5.  
  6. class MyApp(object):
  7. def __init__(self, root, m, y):
  8.  
  9. self.m = m
  10. self.y = y
  11. self.frame1 = tk.Frame(root)
  12. self.frame1.pack()
  13. str1 = cd.month(self.y, self.m)
  14. font1 = ('courier',14,'bold')
  15. self.lb2 = tk.Label(self.frame1, text=str1, font=font1, bg='yellow')
  16. self.lb2.pack()
  17. self.b1 = tk.Button(self.frame1,text=' - ', bg="green")
  18. self.b1.pack(side=tk.LEFT, padx=3, pady=3)
  19. self.b2 = tk.Button(self.frame1,text=' + ',bg="red")
  20. self.b2.pack(side=tk.RIGHT, padx=3, pady=3)
  21.  
  22. self.b1.bind("<Button-1>", self.decrease)
  23. self.b2.bind("<Button-1>", self.increase)
  24.  
  25. root.mainloop()
  26.  
  27. def increase(self, e):
  28. if self.m == 12:
  29. self.m = 1
  30. self.y = self.y + 1
  31. else:
  32. self.m = self.m + 1
  33. self.lb2['text'] = cd.month(self.y, self.m)
  34.  
  35. def decrease(self, e):
  36. if self.m == 1:
  37. self.m = 12
  38. self.y = self.y - 1
  39. else:
  40. self.m = self.m - 1
  41. self.lb2['text'] = cd.month(self.y, self.m)
  42.  
  43.  
  44. if __name__ == '__main__':
  45. y = 2007
  46. m = 1
  47. root = tk.Tk()
  48. root.title("monthly calendar")
  49. myapp = MyApp(root, m, y)
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Feb 20th, 2007
0

Re: monthly calendar

hi , thank you very much , I know where I went wrong , I am going to play around with the code to see what I will get
Reputation Points: 10
Solved Threads: 0
Light Poster
fonzali is offline Offline
25 posts
since Jan 2007
Feb 20th, 2007
0

Re: monthly calendar

You also complicate your code (and life) by switching to OOP, when python gives you the freedom not to do that with simple programs like that.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Problems with after
Next Thread in Python Forum Timeline: Present time vs. lunch time





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC