使用Python tkinter optionmenu值动态填充列表框

时间:2019-03-22 16:43:37

标签: python tkinter listbox optionmenu

class Schedule(tk.Frame):

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller
    label = tk.Label(self, text="Schedule", font = LARGE_FONT)
    label.pack(side="top", fill="x", pady=10)

    options = ['2014','2015','2016','2017','2018','2019']

    variable = tk.StringVar(self)
    variable.set(options[-1])
    popupMenu1 = tk.OptionMenu(self, variable, *options, command=self.trnSchedule(variable.get(), controller))
    popupMenu1.pack()

    button = tk.Button(self, text="Menu",
                       command=lambda: controller.show_frame("Menu"))
    button.pack()

def trnSchedule(self, value, controller):

    listbox1 = tk.Listbox(self, width=75, height=20)
    listbox1.pack()
    listbox1.bind('<<ListboxSelect>>', self.onselect)

    fp = open("Schedule.txt", "r")

    line = fp.readline()

    while line:

        trnInfo = line.split(",")
        trnName = trnInfo[0]
        trnDate = trnInfo[1]


        listbox1.insert(tk.END, "{:}  {:}".format(trnName, trnDate))
        line = fp.readline()

当页面初始化时调用trnSchedule并初始化并填充列表框时,问题是当我使用optionmenu而不更改更改选项trnSchedule时。

0 个答案:

没有答案