簡體   English   中英

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

[英]How to change button state upon press tkinter

我正在編寫一個可以做很多事情的計算器,我想問一下如何在按下按鈕時更改按鈕 state。 我正在使用以下代碼:

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 function。 我希望能夠修復一個錯誤,如果多次按下公式按鈕(關閉按鈕然后需要按下相同的次數),關閉按鈕不起作用。所以我想在按下時禁用公式按鈕直到關閉按鈕被按下。 我真的不知道該怎么做,所以我在這里問。

您可以使用停用按鈕

button.config(state="disabled")

並用

button.config(state="normal")

或者

button.config(state="active")

順便說一句:您也可以以同樣的方式停用其他小部件 - 即。 LabelEntry等。其他小部件可能有不同的狀態 - 即。 Entry有 state "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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM