[英]Tkinter, how to set state of some elements depending of checkbox status
I have variable pliki
which is from checkbox我有来自复选框的变量
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
This code don't work as I had meant to work.这段代码不工作,因为我打算工作。 I want if checkbox is checked set state of some elements to NORMAL but when not checked set to DISABLE
我想是否选中复选框将某些元素的状态设置为 NORMAL 但未选中时设置为 DISABLE
I've done something similar with a RadioButton()
by linking the command to a function that hides it:通过将命令链接到隐藏它的函数,我对
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)
Which links to:链接到:
def hide_other_widget(self):
self.other_widget.configure(state='disabled')
def show_other_widget(self):
self.other_widget.configure(state='normal')
I don't think you can do this with a single Checkbutton though because the command is only run when it is turned on and not when it is turned off (As described at http://effbot.org/tkinterbook/checkbutton.htm ).我不认为您可以使用单个 Checkbutton 执行此操作,因为该命令仅在打开时运行,而不在关闭时运行(如http://effbot.org/tkinterbook/checkbutton.htm 所述) . You could instead use a thread to continually check the button status and adjust your widget accordingly though!
您可以改为使用线程来不断检查按钮状态并相应地调整您的小部件!
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.