python - 如何在按下 tkinter 时更改按钮状态
问题描述
我正在编写一个可以做很多事情的计算器,我想问一下如何在按下按钮时更改按钮状态。我正在使用以下代码:
import math, sqlite3
from tkinter import *
root = Tk()
x_are = Label(root)
x_are.grid(row=0, column=0)
x_are_ = Label(root)
x_are_.grid(row=0, column=0)
boo = True
bool_ = True
formula_entry = ''
def geometric_calc_press():
global bool_
def formula():
global bool_, state, formula_entry
if bool_:
formula_entry = Entry(geometric_calc, width=66)
formula_entry.grid(row=1, column=0, columnspan=1000)
bool_ = False
def show_searched_formula():
global formula_entry
x = formula_entry.get()
print(x)
def close():
global bool_, state, formula_entry, close, search_
bool_ = True
formula_entry.destroy()
close_.destroy()
search.destroy()
if not bool_:
close_ = Button(geometric_calc, text='Close', command=close, bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=80, pady=20)
close_.grid(row=3, column=0)
search = Button(geometric_calc, text='Search', command=show_searched_formula, bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=77, pady=20)
search.grid(row=2, column=0)
global root
root.destroy()
geometric_calc = Tk()
bindings = {
'<FocusIn>': {'default': 'active'},
'<FocusOut>': {'default': 'active'}
}
for k, v in bindings.items():
geometric_calc.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))
geometric_calc.geometry('400x415')
geometric_calc.config(bg='black')
formula_finder = Button(geometric_calc, text='Formulas', bg='black', fg='lime', highlightcolor="lime",
highlightbackground="lime", highlightthickness=2, relief=SOLID, default='active', padx=70,
command=formula)
formula_finder.grid(row=0, column=0)
formula_user = Button(geometric_calc, text='Calculate', bg='black', fg='lime', highlightcolor="lime",
highlightbackground="lime", highlightthickness=2, relief=SOLID, default='active', padx=70)
formula_user.grid(row=0, column=2)
唯一相关的部分是 geometry_calc_press 函数。我希望能够修复一个错误,如果多次按下公式按钮(关闭按钮然后需要按下相同的次数),关闭按钮不起作用。所以我想在按下时禁用公式按钮直到关闭按钮被按下。我真的不知道该怎么做,所以我在这里问。
解决方案
您可以使用停用按钮
button.config(state="disabled")
并用
button.config(state="normal")
或者
button.config(state="active")
顺便说一句:您也可以以同样的方式停用其他小部件 - 即。Label
, Entry
, 等等。其他小部件可能有不同的状态 - 即。Entry
有状态"read-only"
(但没有"active"
)
最小的工作示例
import tkinter as tk # PEP8: `import *` is not preferred
# --- functions ---
def on_press_1():
b1.config(state="disabled")
l1.config(state="disabled")
e1.config(state="disabled") # or `readonly`
def on_press_2():
b1.config(state="normal") # or `active`
l1.config(state="normal") # or `active`
e1.config(state="normal")
# --- main ---
root = tk.Tk()
l1 = tk.Label(root, text="Label Text")
l1.pack()
e1 = tk.Entry(root)
e1.insert(0, "Entry Text")
e1.pack()
b1 = tk.Button(root, text="Deactivate", command=on_press_1)
b1.pack()
b2 = tk.Button(root, text="Activate", command=on_press_2)
b2.pack()
root.mainloop()
推荐阅读
- php - chrome浏览器硬重置后,我的下拉菜单看起来很大
- html - 徽标与导航栏对齐良好。而我后来添加的东西总是从顶部开始
- python - 如何在同一个视图中有两个 POST 请求而不会出现 MultiValueDictKeyError?
- sql - ORA-12801:在并行查询服务器 P02T 中发出错误信号 ORA-30926:无法在源表中获得一组稳定的行
- mongodb - 将元素添加到 MongoDB 中对象数组中的对象中的数组
- cassandra - 如何在 Nifi 中设置 QueryCassandra 处理器的增量或最大值列?
- cmake - 在 Mac 上设置 VulkanGuide 教程
- javascript - 在 jsplumb 社区版中需要帮助以拖放不同的操作组件
- android - 将 layout_height 更改为“wrap_content”会缩小组件
- excel - excel时间少于3小时条件格式