我有来自复选框的变量 pliki
pliki = IntVar()
plikiC = Checkbutton(secunderFrame, text="Twórz pliki",
font=("Bookman Old Style", 8, 'bold'),
variable=pliki, onvalue=True, offvalue=False)
if pliki.get() == 1:
liczba_pE['state'] = NORMAL
nazwa_pE['state'] = NORMAL
tresc_pE['state'] = NORMAL
if pliki.get() == 0:
liczba_pE['state'] = DISABLED
nazwa_pE['state'] = DISABLED
tresc_pE['state'] = DISABLED
此代码无法正常工作。 如果选中复选框,我想将某些元素的状态设置为正常,但未选中时设置为禁用
请您参考如下方法:
我通过将命令链接到隐藏它的函数,对 RadioButton() 做了类似的事情:
v = tk.IntVar()
tk.Radiobutton(self.widget, text="Turn on", variable=v, value=1, command=self.show_other_widget).grid(row=0, column=0)
tk.Radiobutton(self.widget, text="Turn off", variable=v, value=2, command=self.hide_other_widget).grid(row=0, column=1)
链接到:
def hide_other_widget(self):
self.other_widget.configure(state='disabled')
def show_other_widget(self):
self.other_widget.configure(state='normal')
我不认为您可以使用单个 Checkbutton 来执行此操作,因为该命令仅在打开时运行,而不是在关闭时运行(如 http://effbot.org/tkinterbook/checkbutton.htm 中所述)。不过,您可以改为使用线程来持续检查按钮状态并相应地调整您的小部件!
import threading
def check_button_status():
while True:
status = pliki.get()
if status == 1:
liczba_pE['state'] = NORMAL
nazwa_pE['state'] = NORMAL
tresc_pE['state'] = NORMAL
else:
liczba_pE['state'] = DISABLED
nazwa_pE['state'] = DISABLED
tresc_pE['state'] = DISABLED
pliki = IntVar()
plikiC = Checkbutton(secunderFrame, text="Twórz pliki",
font=("Bookman Old Style", 8, 'bold'),
variable=pliki, onvalue=True, offvalue=False)
t = threading.Thread(target=check_button_status) # make our thread
t.start() # have it monitor the button status forever
