【问题标题】:Tkinter button only works once when using a list使用列表时,Tkinter 按钮仅工作一次
【发布时间】:2020-08-19 02:50:00
【问题描述】:

我正在尝试使用一个按钮来循环浏览列表。它只工作一次,但不会响应任何其他压力。

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】:

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

你需要做两件事:

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

例子:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 2018-11-22
    相关资源
    最近更新 更多