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()
Cœur
  • 37,241
  • 25
  • 195
  • 267
Erwin
  • 11
  • 4

2 Answers2

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()
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • 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 Mar 03 '16 at 12:54
  • @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`). – TigerhawkT3 Mar 03 '16 at 13:01
  • Sorry Tigerhawkt3, this still does not work correctly. I'm getting an error: AttributeError: 'mainwindow' object has no attribute 'var'. – Erwin Mar 03 '16 at 13:15
  • My apologies; same problem and same solution. Change `self.var` to `var` and `self.var2` to `var2`. – TigerhawkT3 Mar 03 '16 at 13:26
-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)
pinogun
  • 110
  • 1
  • 7
  • That isn't what's causing the problem, isn't necessary at all, and isn't even recommended in the (unofficial) [documentation](http://effbot.org/tkinterbook/variable.htm). – TigerhawkT3 Mar 03 '16 at 22:02