Python Tkinter按钮在使用列表时仅起作用一次
我试图用一个按钮在列表中循环。它只工作一次,但对任何其他压力都没有反应Python Tkinter按钮在使用列表时仅起作用一次,python,function,tkinter,tk,Python,Function,Tkinter,Tk,我试图用一个按钮在列表中循环。它只工作一次,但对任何其他压力都没有反应 cards = ["2 of Diamonds", "3 of Diamonds"] #etc (don't want it to be too long) current=0 def next(): 电流=+1 打印(“当前”变量值:,当前) config(text=cards[当前]) 下一步=按钮(文本=”⇛", command=next,fg=“白色”,bg=“红色”,activebackground=“#8b00
cards = ["2 of Diamonds", "3 of Diamonds"] #etc (don't want it to be too long)
current=0
def next():
电流=+1
打印(“当前”变量值:,当前)
config(text=cards[当前])
下一步=按钮(文本=”⇛", command=next,fg=“白色”,bg=“红色”,activebackground=“#8b0000”,activeforeground=“白色”,relief=GROOVE)。网格(列=2,行=1)
有什么建议吗?
current
是一个局部变量,每次调用函数时,您都会将其初始化为1
你需要做两件事:
- 将当前<代码>声明为全局
- 正确递增(
而不是+=
)=+
def next():
global current
current += 1
...
通读