Veitch
Veitch

Reputation: 27

Python tkinter: Continuously updating labels in frame

In my code I have a spin box that the user uses to select the number of teams they want, then in a separate frame columns are created to match the number of teams they chose, and when they use the spin box to change this value it also changes the number of columns

frame=Frame(root)
frame.pack(anchor=CENTER)
Label(frame,text='Team selection').grid(row=0,column=0,columnspan=5)

NumTeams=StringVar()
Spinbox(frame,values=(1,2,3,4,5),textvariable=NumTeams).grid(row=2,column=0,columnspan=5)

frame2=Frame(root)
frame2.pack()
for i in range(int(NumTeams.get())):
    Label(frame2,text=str(i)).grid(row=0,column=i)
    frame2.update()

The above code is an attempt at achieving this, does anyone know a way that this can be done?

Upvotes: 0

Views: 375

Answers (1)

Hobbes
Hobbes

Reputation: 404

You can use the command argument to specify a method to be run whenever your spinbox changes values. I'm not entirely sure what type of columns you mean, but hopefully you can work with this.

from Tkinter import *

def on_spin():
    num_teams.set("New text")

root = Tk()

frame = Frame(root)
frame.pack(anchor=CENTER)

frame2 = Frame(root)
frame2.pack()

num_teams = StringVar()
label = Label(frame2, textvariable=num_teams)
label.pack()

spin = Spinbox(frame, from_=1, to=5, command=on_spin)
spin.pack()

root.mainloop()

Upvotes: 1

Related Questions