python-3.x - Python:禁用和重新启用按钮
问题描述
我想创建一个我可以点击disable
的re-enable
按钮。使用以下代码,我得到一个按钮,我可以disable
,但是re-enable
当我单击它时它不会。如果查看此威胁,但没有帮助:禁用/启用 TKinter 中的按钮
import tkinter
from tkinter import *
# toplevel widget of Tk which represents mostly the main window of an application
root = tkinter.Tk()
root.geometry('1800x600')
root.title('Roll Dice')
frame = Frame(root)
# label to display dice
label = tkinter.Label(root, text='', font=('Helvetica', 120))
# function activated by button
def switch1():
if button1["state"] == "active":
button1["state"] = "disabled"
else:
button1["state"] = "active"
button1 = tkinter.Button(root, text='Würfel 1', foreground='green', command=lambda: switch1, state = "active")
button1.pack(side = LEFT)
root.mainloop()
解决方案
看这个:
import tkinter as tk
def toggle_state():
if button1.cget("state") == "normal":
button1.config(state="disabled")
else:
button1.config(state="normal")
root = tk.Tk()
button1 = tk.Button(root, text="This button toggles state")
button1.pack()
button2 = tk.Button(root, text="Click me", command=toggle_state)
button2.pack()
root.mainloop()
这使用一个按钮来切换另一个按钮的状态。
推荐阅读
- sql-server - 如何在 Django 中连接来自两个不同数据库的两个表?
- x86 - 在完全虚拟化环境中,来宾操作系统系统调用会发生什么?
- jenkins - Jenkins 作业未能执行日志轮换消息
- php - 如何在 docker 容器中启用 php-fpm 的访问日志?
- vbscript - WshShell.Run 在命令行参数中带有空格
- java - 当按下 Enter 时,Java 中的循环不会中断,当条件是 DataInputStream.isAvailable() == 0 并且应用程序使用 telnet 运行时
- javascript - 如何将句子而不是单个单词发送到 API 并让它们发回
- c# - 通过 FuelSDK 从 C# 调用 Marketing Cloud API
- java - 无法使用 Java 使用 JAXB 读取 XML 文件的子节点中的内容
- javascript - Discord.JS 如何从旧消息中获取表情