【问题标题】:How to disable a button in tkinter? [duplicate]如何禁用 tkinter 中的按钮? [复制]
【发布时间】:2020-12-19 15:47:53
【问题描述】:

当我单击一个按钮时,会显示一个新按钮,但我希​​望禁用之前单击的按钮。

import tkinter as tk

def newbutton():  
    newbtn = tk.Button(app, text = "New Window button")
    newbtn.pack() 

app = tk.Tk()
buttonExample = tk.Button(app, text="Create new window", command=newbutton)
buttonExample.pack()

app.mainloop()

【问题讨论】:

  • 配置之前tk.Button的状态为tk.DISABLED
  • newbutton()函数上添加buttonExample.config(state=tk.DISABLED)

标签: python tkinter


【解决方案1】:

嗨,你有没有尝试过像这样使用 state 命令

buttonExample = tk.Button(app, text="创建新窗口", command=newbutton, state=DISABLED)

【讨论】:

    【解决方案2】:

    你可以实现一个非常简单的功能,可以禁用一个按钮

    def disableButton(my_button):
        my_button.config(state='disabled')
    

    堆栈上已经有一个帖子在谈论 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2023-04-10
      • 1970-01-01
      • 2018-03-13
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多