How can I update the text on a label after each entry in Tkinter?

Viewed 24

I currently need some way to refresh the text on a label after each entry in an input field. For example, my current label is "please input name" and a button that says "enter". Once the user inputs their name and clicks enter, I want the label name to change to "please enter age". I would repeat this process 6 times. Do I have to use some sort of loop for this?

1 Answers

How I would do this:

  1. create a list with all your labels.
  2. define a function for the button click that (I) updates a counter and (II) sets the label to the index (the counter) of the list.

something like this:

myLabels = ["This is label 1", "This is label 2"]
btn = Button(root, text="OK", command=onclick)
counter = 0

def onclick():
    counter +=1
    myLabel.config(text=myLabels[counter])
Related