0

所以我目前遇到的问题是我想根据用户在第一个中选择的内容更新第二个选项菜单。我想我必须在这里使用一个 lambda 函数来使框架更新或其他东西,但我不确定如何做到这一点。到目前为止,这是我的代码:

from tkinter import *
import time
class CustomerEntryForm(Frame):
    def __init__(self):

        Frame.__init__(self)
        self.master.title("Customer Entry form:")
        self.pack()

        execute = True
        thirtyMonthList = [4,6,9,11]
        thirtyOneMonthList = [1,2,6,7,8,10,12]
        monthList = []
        dayList = []

        for i in range(1,13):
            monthList.append(i)

        initialMonth = IntVar(self)
        initialMonth.set(monthList[0])
        initialDay = IntVar(self)

        def resetDayOptionMenu():
            for i in range(1,len(dayList)+1):
                dayList.remove(i)

        def setDayList():
            resetDayOptionMenu()
            if initialMonth.get() == 2:
                for i in range(1, 29):
                    dayList.append(i)
                    initialDay.set(dayList[0])
            elif initialMonth.get() in thirtyMonthList:
                for i in range(1, 31):
                    dayList.append(i)
                    initialDay.set(dayList[0])
            elif initialMonth.get() in thirtyOneMonthList:
                for i in range(1, 32):
                    dayList.append(i)
                    initialDay.set(dayList[0])


        self.om2 = OptionMenu(self, initialMonth, *monthList, command = setDayList())
        self.om2.grid(row=0)
        self.om = OptionMenu(self, initialDay, *dayList)
        self.om.grid(row=1)



root = CustomerEntryForm()
root.mainloop()

我很感激任何帮助。谢谢。

4

1 回答 1

2

它会更容易删除,然后OptionMenu在月份更改后添加第二个字段。

像这样:

    ...

    thirtyMonthList = [4,6,9,11]

    initialMonth = IntVar(self)
    initialMonth.set(1)
    initialDay = IntVar(self)
    initialDay.set(1)

    def removeDayOptionMenu():
        self.om.destroy()

    def setDayList(event):
        removeDayOptionMenu()
        if initialMonth.get() == 2:
            addDayOptionMenu(range(1,29))
        elif initialMonth.get() in thirtyMonthList:
            addDayOptionMenu(range(1,31))
        else:
            addDayOptionMenu(range(1,32))

    def addDayOptionMenu(dayList):
        self.om = OptionMenu(self, initialDay, *dayList)
        self.om.grid(row=1)

    self.om2 = OptionMenu(self, initialMonth, *range(1,12), command = setDayList)
    self.om2.grid(row=0)
    self.om = OptionMenu(self, initialDay, *range(1,32))
    self.om.grid(row=1)
于 2013-03-31T23:29:56.637 回答