首页 > 解决方案 > Tkinter 中的一个组合框、一个按钮和两个页面

问题描述

这是关于同一问题的修订帖子。使用 tkinter,我想向框架(FrameFour)添加一个按钮,当在组合框中选择某个值时打开一个页面。例如,如果选择 1 并单击按钮,则应该打开 frameOne,如果选择 2 并单击按钮,则应该打开 frameTwo。我对 FrameFour 的代码如下

class PageFour(tk.Frame):

def __init__(self, parent,controller):
    tk.Frame.__init__(self,parent)

    self.InitUI()

def nextButton(self,controller):
    if self.mychoice.get()=='1':
        controller.show_frame(PageTwo)

def InitUI(self):
    self.mychoice=StringVar()

    self.combo = ttk.Combobox(self,width =15, textvariable=self.mychoice)
    self.combo['values']=(" ","1","2")
    self.combo.grid(column=1, row=0)
    self.label=ttk.Label(self, text="How many files do you want to Process?")
    self.label.grid(column=0, row=0)


    self.button=ttk.Button(self, text="Next",command=self.nextButton)
    self.button.grid(column=1, row=1)

app = GUI_ATT() app.mainloop()

但现在我在运行代码时收到错误消息“TypeError: nextButton() missing 1 required positional argument: 'controller'”。所有其他页面都运行良好并正确链接到彼此。如果您需要更多信息,请告诉我。

干杯

标签: python-3.xtkinter

解决方案


你已经有了按钮和命令,你不需要再做一次。尝试这个:

class PageFour(tk.Frame):
    def __init__(self, parent,controller):
        tk.Frame.__init__(self,parent)
        self.controller = controller
        self.InitUI()

    def nextButton(self):
        if self.mychoice.get()=='1':
            self.controller.show_frame(PageTwo)

    def InitUI(self):
        self.mychoice=StringVar()

        self.combo = ttk.Combobox(self,width =15, textvariable=self.mychoice)
        self.combo['values']=(" ","1","2")
        self.combo.grid(column=1, row=0)
        self.label=ttk.Label(self, text="How many files do you want to Process?")
        self.label.grid(column=0, row=0)


        self.button=ttk.Button(self, text="Next",command=self.nextButton)
        self.button.grid(column=1, row=1)

推荐阅读