[英]Button to close a pop up window using .destroy() gives error that the pop-up window is not defined. Using Python tkinter
[英]Tkinter pop-up window with check button, not able to get check button states
我正在按主窗口上的按鈕制作一個彈出窗口。 彈出窗口中有許多檢查按鈕,我需要將檢查按鈕的狀態返回到主窗口,還需要使用全選和取消全選按鈕。 但是我在使用變量時出錯,無法將狀態傳遞回主窗口。
這是我編寫的程序:
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 [[]]來獲取檢查按鈕的狀態。
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
我應該能夠使用全選/取消全選按鈕,並且還能夠將每個檢查按鈕的所有狀態返回到主窗口。
使用類的示例代碼。
它使用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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.