Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I have this code where I am trying to have it so I can only press the button once and then it becomes disabled, but I keep getting the error TypeError: 'NoneType' object is not subscriptable, I’m not so sure what I am doing incorrect. Any help is appreciated. Here is my code:

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The problem is in your following code segment, where you are creating a button

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

You are calling pack(pady = 10) immediately on the button due to which your button_event_1 object is not referring to the button but None returned by pack()

the solution is simple, remove it from there and then pack the button separately, after button initialization

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

button_event_1.pack()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading