user3172304
user3172304

Reputation: 1

Cannot get Checkbox status in MessageWindow

I want to open a MessageBox with several Checkboxes to choose some options. The MessageBox works, but I cannot access the status of the checkboxes. I can toggle the checkbox (access to the class works) but I am not able to get the status. How can I get back the Checkboxes status in the main window?

How the program should work: Main window can create ChooseBox ChooseBox should give options to choose (here one Option for test example) Mein window shall get status (tested here with the test button) (working with python27 and windows - but on ubuntu it did not work neither)

#!/usr/bin/python3
# -*- coding: cp1252 -*-

from Tkinter import *

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

        self.var = IntVar()
        self.chk = Checkbutton(self, text="Option 1", variable=self.var)
        self.chk.pack()
        # Button to show the status of the checkbutton
        button = Button(self, text='Show Stat',
                            command=lambda: self.Status(self.var))
        button.pack()

    def Status(self, var):
        print var.get()

def message():
    global Choose
    Choose = ChooseBox()
    Choose.mainloop()

def test():
    global Choose
    Choose.chk.toggle()
    print Choose.var.get()

def main_wrapper(argv):
    global Choose
    root = Tk()
    root.geometry("200x150+30+30")

    Button_Frame=Frame(root)
    Button_Frame.pack(side=BOTTOM, anchor=W, fill=X, expand=NO)

    Button(Button_Frame, text='Make ChooseBox', command=message).pack(side=LEFT, anchor=W, padx=5, pady=5)
    # Button to test access to the Box - here it toggles the Checkbutton and (should) prints the status
    Button(Button_Frame, text='test', command=test).pack(side=LEFT, anchor=W, padx=5, pady=5)
    Button(Button_Frame, text='Quit', command=root.quit).pack(side=RIGHT, anchor=E, padx=5)

    root.mainloop()


if __name__ == '__main__':
   main_wrapper(sys.argv)

Upvotes: 0

Views: 117

Answers (1)

You can not have two Tk() windows, the Tk window is unique and can only be called once. Instead replace the Tk in your choosebox with a Toplevel instead. Other than that I think it should work

Upvotes: 1

Related Questions