lukesfridge
lukesfridge

Reputation: 23

Tkinter button only works once when using a list

I am trying to use a button to cycle through a list. It works once, but then doesn't respond to any other presses.

cards = ["2 of Diamonds", "3 of Diamonds"] #etc (don't want it to be too long)
current = 0
def next():
   current=+1
   print("\"current\" variable value: ", current)
   card.config(text=cards[current])
next = Button(text="⇛", command=next, fg="White", bg="Red", activebackground="#8b0000", activeforeground="White", relief=GROOVE).grid(column=2, row=1)

Any suggestions?

Upvotes: 1

Views: 95

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385940

current is a local variable that you initialize to 1 each time the function is called.

You need to do two things:

  • declare current as global
  • increment it correctly (+= rather than =+)

Example:

def next():
    global current
    current += 1
    ...

Upvotes: 2

Related Questions