Tkinter 按钮仅在使用列表时有效一次

Tkinter button only works once when using a list

我正在尝试使用按钮循环浏览列表。它工作一次,但之后不会响应任何其他印刷机。

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)

有什么建议吗?

current 是一个局部变量,每次调用函数时都会初始化为 1

你需要做两件事:

  • 声明 current 为全局
  • 正确增加它(+= 而不是 =+

示例:

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