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...pleas e help.
Below is my current formula:
Below is my current formula:
Code:
from Tkinter import*
import tkMessageBox
import ttk
def dateDis():
app=Tk()
app.title('Date Picker')
app.geometry('400x300+300+400')
import sys, string, calendar
import Tkinter
import time
year = time.localtime()[0]
month = time.localtime()[1]
day =time.localtime()[2]
strdate = (str(year) + "/" + str(month) + "/" + str(day))
tk = Tkinter
fnta = ("Times", 10)
fnt = ("Times", 12)
fntc = ("Times", 12, 'bold')
strtitle = "Calendar"
strdays = "Mon Tue Wed Thu Fri Sat Sun"
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'}
##############################################
# BEGIN CLASS
class tkCalendar :
def __init__ (self, master, arg_year, arg_month, arg_day, arg_parent_updatable_var):
self.update_var = arg_parent_updatable_var
top = self.top = tk.Toplevel(master)
try : self.intmonth = int(arg_month)
except: self.intmonth = int(1)
self.canvas =tk.Canvas (top, width =200, height =220,relief =tk.RIDGE, background ="white", borderwidth =1)
self.canvas.create_rectangle(0,0,303,30, fill="#a4cae8",width=0 )
self.canvas.create_text(100,17, text=strtitle, font=fntc, fill="#2024d6")
stryear = str(arg_year)
self.year_var=tk.StringVar()
self.year_var.set(stryear)
self.lblYear = tk.Label(top, textvariable = self.year_var, font = fnta, background="white")
self.lblYear.place(x=85, y = 30)
self.month_var=tk.StringVar()
strnummonth = str(self.intmonth)
strmonth = dictmonths[strnummonth]
self.month_var.set(strmonth)
self.lblYear = tk.Label(top, textvariable = self.month_var, font = fnta, background="white")
self.lblYear.place(x=85, y = 50)
#Variable muy usada
tagBaseButton = "Arrow"
self.tagBaseNumber = "DayButton"
#draw year arrows
x,y = 40, 43
tagThisButton = "leftyear"
tagFinalThisButton = tuple((tagBaseButton,tagThisButton))
self.fnCreateLeftArrow(self.canvas, x,y, tagFinalThisButton)
x,y = 150, 43
tagThisButton = "rightyear"
tagFinalThisButton = tuple((tagBaseButton,tagThisButton))
self.fnCreateRightArrow(self.canvas, x,y, tagFinalThisButton)
#draw month arrows
x,y = 40, 63
tagThisButton = "leftmonth"
tagFinalThisButton = tuple((tagBaseButton,tagThisButton))
self.fnCreateLeftArrow(self.canvas, x,y, tagFinalThisButton)
x,y = 150, 63
tagThisButton = "rightmonth"
tagFinalThisButton = tuple((tagBaseButton,tagThisButton))
self.fnCreateRightArrow(self.canvas, x,y, tagFinalThisButton)
#Print days
self.canvas.create_text(100,90, text=strdays, font=fnta)
self.canvas.pack (expand =1, fill =tk.BOTH)
self.canvas.tag_bind ("Arrow", "<ButtonRelease-1>", self.fnClick)
self.canvas.tag_bind ("Arrow", "<Enter>", self.fnOnMouseOver)
self.canvas.tag_bind ("Arrow", "<Leave>", self.fnOnMouseOut)
self.fnFillCalendar()
def fnCreateRightArrow(self, canv, x, y, strtagname):
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)
def fnCreateLeftArrow(self, canv, x, y, strtagname):
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)
def fnClick(self,event):
owntags =self.canvas.gettags(tk.CURRENT)
if "rightyear" in owntags:
intyear = int(self.year_var.get())
intyear +=1
stryear = str(intyear)
self.year_var.set(stryear)
if "leftyear" in owntags:
intyear = int(self.year_var.get())
intyear -=1
stryear = str(intyear)
self.year_var.set(stryear)
if "rightmonth" in owntags:
if self.intmonth < 12 :
self.intmonth += 1
strnummonth = str(self.intmonth)
strmonth = dictmonths[strnummonth]
self.month_var.set(strmonth)
else :
self.intmonth = 1
strnummonth = str(self.intmonth)
strmonth = dictmonths[strnummonth]
self.month_var.set(strmonth)
intyear = int(self.year_var.get())
intyear +=1
stryear = str(intyear)
self.year_var.set(stryear)
if "leftmonth" in owntags:
if self.intmonth > 1 :
self.intmonth -= 1
strnummonth = str(self.intmonth)
strmonth = dictmonths[strnummonth]
self.month_var.set(strmonth)
else :
self.intmonth = 12
strnummonth = str(self.intmonth)
strmonth = dictmonths[strnummonth]
self.month_var.set(strmonth)
intyear = int(self.year_var.get())
intyear -=1
stryear = str(intyear)
self.year_var.set(stryear)
self.fnFillCalendar()
def fnFillCalendar(self):
init_x_pos = 20
arr_y_pos = [110,130,150,170,190,210]
intposarr = 0
self.canvas.delete("DayButton")
self.canvas.update()
intyear = int(self.year_var.get())
monthcal = calendar.monthcalendar(intyear, self.intmonth)
for row in monthcal:
xpos = init_x_pos
ypos = arr_y_pos[intposarr]
for item in row:
stritem = str(item)
if stritem == "0":
xpos += 27
else :
tagNumber = tuple((self.tagBaseNumber,stritem))
self.canvas.create_text(xpos, ypos , text=stritem, font=fnta,tags=tagNumber)
xpos += 27
intposarr += 1
self.canvas.tag_bind ("DayButton", "<ButtonRelease-1>", self.fnClickNumber)
self.canvas.tag_bind ("DayButton", "<Enter>", self.fnOnMouseOver)
self.canvas.tag_bind ("DayButton", "<Leave>", self.fnOnMouseOut)
def fnClickNumber(self,event):
owntags =self.canvas.gettags(tk.CURRENT)
for x in owntags:
if (x == "current") or (x == "DayButton"): pass
else :
strdate = (str(self.year_var.get()) + "/" + str(self.intmonth) + "/" + str(x))
self.update_var.set(strdate)
self.top.withdraw()
def fnOnMouseOver(self,event):
self.canvas.move(tk.CURRENT, 1, 1)
self.canvas.update()
def fnOnMouseOut(self,event):
self.canvas.move(tk.CURRENT, -1, -1)
self.canvas.update()
def fnCalendar():
parent = ()
date_var = tk.StringVar()
date_var.set(strdate)
label = tk.Entry(app, textvariable=date_var, bg = "white", fg = 'blue', justify='center')
label.grid(row=2, column=2)
exitBtn = tk.Button(app, text = 'Exit',bg='maroon', fg='white', relief=RAISED, command = app.destroy)
exitBtn.grid(row=5, column=2)
tkCalendar(parent, year, month, day, date_var )
#label = tk.Entry(a, textvariable=date_var, bg = "white", justify='center')
#label.grid(row=2, column=2)
#tkCalendar(parent, year, month, day, date_var )
l = Label(app, text= 'Beginning: ', bg='maroon', fg='white', relief=RIDGE, width=10)
l.grid(row=2, column=1)
e1 = Entry(app)
e1.grid(row=2, column=2)
l = Label(app, text= 'Ending: ', bg='maroon', fg='white', relief=RIDGE, width=10)
l.grid(row=4, column=1)
e2 = Entry(app)
e2.grid(row=4, column=2)
l = Button(app, text= 'Pick Date', bg='pink', relief=RAISED, command=fnCalendar)
l.grid(row=2, column=3)
l = Button(app, text= 'Pick Date', bg='wheat', relief=RAISED, command=fnCalendar)
l.grid(row=4, column=3)
def quitter():
if tkMessageBox.askokcancel('Verify Exit', 'Are you sure you want to quit?'):
quit()
def aboutMe():
tkMessageBox.showinfo('Hello',message='We are the Medical Coding Team!')
return
app=Tk()
app.title('My Gui')
app.geometry('1000x700+600+600')
label1 = Label(app, text='- No day should pass without something being done :) -', bg= 'maroon',fg = 'white', relief=RAISED,height=2)
buttonfont=('georgia',14,'bold')
label1.config(font=buttonfont)
label1.pack(side='top', fill=BOTH)
button1=Button(app, text='By Date', bg='white', fg='red',width=20, command=dateDis)
buttonfont=('georgia',12,'bold')
button1.config(font=buttonfont)
button1.pack(side='top',padx=35, pady=5)
button1=Button(app, text='Logout', bg='maroon', fg='white',width=20, relief=RAISED, command=quitter)
buttonfont=('georgia',12,'bold')
button1.config(font=buttonfont)
button1.pack(side='bottom', padx=15,pady=15)
app.mainloop()
Comment