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

时间:2018-07-06 20:11:02

标签: 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

        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')

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

1 个答案:

答案 0 :(得分:2)

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)