Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法在MessageWindow中获取复选框状态_Python_Checkbox_Tkinter - Fatal编程技术网

Python 无法在MessageWindow中获取复选框状态

Python 无法在MessageWindow中获取复选框状态,python,checkbox,tkinter,Python,Checkbox,Tkinter,我想打开一个带有多个复选框的消息框来选择一些选项。 MessageBox可以工作,但我无法访问复选框的状态。 我可以切换复选框(对类的访问有效),但无法获取状态。 如何在主窗口中恢复复选框状态 该计划应如何运作: 主窗口可以创建ChooseBox ChooseBox应提供可供选择的选项(这里有一个选项用于测试示例) Mein窗口应获得状态(此处使用测试按钮进行测试) (使用python27和windows—但在ubuntu上也不起作用) 不能有两个Tk()窗口,Tk窗口是唯一的,只能调用一次。取

我想打开一个带有多个复选框的消息框来选择一些选项。 MessageBox可以工作,但我无法访问复选框的状态。 我可以切换复选框(对类的访问有效),但无法获取状态。 如何在主窗口中恢复复选框状态

该计划应如何运作: 主窗口可以创建ChooseBox ChooseBox应提供可供选择的选项(这里有一个选项用于测试示例) Mein窗口应获得状态(此处使用测试按钮进行测试) (使用python27和windows—但在ubuntu上也不起作用)

不能有两个Tk()窗口,Tk窗口是唯一的,只能调用一次。取而代之的是将choosebox中的Tk替换为Toplevel。除此之外,我认为它应该有效

#!/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)