Python 3.x 如何在tkinter中启用和禁用按钮?
我试图看到,当用户向输入字段添加内容时,我的按钮被启用,然后当用户删除该输入时,我的按钮被禁用。禁用按钮的方法是使用以下代码:Python 3.x 如何在tkinter中启用和禁用按钮?,python-3.x,tkinter,Python 3.x,Tkinter,我试图看到,当用户向输入字段添加内容时,我的按钮被启用,然后当用户删除该输入时,我的按钮被禁用。禁用按钮的方法是使用以下代码: button = tkinter.Button(root, text='enable/disable')) def switchButtonState(): if (button['state'] == tk.NORMAL): button['state'] = tk.DISABLED else: button['stat
button = tkinter.Button(root, text='enable/disable'))
def switchButtonState():
if (button['state'] == tk.NORMAL):
button['state'] = tk.DISABLED
else:
button['state'] = tk.NORMAL
root.after(1000, check_text_box) # I am pretty sure the 1000 is number of milliseconds
要从文本小部件中获取输入,意味着您必须运行以下代码:
button = tkinter.Button(root, text='enable/disable'))
def switchButtonState():
if (button['state'] == tk.NORMAL):
button['state'] = tk.DISABLED
else:
button['state'] = tk.NORMAL
root.after(1000, check_text_box) # I am pretty sure the 1000 is number of milliseconds
after函数每1000毫秒调用一次check_text_box函数
以下是使其工作的完整代码:
def check_to_disable_enable():
text = text_widget_name.get(1.0, tkinter.END)
if text == '': # If the textbox is empty
button['state'] = tk.DISABLED # Disables button
else: # If the textbox is not empty
button['state'] = tk.NORMAL # Enables button
# Call this function when the frame comes up
root.after(100, check_to_disable_enable)
我意识到这可能有点混乱,如果是的话,请不要避免问问题。如果这回答了您的问题,请将其标记为已回答。到目前为止您做了什么?您可以显示任何代码吗?请查看帮助中心以改进您的问题:谢谢您回答我,但我所寻找的与您发送给我的有点不同。实际上,我想了解当用户在输入窗口小部件中添加内容时,我的一个按钮被启用,然后当用户尝试清除该按钮时再次输入我的按钮被禁用如何编码这对我来说有点困难