Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 关于tkinter/按钮和矩阵绑定的简单匹配游戏_Python_User Interface_Matrix_Tkinter - Fatal编程技术网

Python 关于tkinter/按钮和矩阵绑定的简单匹配游戏

Python 关于tkinter/按钮和矩阵绑定的简单匹配游戏,python,user-interface,matrix,tkinter,Python,User Interface,Matrix,Tkinter,我目前正在使用tkinter制作一个匹配/记忆游戏 “播放屏幕”只是一个按钮矩阵。我想做的是给他们一个命令,显示隐藏的图像并禁用按钮,如果下一个按下的按钮是匹配的,我需要它显然保持这样 问题是我不知道怎么做,因为我甚至不知道如何访问代码上的按钮。我的意思是,我创建了它们,但是现在我如何输入每个特定的按钮,给它一个图像(必须是随机的,因为游戏不能一直都是一样的),然后给它留下或不留下的命令 这可能是一个noob问题,但我刚刚进入矩阵世界,还有tkinter 这是我到目前为止所做的 from tki

我目前正在使用tkinter制作一个匹配/记忆游戏

“播放屏幕”只是一个按钮矩阵。我想做的是给他们一个命令,显示隐藏的图像并禁用按钮,如果下一个按下的按钮是匹配的,我需要它显然保持这样

问题是我不知道怎么做,因为我甚至不知道如何访问代码上的按钮。我的意思是,我创建了它们,但是现在我如何输入每个特定的按钮,给它一个图像(必须是随机的,因为游戏不能一直都是一样的),然后给它留下或不留下的命令

这可能是一个noob问题,但我刚刚进入矩阵世界,还有tkinter

这是我到目前为止所做的

from tkinter import *


def VentanaPlay():
    matriz = [[0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0]]

    ventana = Tk()
    ventana.title('Ejemplo')
     
    longitud_i = len(matriz)
    longitud_j = len(matriz[0])

    def creaMatriz(i = 0, j = 0):
        if i == longitud_i and j == longitud_j:
            print('Listo')
        elif j < longitud_j:
            boton = Button(ventana, width = 10, height = 5)
            boton.grid(row = i, column = j)
            return creaMatriz(i, j + 1)
        else:
            return creaMatriz(i + 1, 0)

    creaMatriz()

    ventana.mainloop()
            
VentanaPlay()
从tkinter导入*
def VentanaPlay():
matriz=[[0,0,0,0,0,0,0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]
文塔纳=Tk()
ventana.title(‘Ejempo’)
longitud_i=len(矩阵)
longitud_j=len(矩阵[0])
def creaMatriz(i=0,j=0):
如果i==longitud_i和j==longitud_j:
打印('Listo')
elif j

所以我需要知道如何访问矩阵的按钮?

您必须将按钮添加到列表(或矩阵)中

比如说

self.all_buttons = []

# ...

boton = Button( ... )
self.all_buttons.append( boton )
self.all_buttons[y*longitud_j + x].grid( ... )
然后您可以访问位于位置(x,y)的按钮

比如说

self.all_buttons = []

# ...

boton = Button( ... )
self.all_buttons.append( boton )
self.all_buttons[y*longitud_j + x].grid( ... )

编辑:

顺便说一句:您可以使用两个
for
循环,而不是递归来创建按钮


编辑:

完整示例(我更喜欢对象编程,所以我使用了
class
):

单击任意按钮可将颜色更改为红色,再次单击可将颜色更改为绿色

from tkinter import *

class VentanaPlay():

    def __init__(self):

        self.matriz = [
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0]
        ]

        self.all_buttons = []

        self.ventana = Tk()
        self.ventana.title('Ejemplo')

        #self.long_x = len(self.matriz)

        #self.long_y = len(self.matriz[0])

        self.creaMatriz()

    def run(self):
        self.ventana.mainloop()


    def creaMatriz(self):
        for y, row in enumerate(self.matriz):
            buttons_row = []
            for x, element in enumerate(row):
                boton = Button(self.ventana, width=10, height=5, command=lambda a=x,b=y: self.onButtonPressed(a,b))
                boton.grid(row=y, column=x)
                buttons_row.append( boton )
            self.all_buttons.append( buttons_row )

    def onButtonPressed(self, x, y):
        print( "pressed: x=%s y=%s" % (x, y) )
        if self.all_buttons[y][x]['bg'] == 'red':
            self.all_buttons[y][x]['bg'] = 'green'
        else:
            self.all_buttons[y][x]['bg'] = 'red'

VentanaPlay().run()