By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,001 Members | 1,262 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,001 IT Pros & Developers. It's quick & easy.

Tk Calendar widget problem

bona25
P: 4
I am currently working on a tkinter calendar widget and gladly to find one in this site http://pythonical.sourceforge.net/dlgCalendar.py. It was truly a great help but there is something missing and i don't know how i can fix it. I am about to create said calendar on another window (except that of the main window) but upon clicking the choosen date on the widget, the result is nowhere to find...the date should be shown on Entry field (the one with "white" background on 'Date Picker' window...and i noticed that everytime i tried to create separate window for the result, the choosen date does not appear anymore...please help.

Below is my current formula:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import*
  2. import tkMessageBox
  3. import ttk
  4.  
  5. def dateDis():    
  6.     app=Tk()
  7.     app.title('Date Picker')
  8.     app.geometry('400x300+300+400')
  9.  
  10.     import sys, string, calendar
  11.     import Tkinter
  12.     import time
  13.     year = time.localtime()[0]
  14.     month = time.localtime()[1]
  15.     day =time.localtime()[2]
  16.     strdate = (str(year) +  "/" + str(month) + "/" + str(day))
  17.  
  18.     tk = Tkinter
  19.     fnta = ("Times", 10)
  20.     fnt = ("Times", 12)
  21.     fntc = ("Times", 12, 'bold')
  22.  
  23.     strtitle = "Calendar"
  24.     strdays = "Mon  Tue  Wed  Thu  Fri  Sat Sun"
  25.     dictmonths = {'1':'Jan','2':'Feb','3':'Mar','4':'Apr','5':'May','6':'Jun','7':'Jul','8':'Aug','9':'Sep','10':'Oct','11':'Nov','12':'Dec'}
  26.  
  27.     ##############################################
  28.     #  BEGIN CLASS
  29.     class tkCalendar :
  30.       def __init__ (self, master, arg_year, arg_month, arg_day, arg_parent_updatable_var):
  31.         self.update_var = arg_parent_updatable_var
  32.         top = self.top = tk.Toplevel(master)
  33.         try : self.intmonth = int(arg_month)
  34.         except: self.intmonth = int(1)
  35.         self.canvas =tk.Canvas (top, width =200, height =220,relief =tk.RIDGE, background ="white", borderwidth =1)
  36.         self.canvas.create_rectangle(0,0,303,30, fill="#a4cae8",width=0 )
  37.         self.canvas.create_text(100,17, text=strtitle,  font=fntc, fill="#2024d6")
  38.         stryear = str(arg_year)
  39.  
  40.         self.year_var=tk.StringVar()
  41.         self.year_var.set(stryear)
  42.         self.lblYear = tk.Label(top, textvariable = self.year_var, font = fnta, background="white")
  43.         self.lblYear.place(x=85, y = 30)
  44.  
  45.         self.month_var=tk.StringVar()
  46.         strnummonth = str(self.intmonth)
  47.         strmonth = dictmonths[strnummonth]
  48.         self.month_var.set(strmonth)
  49.  
  50.         self.lblYear = tk.Label(top, textvariable = self.month_var, font = fnta, background="white")
  51.         self.lblYear.place(x=85, y = 50)
  52.         #Variable muy usada
  53.         tagBaseButton = "Arrow"
  54.         self.tagBaseNumber = "DayButton"
  55.         #draw year arrows
  56.         x,y = 40, 43
  57.         tagThisButton = "leftyear"
  58.         tagFinalThisButton = tuple((tagBaseButton,tagThisButton))
  59.         self.fnCreateLeftArrow(self.canvas, x,y, tagFinalThisButton)
  60.         x,y = 150, 43
  61.         tagThisButton = "rightyear"
  62.         tagFinalThisButton = tuple((tagBaseButton,tagThisButton))
  63.         self.fnCreateRightArrow(self.canvas, x,y, tagFinalThisButton)
  64.         #draw month arrows
  65.         x,y = 40, 63
  66.         tagThisButton = "leftmonth"
  67.         tagFinalThisButton = tuple((tagBaseButton,tagThisButton))
  68.         self.fnCreateLeftArrow(self.canvas, x,y, tagFinalThisButton)
  69.         x,y = 150, 63
  70.         tagThisButton = "rightmonth"
  71.         tagFinalThisButton = tuple((tagBaseButton,tagThisButton))
  72.         self.fnCreateRightArrow(self.canvas, x,y, tagFinalThisButton)
  73.         #Print days
  74.         self.canvas.create_text(100,90, text=strdays, font=fnta)
  75.         self.canvas.pack (expand =1, fill =tk.BOTH)
  76.         self.canvas.tag_bind ("Arrow", "<ButtonRelease-1>", self.fnClick)
  77.         self.canvas.tag_bind ("Arrow", "<Enter>", self.fnOnMouseOver)
  78.         self.canvas.tag_bind ("Arrow", "<Leave>", self.fnOnMouseOut) 
  79.         self.fnFillCalendar()
  80.  
  81.       def fnCreateRightArrow(self, canv, x, y, strtagname):
  82.         canv.create_polygon(x,y, [[x+0,y-5], [x+10, y-5] , [x+10,y-10] , [x+20,y+0], [x+10,y+10] , [x+10,y+5] , [x+0,y+5]],tags = strtagname , fill="blue", width=0)
  83.  
  84.       def fnCreateLeftArrow(self, canv, x, y, strtagname):
  85.         canv.create_polygon(x,y, [[x+10,y-10], [x+10, y-5] , [x+20,y-5] , [x+20,y+5], [x+10,y+5] , [x+10,y+10] ],tags = strtagname , fill="blue", width=0)
  86.  
  87.       def fnClick(self,event):
  88.         owntags =self.canvas.gettags(tk.CURRENT)
  89.         if "rightyear" in owntags:
  90.             intyear = int(self.year_var.get())
  91.             intyear +=1
  92.             stryear = str(intyear)
  93.             self.year_var.set(stryear)
  94.         if "leftyear" in owntags:
  95.             intyear = int(self.year_var.get())
  96.             intyear -=1
  97.             stryear = str(intyear)
  98.             self.year_var.set(stryear)
  99.         if "rightmonth" in owntags:
  100.             if self.intmonth < 12 :
  101.                 self.intmonth += 1
  102.                 strnummonth = str(self.intmonth)
  103.                 strmonth = dictmonths[strnummonth]
  104.                 self.month_var.set(strmonth)
  105.             else :
  106.                 self.intmonth = 1
  107.                 strnummonth = str(self.intmonth)
  108.                 strmonth = dictmonths[strnummonth]
  109.                 self.month_var.set(strmonth)
  110.                 intyear = int(self.year_var.get())
  111.                 intyear +=1
  112.                 stryear = str(intyear)
  113.                 self.year_var.set(stryear)
  114.         if "leftmonth" in owntags:
  115.             if self.intmonth > 1 :
  116.                 self.intmonth -= 1
  117.                 strnummonth = str(self.intmonth)
  118.                 strmonth = dictmonths[strnummonth]
  119.                 self.month_var.set(strmonth)
  120.             else :
  121.                 self.intmonth = 12
  122.                 strnummonth = str(self.intmonth)
  123.                 strmonth = dictmonths[strnummonth]
  124.                 self.month_var.set(strmonth)
  125.                 intyear = int(self.year_var.get())
  126.                 intyear -=1
  127.                 stryear = str(intyear)
  128.                 self.year_var.set(stryear)
  129.         self.fnFillCalendar()      
  130.  
  131.       def fnFillCalendar(self):
  132.         init_x_pos = 20
  133.         arr_y_pos = [110,130,150,170,190,210]
  134.         intposarr = 0
  135.         self.canvas.delete("DayButton")
  136.         self.canvas.update()
  137.         intyear = int(self.year_var.get())
  138.         monthcal = calendar.monthcalendar(intyear, self.intmonth)  
  139.         for row in monthcal:
  140.             xpos = init_x_pos
  141.             ypos = arr_y_pos[intposarr]
  142.             for item in row:  
  143.                 stritem = str(item)
  144.                 if stritem == "0":
  145.                     xpos += 27
  146.                 else :
  147.                     tagNumber = tuple((self.tagBaseNumber,stritem))
  148.                     self.canvas.create_text(xpos, ypos , text=stritem, font=fnta,tags=tagNumber)  
  149.                     xpos += 27
  150.             intposarr += 1
  151.         self.canvas.tag_bind ("DayButton", "<ButtonRelease-1>", self.fnClickNumber)
  152.         self.canvas.tag_bind ("DayButton", "<Enter>", self.fnOnMouseOver)
  153.         self.canvas.tag_bind ("DayButton", "<Leave>", self.fnOnMouseOut) 
  154.  
  155.       def fnClickNumber(self,event):
  156.         owntags =self.canvas.gettags(tk.CURRENT)
  157.         for x in owntags:
  158.             if (x == "current") or (x == "DayButton"): pass
  159.             else :
  160.                 strdate = (str(self.year_var.get()) + "/" + str(self.intmonth) + "/" +  str(x))
  161.                 self.update_var.set(strdate)
  162.                 self.top.withdraw()
  163.  
  164.       def fnOnMouseOver(self,event):
  165.         self.canvas.move(tk.CURRENT, 1, 1)
  166.         self.canvas.update()
  167.  
  168.       def fnOnMouseOut(self,event):
  169.         self.canvas.move(tk.CURRENT, -1, -1)
  170.         self.canvas.update()
  171.  
  172.     def fnCalendar():
  173.         parent = ()
  174.         date_var = tk.StringVar()
  175.         date_var.set(strdate)
  176.         label = tk.Entry(app, textvariable=date_var, bg = "white", fg = 'blue', justify='center')
  177.         label.grid(row=2, column=2)
  178.         exitBtn = tk.Button(app, text = 'Exit',bg='maroon', fg='white', relief=RAISED, command = app.destroy)
  179.         exitBtn.grid(row=5, column=2)
  180.         tkCalendar(parent, year, month, day, date_var )
  181.             #label = tk.Entry(a, textvariable=date_var, bg = "white", justify='center')
  182.             #label.grid(row=2, column=2)
  183.  
  184.             #tkCalendar(parent, year, month, day, date_var )
  185.     l = Label(app, text= 'Beginning: ', bg='maroon', fg='white', relief=RIDGE, width=10)
  186.     l.grid(row=2, column=1)
  187.     e1 = Entry(app)
  188.     e1.grid(row=2, column=2)
  189.  
  190.  
  191.     l = Label(app, text= 'Ending: ', bg='maroon', fg='white', relief=RIDGE, width=10)
  192.     l.grid(row=4, column=1)
  193.     e2 = Entry(app)
  194.     e2.grid(row=4, column=2)
  195.  
  196.     l = Button(app, text= 'Pick Date', bg='pink', relief=RAISED, command=fnCalendar)
  197.     l.grid(row=2, column=3)  
  198.  
  199.     l = Button(app, text= 'Pick Date', bg='wheat', relief=RAISED, command=fnCalendar)
  200.     l.grid(row=4, column=3)
  201.  
  202. def quitter():
  203.     if tkMessageBox.askokcancel('Verify Exit', 'Are you sure you want to quit?'):
  204.         quit()
  205.  
  206. def aboutMe():
  207.     tkMessageBox.showinfo('Hello',message='We are the Medical Coding Team!')
  208.     return
  209.  
  210. app=Tk()
  211. app.title('My Gui')
  212. app.geometry('1000x700+600+600')
  213.  
  214. label1 = Label(app, text='- No day should pass without something being done :) -', bg= 'maroon',fg = 'white', relief=RAISED,height=2)
  215. buttonfont=('georgia',14,'bold')
  216. label1.config(font=buttonfont)
  217. label1.pack(side='top', fill=BOTH)
  218.  
  219. button1=Button(app, text='By   Date', bg='white', fg='red',width=20, command=dateDis)
  220. buttonfont=('georgia',12,'bold')
  221. button1.config(font=buttonfont)
  222. button1.pack(side='top',padx=35, pady=5)
  223.  
  224. button1=Button(app, text='Logout', bg='maroon', fg='white',width=20, relief=RAISED, command=quitter)
  225. buttonfont=('georgia',12,'bold')
  226. button1.config(font=buttonfont)
  227. button1.pack(side='bottom', padx=15,pady=15)
  228.  
  229. app.mainloop()
