Home > Mobile >  How can I create a button with Tkinter that uses two commands, which one of them updates a label?
How can I create a button with Tkinter that uses two commands, which one of them updates a label?

Time:03-23

I am trying to create a Tkinter button that runs two commands, one primary function and the other one which updates a label when the button is pressed. I have been able to make it run with both the functions but it does not update the label. Any advice on this problem? I have included the code that I have tried. Thank you in advance.

from tkinter import *
from tkinter import ttk

csv_team_count = 0
team_spaces_left = 4 - csv_team_count

def add_a_team():
    print("testing")
    global csv_team_count
    csv_team_count  = 1

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

my_notebook = ttk.Notebook(root)
my_notebook.pack()

#creates frames for the notebook.
frame1_overview = Frame(my_notebook, width=600, height=500, bg="#5797ff")
frame2_teams = Frame(my_notebook, width=600, height=500, bg="#5797ff")
frame1_overview.pack(fill="both", expand=0,)
frame2_teams.pack(fill="both", expand=0)
my_notebook.add(frame1_overview, text="Overview",)
my_notebook.add(frame2_teams, text="Teams")


teams_updater_var = StringVar()
teams_updater_var.set("Spaces Left: " str(team_spaces_left))

def teams_updater():
    teams_updater_var.set("Spaces Left: " str(team_spaces_left))

def combined_TeamS_addT():
    teams_updater()
    add_a_team()

label_team_spaces_left = Label(frame2_teams, 
    textvariable=teams_updater_var,
    font=('Helvatical bold',15),
    bg="#5797ff",
    fg="white",
).pack(pady = 5)

button_team_add = Button(frame2_teams, 
    text="Add A Team",
    bg="white",
    height = 1,
    command=(combined_TeamS_addT)
).pack(pady = 10)

root.mainloop()

CodePudding user response:

Updated Code:

from tkinter import *
from tkinter import ttk

csv_team_count = 0
team_spaces_left = 4 - csv_team_count

def add_a_team():
    global csv_team_count
    csv_team_count  = 1
    print("testing")

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

my_notebook = ttk.Notebook(root)
my_notebook.pack()

#creates frames for the notebook.
frame1_overview = Frame(my_notebook, width=600, height=500, bg="#5797ff")
frame2_teams = Frame(my_notebook, width=600, height=500, bg="#5797ff")
frame1_overview.pack(fill="both", expand=0,)
frame2_teams.pack(fill="both", expand=0)
my_notebook.add(frame1_overview, text="Overview",)
my_notebook.add(frame2_teams, text="Teams")


teams_updater_var = StringVar()
teams_updater_var.set("Spaces Left: " str(team_spaces_left))

def teams_updater():
    global team_spaces_left
    if 0 < team_spaces_left <= 4:
        team_spaces_left = 4 - csv_team_count
    else:
        team_spaces_left = 0
    teams_updater_var.set("Spaces Left: " str(team_spaces_left))

def combined_TeamS_addT():
    add_a_team()
    teams_updater()

label_team_spaces_left = Label(frame2_teams, 
    textvariable=teams_updater_var,
    font=('Helvatical bold',15),
    bg="#5797ff",
    fg="white",
).pack(pady = 5)

button_team_add = Button(frame2_teams, 
    text="Add A Team",
    bg="white",
    height = 1,
    command=(combined_TeamS_addT)
).pack(pady = 10)

root.mainloop()
  • Related