gpt4 book ai didi

python - 按钮回调怎么写?

转载 作者:行者123 更新时间:2023-11-30 23:39:47 25 4
gpt4 key购买 nike

下面是我用来用按钮制作棋盘的代码。

from Tkinter import *

for x in xrange(8):
for y in xrange(8:
if((x+y)%2 == 0):
Button(root, bg="white", width=11, height=5).grid(row=y, column=x)
else:
Button(root, bg="black", width=11, height=5).grid(row=y, column=x)

我知道如何为单个按钮创建回调函数,但我不确定如何为这 64 个按钮中的每一个按钮实现回调函数,以便在按下时它们将返回其在网格中的位置.

最佳答案

def callback(event):
x,y = event.widget.grid_location()

这个例子应该为您指明正确的方向。

更新:为了澄清 grid_location 的用法,我快速谷歌了一下,发现...... a SO-post ;-)通过更直接地提供您所需的解决方案,这让我感到羞愧way :

grid_info = event.widget.grid_info()
print "row:", grid_info["row"], "column:", grid_info["column"]

所以功劳应该归于 Bryan Oakley ;-),这个问题可能被称为重复问题......

关于python - 按钮回调怎么写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13250835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com