Sep 13 '11 #1

✓ answered by bvdet

For the StringVar to work, you have to tell Tkinter which top level window: date_var1 = StringVar(app)
I reorganized your code a little bit, and it seems to work:
Expand|Select|Wrap|Line Numbers
  1. import Tkinter as tk
  2. from Tkinter import*
  3. import tkMessageBox
  4. import sys, string, calendar
  5. import time
  6. #import ttk
  7.  
  8. def dateDis():    
  9.     app=Tk()
  10.     app.title('Date Picker')
  11.     app.geometry('400x300+300+400')
  12.  
  13.     fnta = ("Times", 10)
  14.     fnt = ("Times", 12)
  15.     fntc = ("Times", 12, 'bold')
  16.  
  17.     strtitle = "Calendar"
  18.     strdays = "Mon  Tue  Wed  Thu  Fri  Sat Sun"
  19.     dictmonths = {'1':'Jan','2':'Feb','3':'Mar',
  20.                   '4':'Apr','5':'May','6':'Jun',
  21.                   '7':'Jul','8':'Aug','9':'Sep',
  22.                   '10':'Oct','11':'Nov','12':'Dec'}
  23.  
  24.     year = time.localtime()[0]
  25.     month = time.localtime()[1]
  26.     day =time.localtime()[2]
  27.     strdate = (str(year) +  "/" + dictmonths[str(month)] + "/" + str(day))
  28.  
  29.     def fnCalendar(date_var):
  30.         parent = ()
  31.         tkCalendar(parent, year, month, day, date_var )
  32.  
  33.     l = Label(app, text= 'Beginning: ', bg='maroon', fg='white', relief=RIDGE, width=10)
  34.     l.grid(row=2, column=1)
  35.     date_var1 = StringVar(app)
  36.     date_var1.set(strdate)
  37.     e1 = Entry(app, textvariable=date_var1, bg="white", fg='blue', justify='center')
  38.     e1.grid(row=2, column=2)
  39.  
  40.     l = Label(app, text= 'Ending: ', bg='maroon', fg='white', relief=RIDGE, width=10)
  41.     l.grid(row=4, column=1)
  42.     date_var2 = StringVar(app)
  43.     date_var2.set(strdate)
  44.     e2 = Entry(app, textvariable=date_var2, bg="white", fg='blue', justify='center')
  45.     e2.grid(row=4, column=2)
  46.  
  47.     l = Button(app, text='Pick Date', bg='pink', relief=RAISED, command=lambda: fnCalendar(date_var1))
  48.     l.grid(row=2, column=3)  
  49.  
  50.     l = Button(app, text='Pick Date', bg='wheat', relief=RAISED, command=lambda: fnCalendar(date_var2))
  51.     l.grid(row=4, column=3)
  52.     exitBtn = Button(app, text='Exit',bg='maroon', fg='white', relief=RAISED, command=app.destroy)
  53.     exitBtn.grid(row=5, column=2)
  54.  
  55.     ##############################################
  56.     #  BEGIN CLASS
  57.     class tkCalendar :
  58.       def __init__ (self, master, arg_year, arg_month, arg_day, arg_parent_updatable_var):
  59.         self.update_var = arg_parent_updatable_var
  60.         top = self.top = tk.Toplevel(master)
  61.         try:
  62.             self.intmonth = int(arg_month)
  63.         except:
  64.             self.intmonth = int(1)
  65.         self.canvas =tk.Canvas (top, width =200, height =220,relief =tk.RIDGE, background ="white", borderwidth =1)
  66.         self.canvas.create_rectangle(0,0,303,30, fill="#a4cae8",width=0 )
  67.         self.canvas.create_text(100,17, text=strtitle,  font=fntc, fill="#2024d6")
  68.         stryear = str(arg_year)
  69.  
  70.         self.year_var=tk.StringVar()
  71.         self.year_var.set(stryear)
  72.         self.lblYear = tk.Label(top, textvariable = self.year_var, font = fnta, background="white")
  73.         self.lblYear.place(x=85, y = 30)
  74.  
  75.         self.month_var=tk.StringVar()
  76.         strnummonth = str(self.intmonth)
  77.         strmonth = dictmonths[strnummonth]
  78.         self.month_var.set(strmonth)
  79.  
  80.         self.lblYear = tk.Label(top, textvariable = self.month_var, font = fnta, background="white")
  81.         self.lblYear.place(x=85, y = 50)
  82.         #Variable muy usada
  83.         tagBaseButton = "Arrow"
  84.         self.tagBaseNumber = "DayButton"
  85.         #draw year arrows
  86.         x,y = 40, 43
  87.         tagThisButton = "leftyear"
  88.         tagFinalThisButton = tuple((tagBaseButton,tagThisButton))
  89.         self.fnCreateLeftArrow(self.canvas, x,y, tagFinalThisButton)
  90.         x,y = 150, 43
  91.         tagThisButton = "rightyear"
  92.         tagFinalThisButton = tuple((tagBaseButton,tagThisButton))
  93.         self.fnCreateRightArrow(self.canvas, x,y, tagFinalThisButton)
  94.         #draw month arrows
  95.         x,y = 40, 63
  96.         tagThisButton = "leftmonth"
  97.         tagFinalThisButton = tuple((tagBaseButton,tagThisButton))
  98.         self.fnCreateLeftArrow(self.canvas, x,y, tagFinalThisButton)
  99.         x,y = 150, 63
  100.         tagThisButton = "rightmonth"
  101.         tagFinalThisButton = tuple((tagBaseButton,tagThisButton))
  102.         self.fnCreateRightArrow(self.canvas, x,y, tagFinalThisButton)
  103.         #Print days
  104.         self.canvas.create_text(100,90, text=strdays, font=fnta)
  105.         self.canvas.pack (expand =1, fill =tk.BOTH)
  106.         self.canvas.tag_bind ("Arrow", "<ButtonRelease-1>", self.fnClick)
  107.         self.canvas.tag_bind ("Arrow", "<Enter>", self.fnOnMouseOver)
  108.         self.canvas.tag_bind ("Arrow", "<Leave>", self.fnOnMouseOut) 
  109.         self.fnFillCalendar()
  110.  
  111.       def fnCreateRightArrow(self, canv, x, y, strtagname):
  112.         canv.create_polygon(x,y, [[x+0,y-5], [x+10, y-5] , [x+10,y-10] , [x+20,y+0], [x+10,y+10] , [x+10,y+5] , [x+0,y+5]],tags = strtagname , fill="blue", width=0)
  113.  
  114.       def fnCreateLeftArrow(self, canv, x, y, strtagname):
  115.         canv.create_polygon(x,y, [[x+10,y-10], [x+10, y-5] , [x+20,y-5] , [x+20,y+5], [x+10,y+5] , [x+10,y+10] ],tags = strtagname , fill="blue", width=0)
  116.  
  117.       def fnClick(self,event):
  118.         owntags =self.canvas.gettags(tk.CURRENT)
  119.         if "rightyear" in owntags:
  120.             intyear = int(self.year_var.get())
  121.             intyear +=1
  122.             stryear = str(intyear)
  123.             self.year_var.set(stryear)
  124.         if "leftyear" in owntags:
  125.             intyear = int(self.year_var.get())
  126.             intyear -=1
  127.             stryear = str(intyear)
  128.             self.year_var.set(stryear)
  129.         if "rightmonth" in owntags:
  130.             if self.intmonth < 12 :
  131.                 self.intmonth += 1
  132.                 strnummonth = str(self.intmonth)
  133.                 strmonth = dictmonths[strnummonth]
  134.                 self.month_var.set(strmonth)
  135.             else :
  136.                 self.intmonth = 1
  137.                 strnummonth = str(self.intmonth)
  138.                 strmonth = dictmonths[strnummonth]
  139.                 self.month_var.set(strmonth)
  140.                 intyear = int(self.year_var.get())
  141.                 intyear +=1
  142.                 stryear = str(intyear)
  143.                 self.year_var.set(stryear)
  144.         if "leftmonth" in owntags:
  145.             if self.intmonth > 1 :
  146.                 self.intmonth -= 1
  147.                 strnummonth = str(self.intmonth)
  148.                 strmonth = dictmonths[strnummonth]
  149.                 self.month_var.set(strmonth)
  150.             else :
  151.                 self.intmonth = 12
  152.                 strnummonth = str(self.intmonth)
  153.                 strmonth = dictmonths[strnummonth]
  154.                 self.month_var.set(strmonth)
  155.                 intyear = int(self.year_var.get())
  156.                 intyear -=1
  157.                 stryear = str(intyear)
  158.                 self.year_var.set(stryear)
  159.         self.fnFillCalendar()      
  160.  
  161.       def fnFillCalendar(self):
  162.         init_x_pos = 20
  163.         arr_y_pos = [110,130,150,170,190,210]
  164.         intposarr = 0
  165.         self.canvas.delete("DayButton")
  166.         self.canvas.update()
  167.         intyear = int(self.year_var.get())
  168.         monthcal = calendar.monthcalendar(intyear, self.intmonth)  
  169.         for row in monthcal:
  170.             xpos = init_x_pos
  171.             ypos = arr_y_pos[intposarr]
  172.             for item in row:  
  173.                 stritem = str(item)
  174.                 if stritem == "0":
  175.                     xpos += 27
  176.                 else :
  177.                     tagNumber = tuple((self.tagBaseNumber,stritem))
  178.                     self.canvas.create_text(xpos, ypos , text=stritem, font=fnta,tags=tagNumber)  
  179.                     xpos += 27
  180.             intposarr += 1
  181.         self.canvas.tag_bind ("DayButton", "<ButtonRelease-1>", self.fnClickNumber)
  182.         self.canvas.tag_bind ("DayButton", "<Enter>", self.fnOnMouseOver)
  183.         self.canvas.tag_bind ("DayButton", "<Leave>", self.fnOnMouseOut) 
  184.  
  185.       def fnClickNumber(self,event):
  186.         owntags =self.canvas.gettags(tk.CURRENT)
  187.         for x in owntags:
  188.             if x not in ( "current", "DayButton" ):
  189.                 strdate = (str(self.year_var.get()) + "/" + str(self.month_var.get()) + "/" +  str(x))
  190.                 self.update_var.set(strdate)
  191.                 self.top.withdraw()
  192.                 #event.widget.update_idletasks()
  193.  
  194.       def fnOnMouseOver(self,event):
  195.         self.canvas.move(tk.CURRENT, 1, 1)
  196.         self.canvas.update()
  197.  
  198.       def fnOnMouseOut(self,event):
  199.         self.canvas.move(tk.CURRENT, -1, -1)
  200.         self.canvas.update()
  201.  
  202. def quitter():
  203.     if tkMessageBox.askokcancel('Verify Exit', 'Are you sure you want to quit?'):
  204.         quit()
  205.  
  206. def aboutMe():
  207.     tkMessageBox.showinfo('Hello',message='We are the Medical Coding Team!')
  208.     return
  209.  
  210. app=Tk()
  211. app.title('My Gui')
  212. app.geometry('1000x700+600+600')
  213.  
  214. label1 = Label(app, text='- No day should pass without something being done :) -', bg='maroon', fg='white', relief=RAISED,height=2)
  215. buttonfont=('georgia',14,'bold')
  216. label1.config(font=buttonfont)
  217. label1.pack(side='top', fill=BOTH)
  218.  
  219. button1=Button(app, text='By   Date', bg='white', fg='red',width=20, command=dateDis)
  220. buttonfont=('georgia',12,'bold')
  221. button1.config(font=buttonfont)
  222. button1.pack(side='top',padx=35, pady=5)
  223.  
  224. button1=Button(app, text='Logout', bg='maroon', fg='white',width=20, relief=RAISED, command=quitter)
  225. buttonfont=('georgia',12,'bold')
  226. button1.config(font=buttonfont)
  227. button1.pack(side='bottom', padx=15,pady=15)
  228.  
  229. app.mainloop()

Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
For the StringVar to work, you have to tell Tkinter which top level window: date_var1 = StringVar(app)
I reorganized your code a little bit, and it seems to work:
Expand|Select|Wrap|Line Numbers
  1. import Tkinter as tk
  2. from Tkinter import*
  3. import tkMessageBox
  4. import sys, string, calendar
  5. import time
  6. #import ttk
  7.  
  8. def dateDis():    
  9.     app=Tk()
  10.     app.title('Date Picker')
  11.     app.geometry('400x300+300+400')
  12.  
  13.     fnta = ("Times", 10)
  14.     fnt = ("Times", 12)
  15.     fntc = ("Times", 12, 'bold')
  16.  
  17.     strtitle = "Calendar"
  18.     strdays = "Mon  Tue  Wed  Thu  Fri  Sat Sun"
  19.     dictmonths = {'1':'Jan','2':'Feb','3':'Mar',
  20.                   '4':'Apr','5':'May','6':'Jun',
  21.                   '7':'Jul','8':'Aug','9':'Sep',
  22.                   '10':'Oct','11':'Nov','12':'Dec'}
  23.  
  24.     year = time.localtime()[0]
  25.     month = time.localtime()[1]
  26.     day =time.localtime()[2]
  27.     strdate = (str(year) +  "/" + dictmonths[str(month)] + "/" + str(day))
  28.  
  29.     def fnCalendar(date_var):
  30.         parent = ()
  31.         tkCalendar(parent, year, month, day, date_var )
  32.  
  33.     l = Label(app, text= 'Beginning: ', bg='maroon', fg='white', relief=RIDGE, width=10)
  34.     l.grid(row=2, column=1)
  35.     date_var1 = StringVar(app)
  36.     date_var1.set(strdate)
  37.     e1 = Entry(app, textvariable=date_var1, bg="white", fg='blue', justify='center')
  38.     e1.grid(row=2, column=2)
  39.  
  40.     l = Label(app, text= 'Ending: ', bg='maroon', fg='white', relief=RIDGE, width=10)
  41.     l.grid(row=4, column=1)
  42.     date_var2 = StringVar(app)
  43.     date_var2.set(strdate)
  44.     e2 = Entry(app, textvariable=date_var2, bg="white", fg='blue', justify='center')
  45.     e2.grid(row=4, column=2)
  46.  
  47.     l = Button(app, text='Pick Date', bg='pink', relief=RAISED, command=lambda: fnCalendar(date_var1))
  48.     l.grid(row=2, column=3)  
  49.  
  50.     l = Button(app, text='Pick Date', bg='wheat', relief=RAISED, command=lambda: fnCalendar(date_var2))
  51.     l.grid(row=4, column=3)
  52.     exitBtn = Button(app, text='Exit',bg='maroon', fg='white', relief=RAISED, command=app.destroy)
  53.     exitBtn.grid(row=5, column=2)
  54.  
  55.     ##############################################
  56.     #  BEGIN CLASS
  57.     class tkCalendar :
  58.       def __init__ (self, master, arg_year, arg_month, arg_day, arg_parent_updatable_var):
  59.         self.update_var = arg_parent_updatable_var
  60.         top = self.top = tk.Toplevel(master)
  61.         try:
  62.             self.intmonth = int(arg_month)
  63.         except:
  64.             self.intmonth = int(1)
  65.         self.canvas =tk.Canvas (top, width =200, height =220,relief =tk.RIDGE, background ="white", borderwidth =1)
  66.         self.canvas.create_rectangle(0,0,303,30, fill="#a4cae8",width=0 )
  67.         self.canvas.create_text(100,17, text=strtitle,  font=fntc, fill="#2024d6")
  68.         stryear = str(arg_year)
  69.  
  70.         self.year_var=tk.StringVar()
  71.         self.year_var.set(stryear)
  72.         self.lblYear = tk.Label(top, textvariable = self.year_var, font = fnta, background="white")
  73.         self.lblYear.place(x=85, y = 30)
  74.  
  75.         self.month_var=tk.StringVar()
  76.         strnummonth = str(self.intmonth)
  77.         strmonth = dictmonths[strnummonth]
  78.         self.month_var.set(strmonth)
  79.  
  80.         self.lblYear = tk.Label(top, textvariable = self.month_var, font = fnta, background="white")
  81.         self.lblYear.place(x=85, y = 50)
  82.         #Variable muy usada
  83.         tagBaseButton = "Arrow"
  84.         self.tagBaseNumber = "DayButton"
  85.         #draw year arrows
  86.         x,y = 40, 43
  87.         tagThisButton = "leftyear"
  88.         tagFinalThisButton = tuple((tagBaseButton,tagThisButton))
  89.         self.fnCreateLeftArrow(self.canvas, x,y, tagFinalThisButton)
  90.         x,y = 150, 43
  91.         tagThisButton = "rightyear"
  92.         tagFinalThisButton = tuple((tagBaseButton,tagThisButton))
  93.         self.fnCreateRightArrow(self.canvas, x,y, tagFinalThisButton)
  94.         #draw month arrows
  95.         x,y = 40, 63
  96.         tagThisButton = "leftmonth"
  97.         tagFinalThisButton = tuple((tagBaseButton,tagThisButton))
  98.         self.fnCreateLeftArrow(self.canvas, x,y, tagFinalThisButton)
  99.         x,y = 150, 63
  100.         tagThisButton = "rightmonth"
  101.         tagFinalThisButton = tuple((tagBaseButton,tagThisButton))
  102.         self.fnCreateRightArrow(self.canvas, x,y, tagFinalThisButton)
  103.         #Print days
  104.         self.canvas.create_text(100,90, text=strdays, font=fnta)
  105.         self.canvas.pack (expand =1, fill =tk.BOTH)
  106.         self.canvas.tag_bind ("Arrow", "<ButtonRelease-1>", self.fnClick)
  107.         self.canvas.tag_bind ("Arrow", "<Enter>", self.fnOnMouseOver)
  108.         self.canvas.tag_bind ("Arrow", "<Leave>", self.fnOnMouseOut) 
  109.         self.fnFillCalendar()
  110.  
  111.       def fnCreateRightArrow(self, canv, x, y, strtagname):
  112.         canv.create_polygon(x,y, [[x+0,y-5], [x+10, y-5] , [x+10,y-10] , [x+20,y+0], [x+10,y+10] , [x+10,y+5] , [x+0,y+5]],tags = strtagname , fill="blue", width=0)
  113.  
  114.       def fnCreateLeftArrow(self, canv, x, y, strtagname):
  115.         canv.create_polygon(x,y, [[x+10,y-10], [x+10, y-5] , [x+20,y-5] , [x+20,y+5], [x+10,y+5] , [x+10,y+10] ],tags = strtagname , fill="blue", width=0)
  116.  
  117.       def fnClick(self,event):
  118.         owntags =self.canvas.gettags(tk.CURRENT)
  119.         if "rightyear" in owntags:
  120.             intyear = int(self.year_var.get())
  121.             intyear +=1
  122.             stryear = str(intyear)
  123.             self.year_var.set(stryear)
  124.         if "leftyear" in owntags:
  125.             intyear = int(self.year_var.get())
  126.             intyear -=1
  127.             stryear = str(intyear)
  128.             self.year_var.set(stryear)
  129.         if "rightmonth" in owntags:
  130.             if self.intmonth < 12 :
  131.                 self.intmonth += 1
  132.                 strnummonth = str(self.intmonth)
  133.                 strmonth = dictmonths[strnummonth]
  134.                 self.month_var.set(strmonth)
  135.             else :
  136.                 self.intmonth = 1
  137.                 strnummonth = str(self.intmonth)
  138.                 strmonth = dictmonths[strnummonth]
  139.                 self.month_var.set(strmonth)
  140.                 intyear = int(self.year_var.get())
  141.                 intyear +=1
  142.                 stryear = str(intyear)
  143.                 self.year_var.set(stryear)
  144.         if "leftmonth" in owntags:
  145.             if self.intmonth > 1 :
  146.                 self.intmonth -= 1
  147.                 strnummonth = str(self.intmonth)
  148.                 strmonth = dictmonths[strnummonth]
  149.                 self.month_var.set(strmonth)
  150.             else :
  151.                 self.intmonth = 12
  152.                 strnummonth = str(self.intmonth)
  153.                 strmonth = dictmonths[strnummonth]
  154.                 self.month_var.set(strmonth)
  155.                 intyear = int(self.year_var.get())
  156.                 intyear -=1
  157.                 stryear = str(intyear)
  158.                 self.year_var.set(stryear)
  159.         self.fnFillCalendar()      
  160.  
  161.       def fnFillCalendar(self):
  162.         init_x_pos = 20
  163.         arr_y_pos = [110,130,150,170,190,210]
  164.         intposarr = 0
  165.         self.canvas.delete("DayButton")
  166.         self.canvas.update()
  167.         intyear = int(self.year_var.get())
  168.         monthcal = calendar.monthcalendar(intyear, self.intmonth)  
  169.         for row in monthcal:
  170.             xpos = init_x_pos
  171.             ypos = arr_y_pos[intposarr]
  172.             for item in row:  
  173.                 stritem = str(item)
  174.                 if stritem == "0":
  175.                     xpos += 27
  176.                 else :
  177.                     tagNumber = tuple((self.tagBaseNumber,stritem))
  178.                     self.canvas.create_text(xpos, ypos , text=stritem, font=fnta,tags=tagNumber)  
  179.                     xpos += 27
  180.             intposarr += 1
  181.         self.canvas.tag_bind ("DayButton", "<ButtonRelease-1>", self.fnClickNumber)
  182.         self.canvas.tag_bind ("DayButton", "<Enter>", self.fnOnMouseOver)
  183.         self.canvas.tag_bind ("DayButton", "<Leave>", self.fnOnMouseOut) 
  184.  
  185.       def fnClickNumber(self,event):
  186.         owntags =self.canvas.gettags(tk.CURRENT)
  187.         for x in owntags:
  188.             if x not in ( "current", "DayButton" ):
  189.                 strdate = (str(self.year_var.get()) + "/" + str(self.month_var.get()) + "/" +  str(x))
  190.                 self.update_var.set(strdate)
  191.                 self.top.withdraw()
  192.                 #event.widget.update_idletasks()
  193.  
  194.       def fnOnMouseOver(self,event):
  195.         self.canvas.move(tk.CURRENT, 1, 1)
  196.         self.canvas.update()
  197.  
  198.       def fnOnMouseOut(self,event):
  199.         self.canvas.move(tk.CURRENT, -1, -1)
  200.         self.canvas.update()
  201.  
  202. def quitter():
  203.     if tkMessageBox.askokcancel('Verify Exit', 'Are you sure you want to quit?'):
  204.         quit()
  205.  
  206. def aboutMe():
  207.     tkMessageBox.showinfo('Hello',message='We are the Medical Coding Team!')
  208.     return
  209.  
  210. app=Tk()
  211. app.title('My Gui')
  212. app.geometry('1000x700+600+600')
  213.  
  214. label1 = Label(app, text='- No day should pass without something being done :) -', bg='maroon', fg='white', relief=RAISED,height=2)
  215. buttonfont=('georgia',14,'bold')
  216. label1.config(font=buttonfont)
  217. label1.pack(side='top', fill=BOTH)
  218.  
  219. button1=Button(app, text='By   Date', bg='white', fg='red',width=20, command=dateDis)
  220. buttonfont=('georgia',12,'bold')
  221. button1.config(font=buttonfont)
  222. button1.pack(side='top',padx=35, pady=5)
  223.  
  224. button1=Button(app, text='Logout', bg='maroon', fg='white',width=20, relief=RAISED, command=quitter)
  225. buttonfont=('georgia',12,'bold')
  226. button1.config(font=buttonfont)
  227. button1.pack(side='bottom', padx=15,pady=15)
  228.  
  229. app.mainloop()
Sep 13 '11 #2

bona25
P: 4
Thank you so much for the help! :)
Sep 14 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.