Tkinter 复选框如何控制相应的文本框小部件? Python
How does a Tkinter checkbox control corresponding textbox widget? Python
我有一个简单的 tkinter 程序,在单击“创建框”按钮后,它会创建一个 Checkbutton 和一个 Text 小部件。我试图 enable/disable 基于 Checkbutton 的文本小部件。例如-如果选中 Checkbutton,相应的 Text 按钮将被禁用。
下面的代码确实可以正常工作,但我不知道怎么做?
Checkbutton 如何跟踪相应的 Text 小部件?
我已经用多个 Checkbutton 尝试过,它们都似乎不会干扰其他的文本小部件。
from tkinter import *
root = Tk()
root.geometry('500x300')
def create_boxes():
def disable_text():
if textbox['state'] == NORMAL:
textbox['state'] = DISABLED
else:
textbox['state'] = NORMAL
checkbox = Checkbutton(root, command = disable_text)
textbox = Text(root, height= 1, width = 30)
checkbox.pack()
textbox.pack()
create_chechkbutton = Button(root, text= 'Create a checkbox and textbox', command= create_boxes)
create_chechkbutton.pack()
root.mainloop()
如有任何解释,我们将不胜感激。
复选框本身实际上并没有跟踪任何东西。这段代码所做的是 将复选框 绑定到 disable_text()
函数。实际上,这意味着只要选中复选框,就会调用 disable_text()
。因此,实际上是 函数 disable_text()
跟踪文本。
创建复选框时:checkbox = Checkbutton(root, command = disable_text)
,disable_text()
函数被传递给 command
参数。这告诉 tkinter 在按下复选按钮时调用 disable_text()
。
在disable_text()
中,有一个if
语句检查文本是否被禁用。如果文本 禁用,disable_text()
启用。如果文本 未 禁用,则 disable_text()
禁用 它。
请注意,这不会获取复选框的状态;也就是说,它不根据复选框的状态设置文本的状态:它根据文本的状态设置文本的状态。因此,如果文本 开始时 禁用而不是启用,那么当文本被启用时复选框将被选中,而当文本未被启用时复选框将被取消选中。您可以将 checkbox
设为 Button
而不是 Checkbutton
,并且代码是 运行.
不会有什么不同
如果您的程序中有多个复选按钮,每个复选按钮都有自己的文本,则必须使用 lambda
语句绑定 disable_text()
函数,并使用一个参数告诉它正在调用哪个复选按钮它。这是一个控制多个文本小部件的多个复选按钮的示例:
from tkinter import *
root = Tk()
root.geometry('500x300')
def create_boxes():
def disable_text(checkbox): ### EDITED LINE
textbox = checkbox.textbox ### ADDED LINE
if textbox['state'] == NORMAL:
textbox['state'] = DISABLED
else:
textbox['state'] = NORMAL
for x in range(0, 5): ### ADDED LINE
checkbox = Checkbutton(root)
checkbox.config(command=lambda checkbutton=checkbox: disable_text(checkbutton)) ### EDTIED LINE
textbox = Text(root, height= 1, width = 30)
checkbox.textbox = textbox ### ADDED LINE
checkbox.pack()
textbox.pack()
create_chechkbutton = Button(root, text= 'Create a checkbox and textbox', command=create_boxes)
create_chechkbutton.pack()
root.mainloop()
使用 for
循环,创建并打包复选按钮和文本小部件。 checkbox.textbox = textbox
创建 checkbox
的新属性,并将其分配给复选框,以便该复选框与该文本小部件相关联。行
checkbox.config(command=lambda checkbutton=checkbox: disable_text(checkbutton))
使复选按钮在调用它时将自身作为参数传递给 disable_text()
。然后,disable_text()
简单地获取与它在其参数中获得的复选按钮关联的文本小部件,并禁用或启用它。
我有一个简单的 tkinter 程序,在单击“创建框”按钮后,它会创建一个 Checkbutton 和一个 Text 小部件。我试图 enable/disable 基于 Checkbutton 的文本小部件。例如-如果选中 Checkbutton,相应的 Text 按钮将被禁用。
下面的代码确实可以正常工作,但我不知道怎么做? Checkbutton 如何跟踪相应的 Text 小部件?
我已经用多个 Checkbutton 尝试过,它们都似乎不会干扰其他的文本小部件。
from tkinter import *
root = Tk()
root.geometry('500x300')
def create_boxes():
def disable_text():
if textbox['state'] == NORMAL:
textbox['state'] = DISABLED
else:
textbox['state'] = NORMAL
checkbox = Checkbutton(root, command = disable_text)
textbox = Text(root, height= 1, width = 30)
checkbox.pack()
textbox.pack()
create_chechkbutton = Button(root, text= 'Create a checkbox and textbox', command= create_boxes)
create_chechkbutton.pack()
root.mainloop()
如有任何解释,我们将不胜感激。
复选框本身实际上并没有跟踪任何东西。这段代码所做的是 将复选框 绑定到 disable_text()
函数。实际上,这意味着只要选中复选框,就会调用 disable_text()
。因此,实际上是 函数 disable_text()
跟踪文本。
创建复选框时:checkbox = Checkbutton(root, command = disable_text)
,disable_text()
函数被传递给 command
参数。这告诉 tkinter 在按下复选按钮时调用 disable_text()
。
在disable_text()
中,有一个if
语句检查文本是否被禁用。如果文本 禁用,disable_text()
启用。如果文本 未 禁用,则 disable_text()
禁用 它。
请注意,这不会获取复选框的状态;也就是说,它不根据复选框的状态设置文本的状态:它根据文本的状态设置文本的状态。因此,如果文本 开始时 禁用而不是启用,那么当文本被启用时复选框将被选中,而当文本未被启用时复选框将被取消选中。您可以将 checkbox
设为 Button
而不是 Checkbutton
,并且代码是 运行.
如果您的程序中有多个复选按钮,每个复选按钮都有自己的文本,则必须使用 lambda
语句绑定 disable_text()
函数,并使用一个参数告诉它正在调用哪个复选按钮它。这是一个控制多个文本小部件的多个复选按钮的示例:
from tkinter import *
root = Tk()
root.geometry('500x300')
def create_boxes():
def disable_text(checkbox): ### EDITED LINE
textbox = checkbox.textbox ### ADDED LINE
if textbox['state'] == NORMAL:
textbox['state'] = DISABLED
else:
textbox['state'] = NORMAL
for x in range(0, 5): ### ADDED LINE
checkbox = Checkbutton(root)
checkbox.config(command=lambda checkbutton=checkbox: disable_text(checkbutton)) ### EDTIED LINE
textbox = Text(root, height= 1, width = 30)
checkbox.textbox = textbox ### ADDED LINE
checkbox.pack()
textbox.pack()
create_chechkbutton = Button(root, text= 'Create a checkbox and textbox', command=create_boxes)
create_chechkbutton.pack()
root.mainloop()
使用 for
循环,创建并打包复选按钮和文本小部件。 checkbox.textbox = textbox
创建 checkbox
的新属性,并将其分配给复选框,以便该复选框与该文本小部件相关联。行
checkbox.config(command=lambda checkbutton=checkbox: disable_text(checkbutton))
使复选按钮在调用它时将自身作为参数传递给 disable_text()
。然后,disable_text()
简单地获取与它在其参数中获得的复选按钮关联的文本小部件,并禁用或启用它。