Erwin
Erwin

Reputation: 11

Python tkinter checkbutton value not accessible

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

Upvotes: 0

Views: 1360

Answers (2)

pinogun
pinogun

Reputation: 108

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)

Upvotes: -1

TigerhawkT3
TigerhawkT3

Reputation: 49330

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

Upvotes: 2

Related Questions