带有检查按钮的Tkinter弹出窗口,无法获得检查按钮状态

时间:2016-12-27 05:11:50

标签: python tkinter

按下主窗口上的按钮,我正在弹出一个窗口。弹出窗口有很多检查按钮,我需要将检查按钮的状态恢复到主窗口,并且还要使用全选和取消全选按钮。但是我使用变量时出错,并且无法将状态传递回主窗口。

这是我写的程序:

import Tkinter
import tkMessageBox

top = Tkinter.Tk()
state=[[]]

def popup(x):

    def select_clear_states():
        global Vars, all_states
        states = all_states.get()

        if states == 0:
            for I in range(len(Vars)):
                Vars[I].set(0)
        if states == 1:
            for I in range(len(Vars)):
                Vars[I].set(1)

    Vars = []

    root = Tkinter.Tk()

    all_states = Tkinter.IntVar()
    select_all = Tkinter.Checkbutton(root, text = "select/deselect all",variable = all_states, command = select_clear_states)
    select_all.grid(row = 0, column = 0, padx = 5, pady = 1)

    for n in range(10):

        var = Tkinter.IntVar()
        Vars.append(var)
        checkbox = Tkinter.Checkbutton(root, text = n+1, variable= Vars[n])
        checkbox.grid(row = n+1, column = 0, padx = 5, pady = 1)

    root.mainloop()


A = Tkinter.Button(top, text ="Hello1",command=lambda: popup(1))
A.pack()

B = Tkinter.Button(top, text ="Hello2",command=lambda: popup(2))
B.pack()

C = Tkinter.Button(top, text ="Hello3",command=lambda: popup(3))
C.pack()

top.mainloop()

我无法使用变量Vars all_state和state [[]]来获取检查按钮的状态.am得到此错误

Exception in Tkinter callback
  Traceback (most recent call last):
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in __call__
      return self.func(*args)
    File "C:\Users\Synapse\Documents\Neo\Tutorial\python_tutorial\button.py", line 11, in select_clear_states
      states = all_states.get()
  NameError: global name 'all_states' is not defined

我应该可以使用select / deselectall按钮,并且还可以将每个复选按钮的所有状态恢复到主窗口。

1 个答案:

答案 0 :(得分:1)

使用类的示例代码。

使用Toplevel()创建PopUp,因为Tkinter应该只有一个Tk()窗口。

它只使用一个mainloop(),因为更多循环会使变量中的值出现问题。

它可以创建不同数量的复选框。

需要更多的工作 - 即。返回值 - 但现在您可以使用win.states获取所有复选框的状态(或设置它们)。

import Tkinter as tk
import tkMessageBox

# --- classes --- (CamelCase names)

class PopUp(tk.Toplevel):

    def __init__(self, number=10):
        tk.Toplevel.__init__(self)

        # - global checkbox to set all "small" checkboxes -

        self.global_state = tk.BooleanVar()

        cb = tk.Checkbutton(self, text="select/deselect all",
                            variable=self.global_state,
                            command=self.select_clear_states)
        cb.grid(row=0, column=0, padx=5, pady=1)

        # - "small" checkboxes -

        self.states = []

        for n in range(1, number+1):
            var = tk.BooleanVar()
            cb = tk.Checkbutton(self, text=str(n), variable=var)
            cb.grid(row=n, column=0, padx=5, pady=1)
            self.states.append(var)


    def select_clear_states(self):
        # get global checkbox
        state = self.global_state.get()

        # set all "small" checkboxes
        for x in self.states:
            x.set(state)

# --- functions --- (lower_case names)

def popup(num):

    win = PopUp(num)
    #win.states[1].set(True)

# --- main --- (lower_case names)

root = tk.Tk()

b = tk.Button(root, text="5 checkboxes", command=lambda:popup(5))
b.pack()

b = tk.Button(root, text="10 checkboxes", command=lambda:popup(10))
b.pack()

b = tk.Button(root, text="3 checkboxes", command=lambda:popup(3))
b.pack()

root.mainloop()