简体   繁体   English

如何在按下 tkinter 后更改按钮 state

[英]How to change button state upon press tkinter

I am programming a calculator that does many things and I wanted to ask how to change a buttons state upon its press.我正在编写一个可以做很多事情的计算器,我想问一下如何在按下按钮时更改按钮 state。 I am working with the following code:我正在使用以下代码:

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)

The only relevant part really is the geometric_calc_press function.唯一相关的部分是geometric_calc_press function。 i wanted to be able to fix a bug where the close button deosnt work if the formula button is pressed multiple times (the close button then needs to be pressed the same amount of times.)so I wanted to disable the formula button when pressed until the close button is pressed.我希望能够修复一个错误,如果多次按下公式按钮(关闭按钮然后需要按下相同的次数),关闭按钮不起作用。所以我想在按下时禁用公式按钮直到关闭按钮被按下。 I don't really know how to do that though so i am asking here.我真的不知道该怎么做,所以我在这里问。

You can deactivate button with您可以使用停用按钮

button.config(state="disabled")

and activate it back with并用

button.config(state="normal")

or或者

button.config(state="active")

BTW: the same way you can also deactivate other widgets - ie.顺便说一句:您也可以以同样的方式停用其他小部件 - 即。 Label , Entry , etc. Other widgets may have different states - ie. LabelEntry等。其他小部件可能有不同的状态 - 即。 Entry has state "read-only" (but it doesn't have "active" ) Entry有 state "read-only" (但它没有"active"


Minimal working example最小的工作示例

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM