jeudi 3 mars 2016

Python tkinter checkbutton value not accesible

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




Aucun commentaire:

Enregistrer un commentaire