如何在按下 tkinter 时更改按钮状态
How to change button state upon press 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)
唯一真正相关的部分是 geometric_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()
我正在编写一个可以做很多事情的计算器,我想问一下如何在按下按钮时更改按钮状态。我正在使用以下代码:
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)
唯一真正相关的部分是 geometric_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()