python - 使用列表时,Tkinter 按钮只能工作一次
问题描述
我正在尝试使用按钮循环浏览列表。它工作一次,但不会响应任何其他压力。
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
...
推荐阅读
- laravel-nova - 如何更改默认 Nova Dropdown Dash (—)
- powerbi - 如何根据 DAX 中其他表的最小日期创建表?
- azure-cli - 如何传递带空格的标签
- ios - 将数字线移动到另一个视图
- azure-devops - 为不同项目的所有用户授予对项目的访问权限
- reactjs - 使用 Jest Mock API 调用 SetState
- javascript - Google Apps 脚本 JQuery 表单
- python - 如何从包含的对象调用容器类方法?
- c++ - Cpp 文件说 [converted] 并且是一堆随机字符
- ruby-on-rails - 在开发日志中静音资产请求(config.assets.quiet = true 不起作用)