我怎样才能做到 Tkinter 按钮只能按一次?

How can I make it so the Tkinter button can only be pressed once?

我有这段代码,我正在尝试使用它,所以我只能按一次按钮,然后它就会被禁用,但我一直收到错误 TypeError: 'NoneType' object is not subscriptable,我不太确定我在做什么我做错了。任何帮助表示赞赏。这是我的代码:

import tkinter as tk
from tkinter import *
from tkinter import ttk

team_array = []
team_dict = {}

def score_event1_team():
    global team_array
    global team_dict

    #First Event, 1st Team, Adding 15 points.
    event1T = str(input("What team was first place?"))
    
    #ADDING 15 POINTS TO FIRST TEAM IN THE DICTIONARY WITH GET METHOD.
    team_dict[event1T] = team_dict.get(event1T, 0) + 15
    print(team_dict)
    print(f"Team {event1T} has been added 15 points.")

    if (button_event_1['state'] == NORMAL):
        button_event_1['state'] = DISABLED


root = Tk()
root.title("Sports Event Organiser!")
root.geometry("600x500")

button_event_1 = Button(root,
    text="Event Pending",
    bg="white",
    height = 1,
    command=(score_event1_team),
    state = NORMAL,
).pack(pady = 10)


root.mainloop()

问题出在您创建按钮的以下代码段中

button_event_1 = Button(root,
    text="Event Pending",
    bg="white",
    height = 1,
    command=(score_event1_team),
    state = NORMAL,
).pack(pady = 10)

您正在按钮上立即调用 pack(pady = 10),因此您的 button_event_1 对象不是指按钮,而是 pack()[=15= 返回的 None ]

解决方法很简单,把它从那里去掉,然后单独打包按钮,在按钮初始化之后

button_event_1 = Button(root,
    text="Event Pending",
    bg="white",
    height = 1,
    command=(score_event1_team),
    state = NORMAL,
)

button_event_1.pack()

你做错的是这一行 button_event_1 = Button(....).pack() 因为你用按钮打包它并打包 returns 一个非类型对象你需要在不同的行中执行 button_event_1 = Button(....) 和 button_event_1.pack() 。 pack 函数 returns 是一个 none 类型的对象,因此在您的问题中 button_event_1 是 None