Python Tkinter复选框按钮更改tk.button命令

Python Tkinter复选框按钮更改tk.button命令,python,tkinter,Python,Tkinter,我试图通过复选框的输入来更改“继续”按钮将用户带到Tkinter GUI中的页面。现在,所有三个页面都显示在一个页面中 从初始化Tkinter开始 def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs)#initialized tkinter container = tk.Frame(self)#define our tkinter container

我试图通过复选框的输入来更改“继续”按钮将用户带到Tkinter GUI中的页面。现在,所有三个页面都显示在一个页面中

从初始化Tkinter开始

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)#initialized tkinter
        container = tk.Frame(self)#define our tkinter container

        container.pack(side ="top", fill="both", expand=True)

        container.grid_rowconfigure(0,weight=1)
        container.grid_columnconfigure(0,weight=1)
        self.frames = { }        
        for F in (StartPage, PageOne, PageTwo):            
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row =0, column =0, sticky ="nsew")

        self.show_frame(StartPage)

        def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()
然后创建起始页

    class StartPage(tk.Frame):

        def __init__(self, parent, controller):

            tk.Frame.__init__(self, parent)

            chk_state = tk.BooleanVar()
            chk_state.set(False)#uncheck
            chk = tk.Checkbutton(self, text = "Select a slice range",font =(20), var=chk_state)#, command = click())
            chk.place(relx=.5,rely=.39,anchor='center')

            def browsefunc3():
                if chk_state == False:
                    command = lambda: self.controller.show_frame(PageOne)
                else:
                    command = lambda: self.controller.show_frame(PageTwo)
                return command
            button3 = tk.Button(text="Continue", bg = "white", fg = 'black',font=(20), command = lambda: browsefunc3())
            button3.place(relx=.5, rely =.75, anchor='center')
然后写另外两页

    class PageOne(tk.Frame):
        def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)
            button3 = tk.Button(text="One", bg = "white", fg = 'black',font=(20), command = lambda: self.controller.show_frame(StartPage))
            button3.place(relx=.5, rely =.75, anchor='center')


Tkinter变量的工作方式与python变量不同。您需要使用
get()
方法获取值。而且,你不需要额外的闭包;直接调用函数即可

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

        self.chk_state = tk.BooleanVar(value=False)#uncheck
        chk = tk.Checkbutton(self, text = "Select a slice range",font =(20), var=self.chk_state)
        chk.place(relx=.5,rely=.39,anchor='center')

        button3 = tk.Button(text="Continue", bg = "white", fg = 'black',font=(20), command = self.browsefunc3)
        button3.place(relx=.5, rely =.75, anchor='center')

    def browsefunc3(self):
        if self.chk_state.get():
            self.controller.show_frame(PageTwo)
        else:
            self.controller.show_frame(PageOne)

Tkinter变量的工作方式与python变量不同。您需要使用
get()
方法获取值。而且,你不需要额外的闭包;直接调用函数即可

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

        self.chk_state = tk.BooleanVar(value=False)#uncheck
        chk = tk.Checkbutton(self, text = "Select a slice range",font =(20), var=self.chk_state)
        chk.place(relx=.5,rely=.39,anchor='center')

        button3 = tk.Button(text="Continue", bg = "white", fg = 'black',font=(20), command = self.browsefunc3)
        button3.place(relx=.5, rely =.75, anchor='center')

    def browsefunc3(self):
        if self.chk_state.get():
            self.controller.show_frame(PageTwo)
        else:
            self.controller.show_frame(PageOne)