Python /Tix : Fill a ComboBox list when another ComboBox is set

322 Views Asked by At

I'm working on a project for school, and I just can't seem to solve my problem:

The application I wrote manages a .csv file containing informations about patients in a hospital, and their date of birth is set by three distinct ComboBoxes (Y/M/D). I want to display the right number of days for each month in the day-ComboBox (ie 31 in january, 28 or 29 in february...).

So, this is (a stripped-down version of) my code :

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from datetime import *
from Tkinter import *
import Tix

#useful variables
today=datetime.date(datetime.today())
listYears = []
for i in range(1900, today.year+1):
    listYears.append(i)
listMonths = []
for i in range(1, 13):
    listMonths.append(i)
#functions
def dispDay():
    birthDay=Tix.ComboBox(frame4, variable=dayVar, selectmode='immediate', listcmd = calculateDay(int(birthYear['value']), int(birthMonth['value'])))
    birthDay.entry.config(state='readonly')
    birthDay.place(anchor='c',x=(w/8-10), y=445)
    for item in listDays :
        birthDay.insert(END, item)

def calculateDay (year, month):
    if year%400 == 0 :
        if month%2 == 0 :
            if month == 2 :
                days=29
            else :
                days=30
        else:
            days=31
    elif (year%4 == 0 and year%100!=0):
        if month%2 == 0 :
            if month == 2 :
                days=29
            else:
                days=30
        else:
            days=31
    else:
        if month%2 == 0:
            if month == 2 :
                days=28
            else:
                days=30
        else:
            days=31

    global listDays 
    listDays= []
    for i in range(1, days-1):
        listDays.append(i)

def quitButton():
    qButton=Button(text="Quit",command=window.quit)
    qButton.place(relx=0.5, rely=0.5, anchor=CENTER)

def createPatientMenu(): 
    yearVar=IntVar()
    monthVar=IntVar()
    dayVar=IntVar()

    global frame4
    frame4=Frame(window,width=w,bg="grey14", height=h)
    frame4.pack()

    lbYear=Label(frame4, text="Year:")
    lbYear.pack()
    birthYear=Tix.ComboBox(frame4, variable=yearVar)
    birthYear.entry.config(state='readonly')
    birthYear.pack()
    for item in listYears :
        birthYear.insert(END, item)

    lbMonth=Label(frame4, text="month:")
    lbMonth.pack()
    birthMonth=Tix.ComboBox(frame4, variable=monthVar, selectmode='immediate', command=dispDay)
    birthMonth.entry.config(state='readonly')
    birthMonth.pack()
    for item in listMonths :
        birthMonth.insert(END, item)

    quitButton()

#main
window=Tix.Tk()
w, h = 800, 600
window.geometry("%dx%d+0+0" % (w, h))
window.config(bg='grey14')      
window.title("database")
createPatientMenu()
window.mainloop()    

My problem is that int(birthYear['value']) and int(birthMonth['value']) both return 0 (already tested it by adding a print at the start of calculateDays()), even when something is selected in the month and year ComboBoxes. I don't understand why.

Also, python gives an error when selecting a month :

Exception in Tkinter callback Traceback (most recent call last):
File "D:\Python27\lib\lib-tk\Tkinter.py", line 1541, in __call__
return self.func(*args)
TypeError: dispDay() takes no arguments (1 given)
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\Python27\lib\lib-tk\Tkinter.py", line 1541, in __call__ return self.func(*args)
TypeError: dispDay() takes no arguments (1 given)

Am I missing something?

Any help would be really appreciated, as I have to give back my assignement in a few weeks and still have plenty work to do. Thank you in advance!

0

There are 0 best solutions below