gpt4 book ai didi

python - 如何将多个鼠标按钮绑定(bind)到一个小部件?

转载 作者:行者123 更新时间:2023-11-30 22:51:49 25 4
gpt4 key购买 nike

我正在尝试制作扫雷游戏。对于每个未区分的方 block ,我创建了一个按钮。

my_list = [[0 for i in range(9)] for j in range(9)]

all_buttons = []


def creaMatriz():
for y, row in enumerate(my_list):
buttons_row = []
for x, element in enumerate(row):
boton2 = Button(root, text="", width=6, height=3, command=lambda a=x, b=y: onButtonPressed(a, b))
boton2.grid(row=y, column=x)
buttons_row.append(boton2)
all_buttons.append(buttons_row)


def onButtonPressed(x, y):
all_buttons[y][x]['text'] = str(qwer[x][y]) # Some action!!!
....

当我在一个无差别的正方形上按下鼠标左键时,我调用函数onButtonPressed(x, y),并且一个数字或一个地雷出现在该正方形上。

在未区分的正方形上按鼠标右键时如何调用另一个函数。我想在广场上看到“M”。

完整代码:http://pastebin.com/cWGS4fBp

最佳答案

您需要绑定(bind)您想要的键才能获得此功能。这是一个简单的概念:

from tkinter import *

root = Tk()

def left(event):
label.config(text="Left clicked")

def right(event):
label.config(text="Right clicked")

label = Label(root, text="Nothing")
label.pack()

label.focus_set()
label.bind("<1>", left)
label.bind("<3>", right)

请告诉我们这是否是您正在寻找的内容。

关于python - 如何将多个鼠标按钮绑定(bind)到一个小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38879041/

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