当使用列表时,Tkinter按钮只工作一次。

问题描述 投票:0回答:1

我试图使用一个按钮来循环浏览一个列表。它工作了一次,但是对其他的按钮没有反应。

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)

有什么建议吗?

python function tkinter tk
1个回答
2
投票

current 是一个本地变量,你将其初始化为 1 每次调用该函数时。

你需要做两件事。

  • 声明 current 作为全球
  • 正确地递增它(+= 而非 =+)

例如:

def next():
    global current
    current += 1
    ...
© www.soinside.com 2019 - 2024. All rights reserved.