0
我想提出一個彈出窗口,對主窗口中的按鈕被按下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()
我不能夠利用變量瓦爾all_state和國家[[]],拿到支票button.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按鈕,也無法取回每個檢查所有按鈕狀態到主窗口。
隨時添加有關完整的錯誤消息(回溯)。 – furas
你在popup()裏面創建'all_states',所以它不是全局變量。在所有函數外部創建'all_states = None',這將是全局變量。然後在兩個函數中使用'global all_states'來通知函數,他們必須使用外部/全局變量而不是創建本地變量。 – furas
順便說一句:你會遇到同樣的問題'瓦爾斯' – furas