0

I have got stuck with this particular bit of code. I have condensed down the problem to this section.

I am running a sort of menu in Python, where the first menu sends you to the second menu, and in the second menu, there is a checkbutton the user can toggle on/off. In the third menu, I want it to read if the checkbutton is on/off and convert it to a Boolean. Code:

import tkinter as tk

class MainMenu(object):
    def __init__(self):
        self.launch_MainMenu()
    def launch_MainMenu(self):
        self.master = tk.Tk()
        tk.Button(self.master,text="MY BUTTON",command= lambda:self.launch_SideMenu()).grid()
        tk.mainloop()
    def launch_SideMenu(self):
        self.master2 = tk.Tk()
        self.var1 = tk.IntVar()
        tk.Checkbutton(self.master2,variable=self.var1).grid()
        tk.Button(self.master2,text="Test",command= lambda:self.launch_FinalMenu()).grid()
    def launch_FinalMenu(self):
        d = bool(int(self.var1.get()))
        print(d,self.var1.get())

mainMenu = MainMenu()

Output: Whether the checkbox is on or off, it outputs "False 0".

Any help would be very much appreciated!

4
  • 1
    You are creating more than one Tk() instance. You can't do that. Also it'd be better if you create your class variables(i.e. self.var1) in __init__ method. Commented Jan 11, 2018 at 5:58
  • Thank you very much for your comment Lafexlos. Is there any other way to open up a new window box other than tk.Tk()? Commented Jan 11, 2018 at 6:04
  • 1
    You can use tkinter.Toplevel() Commented Jan 11, 2018 at 6:07
  • Thank you so much for your help! I found it at the same time as you answered! This has fixed it. Commented Jan 11, 2018 at 6:10

1 Answer 1

3

As per the hint from Lafexlos, the error is in calling tk.Tk() twice. For a new window, you must use tk.Toplevel().

Simply changing the keyline to:

self.master2 = tk.Toplevel()

fixes everything. This took me a long time to work out. Thanks for the help, and best of luck to you coders reading this in the future.

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.