0

I want to build a little GUI application in Python. The goal is to have a main window calling several other windows. In one of these called windows I have a checkbutton. My problem is that I cannot read the value of this checkbutton, whereas I can read the value of an Entry widget. What am I doing wrong?

    from tkinter import *
    import tkinter as tk


    class mainwindow():
        def __init__(self, master):

            self.master = master
            menubalk = Menu(self.master)

            menubalk.add_command(label="New window", command=self.openNewwindow)
            self.master.config(menu=menubalk)

        def openNewwindow(self):
            window = newwindow()
            window.mainloop()

    class newwindow(Tk):

        def __init__(self):
            Tk.__init__(self)

            self.var = BooleanVar()
            self.checkbutton = Checkbutton(self, text="Check", variable=self.var)
            self.checkbutton.grid(column=0, row=0)

            self.var2 = StringVar()
            self.entry = Entry(self, textvariable=self.var2)
            self.entry.grid(column=2,row=0)

            self.button2 = Button(self,text=u"Show", command=self.showValues).grid(column=1, row=0)

        def showValues(self):
            print('Value checkbutton:', self.var.get(), ';', 'Value entryfield: ', self.entry.get())

    def main():
        root = Tk()
        window = mainwindow(root)
        root.mainloop()

    if __name__ == '__main__':
        main()
4
  • What do you mean by "cannot read the value"? What do you get when you call self.var.get()? Commented Mar 3, 2016 at 11:15
  • I always get False, also when the checkbutton is checked. Commented Mar 3, 2016 at 11:30
  • Could you try explicitly setting the check button's on and off values? (E.g. Checkbutton(self, text="Check", variable=self.var, onvalue=True, offvalue=False)) Commented Mar 3, 2016 at 11:34
  • Tried it, same result. Commented Mar 3, 2016 at 11:38

2 Answers 2

2

You are making multiple, separate Tkinter applications in your program. Do not do that. To create new windows, use the Toplevel widget.

from tkinter import *

class mainwindow():
    def __init__(self, master):

        self.master = master
        menubalk = Menu(self.master)

        menubalk.add_command(label="New window", command=self.openNewwindow)
        self.master.config(menu=menubalk)

    def openNewwindow(self):

        def showValues(var, entry):
            print('Value checkbutton:', var.get(), ';', 'Value entryfield: ', entry.get())

        window = Toplevel(self.master)
        var = BooleanVar()
        checkbutton = Checkbutton(window, text="Check", variable=var)
        checkbutton.grid(column=0, row=0)

        var2 = StringVar()
        entry = Entry(window, textvariable=var2)
        entry.grid(column=2,row=0)

        button2 = Button(window,text=u"Show", command=lambda: showValues(var, entry))
        button2.grid(column=1, row=0)

def main():
    root = Tk()
    window = mainwindow(root)
    root.mainloop()

if __name__ == '__main__':
    main()
Sign up to request clarification or add additional context in comments.

4 Comments

When I run this code, I get an error when I click the menu item 'New window' in the main window: AttributeError: 'mainwindow' object has no attribute 'window'.
@Erwin - Sorry, I forgot to change that when I turned it all into local variables. It's called window at the top of the function, so it should be referred to as window throughout the rest of the function (not self.window).
Sorry Tigerhawkt3, this still does not work correctly. I'm getting an error: AttributeError: 'mainwindow' object has no attribute 'var'.
My apologies; same problem and same solution. Change self.var to var and self.var2 to var2.
-1

Tkinter's variable objects (IntVar, StringVar, etc.) must take argument "master" as their firs parameter. i.e. replace

self.var=StringVar()

With

self.var=StringVar(self)

Or

self.var=StringVar(master=self)

1 Comment

That isn't what's causing the problem, isn't necessary at all, and isn't even recommended in the (unofficial) documentation.

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.