开发者社区> 问答> 正文

tkinter 如何启用和禁用按钮?

我试图如此实现我的功能, * 当用户向输入字段中添加某些内容时,我的按钮被启用, * 当用户删除该条目时,我的按钮被禁用。

问题来源:stackoverflow

展开
收起
is大龙 2020-03-24 21:38:14 4482 0
1 条回答
写回答
取消 提交回答
  • 禁用按钮的一种方法是使用以下代码:

    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)
    

    我意识到这可能有点令人困惑,如果是这样,请不要提出任何问题。如果这回答了您的问题,请标记为已回答。

    回答来源:stackoverflow

    2020-03-24 21:38:21
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载