0

I'm trying to change the page that a "continue" button will bring the user to in a Tkinter GUI using the input of a checkbox. Right now, all three pages show up in one page.

Starting by initializing 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()

Then creating the Start Page

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

Then writing the other two pages

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

And

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

1 Answer 1

2

Tkinter variables do not work like python variables. You need to use the get() method to get the value out. Also, you don't need that extra closure; just call the function directly.

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.