473,396 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Tk Calendar widget problem

bona25
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()

2 6949
bvdet
2,851 Expert Mod 2GB
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
4
Thank you so much for the help! :)
Sep 14 '11 #3

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

Similar topics

1
by: Camilo Olarte | last post by:
If you ever need a calendar dialog in Tkinter , You can use mine: http://pythonical.sourceforge.net/pyTkCalendar.html Camilo Olarte
0
by: Michael England | last post by:
I'm having trouble with the Calendar control in ASP.NET using VB. When I place a calendar on a page and then select a new date, it seems that I have to select the date twice in order for any...
6
by: Trond | last post by:
I'm having problem with the onselectionchanged event. I won't fire. Nothing happens when I select a day. That means, the page is posted back, but the test-string is not written. My Page_Load...
0
by: Mini Nutz | last post by:
Hi to everyone, I am currently designing a form that uses the MonthCalendar control, other panels are displayed for each day selected with the month calendar. My problem is that after 2...
8
by: buntyindia | last post by:
Hi , I am facing a problem with Calendar widget in IE a combo is overlapping calendar widget but in mozilla it is working fine... IE http://img120.imageshack.us/img120/2908/ielh0.gif...
3
by: buntyindia | last post by:
Hi, I am using the following Calendar widget script .... http://img169.imageshack.us/img169/5866/dateof5.gif http://www.totallysmartit.com/examples/calendar/simple.asp I am facing a...
0
by: silpa | last post by:
Hi, I have a problem with calendar extender. Example: <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:TextBox ID="TextBox1"...
1
by: aeroumr | last post by:
In the following code, I have created a panel with a button and a textctrl object on it. I have also created a menubar that will create a new text file (i.e. textctrl object). My problem is that...
0
by: kang jia | last post by:
hi currently, i am doing car rental final year project. Now i need to let user choose their date and specific time to book the car from our website. however, i am not sure how to make use of...
2
by: =?Utf-8?B?U3RldmUgQmxhaW4=?= | last post by:
Hi, I am using a calendar extender with a masked edit validator and masked editextender that provides a dropdown calendar and a text box for date entry. On the same page there are other other...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.