python - Python Tkinter:如何从 tkinter 的“.bind”中的函数执行调用者的方法
问题描述
我正在使用 Tkinter 制作以下 GUI:
这些数字在 1 到 999 之间是随机的,没有重复。用户需要从最低值到最高值点击按钮。
在这种情况下,顺序是 39>41>242>272>278>337>846>880。
如果用户单击 39,它将被禁用。如果他一开始的时候不是 39,什么都不会发生。39之后,他可以通过点击禁用41等等......
现在我需要在单击按钮时调用一个函数(click_button)。我只想为所有 8 个按钮使用一个功能,但无法弄清楚如何使该功能适用于所有按钮。
import tkinter as tk
import random
window = tk.Tk()
window.title("Clicker")
items = range(1, 1000)
amount = 8
numbers = random.sample(items, k=amount)
click_sequence = sorted(range(amount), key=numbers.__getitem__)
active_button = tk.IntVar()
button0 = tk.Button(window, text=numbers[0], height=4, width=10,
command=lambda: active_button.set(0))
button1 = tk.Button(window, text=numbers[1], height=4, width=10,
command=lambda: active_button.set(1))
button2 = tk.Button(window, text=numbers[2], height=4, width=10,
command=lambda: active_button.set(2))
button3 = tk.Button(window, text=numbers[3], height=4, width=10,
command=lambda: active_button.set(3))
button4 = tk.Button(window, text=numbers[4], height=4, width=10,
command=lambda: active_button.set(4))
button5 = tk.Button(window, text=numbers[5], height=4, width=10,
command=lambda: active_button.set(5))
button6 = tk.Button(window, text=numbers[6], height=4, width=10,
command=lambda: active_button.set(6))
button7 = tk.Button(window, text=numbers[7], height=4, width=10,
command=lambda: active_button.set(7))
#command=lambda: print(active_button.get()))
def click_button(*args):
if active_button.get() == click_sequence[0]:
click_sequence.pop(0)
button0.configure(state="disabled") # Line A
button0.bind("<Button-1>", click_button)
button1.bind("<Button-1>", click_button)
button2.bind("<Button-1>", click_button)
button3.bind("<Button-1>", click_button)
button4.bind("<Button-1>", click_button)
button5.bind("<Button-1>", click_button)
button6.bind("<Button-1>", click_button)
button7.bind("<Button-1>", click_button)
button0.grid(column=0, row=0)
button1.grid(column=1, row=0)
button2.grid(column=2, row=0)
button3.grid(column=3, row=0)
button4.grid(column=0, row=1)
button5.grid(column=1, row=1)
button6.grid(column=2, row=1)
button7.grid(column=3, row=1)
window.mainloop()
从“A 行”可以看出,该功能仅适用于 button0。
我怎么能调用调用函数“click_button”的按钮并禁用它的状态?
解决方案
你可以试试以下
像这样修改按钮的命令(您不需要使用IntVar
or bind
)
button0 = tk.Button(window, text=numbers[0], height=4, width=10,
command=lambda: click_button(0))
button1 = tk.Button(window, text=numbers[1], height=4, width=10,
command=lambda: click_button(1))
...
将按钮存储在列表中
buttons=[button0,button1,button2,button3,button4,button5,button6,button7]
和功能click_button
def click_button(btn):
if btn==click_sequence[0]:
click_sequence.pop(0)
buttons[btn].configure(state="disabled")
推荐阅读
- scheduled-tasks - Windows 任务计划程序如何识别任务是否失败
- java - 应该由警报触发的广播接收器保持未触发
- angular - 在异步函数之后运行函数
- python - 在 plotly 中更改分组条形图的组内顺序
- android - 我们可以将 Flutter 集成到原生 iOS 或原生 Android 上吗?
- javascript - 如何将一串大数转换为整数?
- machine-learning - 关于构建贝叶斯网络的问题
- excel - 为用户选择的范围创建循环
- java - 如何检查 Android Studio 中是否显示了 toast?
- android - 在 Imageview 上使用动画时应用程序没有响应