Python 如何创建按钮矩阵并将其放入我的一个框架中

Python How to create a matrix of buttons and put it inside of one of my frames

我有这个代码库,我想用按钮矩阵更改那些 button1、2、3、4 并将其放在我的左框架中,这可能吗?我已经尝试了很多东西,但我帮不上忙

from tkinter import *
import tkinter .messagebox

root = Tk()
root.geometry("1350x750+0+0")
root.title("Tres en raya")
root.configure(background = "#FFFFD1")

Tops = Frame(root, bg = "white", pady = 2, width = 1350, height = 100, relief=RIDGE)
Tops.grid(row = 0, column = 0)

lblTitle = Label(Tops, font = ("Tohama", 50, "bold"), text = "*** TRES EN RAYA ***", bd=21, bg="#b28dff", fg="cornsilk", justify="center")
lblTitle.grid(row = 0, column = 1)

MainFrame = Frame(root, bg = "#C5A3FF", bd = 10, width = 1350, height = 600, relief = RIDGE) #ROSA
MainFrame.grid(row = 1, column = 0)

LeftFrame = Frame (MainFrame, bd=10, width = 560, height = 500, pady = 2, padx = 10, bg = "#85E3FF", relief= RIDGE)
LeftFrame.pack(side = LEFT)

RightFrame = Frame (MainFrame, bd=10, width = 560, height = 500, pady = 10, padx = 2, bg = "#85E3FF", relief= RIDGE)
RightFrame.pack(side = RIGHT)

RightFrame1 = Frame(RightFrame, bd=10, width = 560, height = 200, pady = 10, padx = 2, bg = "#85E3FF", relief= RIDGE)
RightFrame1.grid(row = 0, column = 0)

RightFrame2 = Frame (RightFrame, bd=10, width = 560, height = 200, pady = 10,   padx = 2, bg = "#85E3FF", relief= RIDGE)
RightFrame2.grid(row = 1, column = 0)

playerX = IntVar()
player0 = IntVar()

playerX.set(0)
player0.set(0)

buttons = StringVar()
click = True

def checker(buttons):
    global click
    
    if buttons["text"] == "" and click == False:
        buttons["text"] = "O"
        click = True
        scorekeeper()
        
    elif buttons["text"] == "" and click == True:
        buttons["text"] = "X"
        click = False
        scorekeeper()
    
def scorekeeper():
    global tie
    global button1, button2, button3, button4, button5, button6, button7, button8, button9
    
lblscore = Label(RightFrame1, font = ("tahoma", 30, "bold"),text = "*** TABLA DE PUNTUACIONES", padx = 2, pady = 2)    
lblscore.grid(row = 0, column =1)

lblplayerX = Label(RightFrame2, font = ("tahoma", 25, "bold"), bd = 2, fg="black", textvariable=playerX, width = 14)
lblplayer0 = Label(RightFrame2, font = ("tahoma", 25, "bold"), bd = 2, fg="black", textvariable=player0, width = 14)
lblnombrex = Label(RightFrame2, font = ("tahoma", 25, "bold"),text = "Jugador Uno", bd = 2, fg="black", width = 14)
lblnombre0 = Label(RightFrame2, font = ("tahoma", 25, "bold"),text = "Jugador Dos", bd = 2, fg="black", width = 14)


lblplayerX.grid(row = 3, column = 2, padx = 6, pady = 5)
lblnombrex.grid(row = 3, column = 1, padx = 6, pady = 5)

lblplayer0.grid(row = 4, column = 2, padx = 6, pady = 5)
lblnombre0.grid(row = 4, column = 1, padx = 6, pady = 5)



button1 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button1))


button1.grid(row = 1, column =1)


button2 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button2))
button2.grid(row = 1, column = 2)


button3 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button3))

button3.grid(row = 1, column =3)

button4 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button4))
button4.grid(row = 2, column =1)


button5 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button5))
button5.grid(row = 2, column =2)


button6 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button6))
button6.grid(row = 2, column =3)


button7 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button7))
button7.grid(row = 3, column =1)


button8 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button8))
button8.grid(row = 3, column =2)


button9 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button9))
button9.grid(row = 3, column =3)


root.mainloop()

这是主根的样子[图片][1] 我是新手 python 我看到了制作这个框架的教程,我想使用矩阵而不是单个按钮按钮,因为我需要实现最小最大算法。

我试过了

botones = [[],[],[]]

for i in range(3):
    for j in range(3):
        botones[i].append(None)
        botones[i][j] = Button(LeftFrame, text = "", font=("tahoma", 25, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(botones[i][j])).grid(row = i+1, column = j+1)


但是当我使用这些按钮时,我的命令功能不起作用 [1]: https://i.stack.imgur.com/ToOF4.png

潜在的问题是您在同一行上创建和使用了 grid。试试这个最小的例子,看看你是否可以根据你的需要进行调整。

import tkinter as tk

root = tk.Tk()
LeftFrame = tk.Frame(root)
LeftFrame.grid()

def checker(i,j):
    print(f"You pressed button {i},{j}")

#Create a 2-d list containing 3 rows, 3 columns (using list comprehension)
botones = [[None for i in range(3)] for j in range(3) ]

for i in range(3):
    for j in range(3):
        current_button = tk.Button(LeftFrame,
                               text = f"{i},{j}",
                               font=("tahoma", 25, "bold"),
                               height = 3,
                               width = 8,
                               bg="gainsboro",
                               command=lambda i=i,j=j:checker(i,j)) #lambda is passed parameters i and j
        #Grid occurs on a new line
        current_button.grid(row = i+1, column = j+1)
        botones[i][j] = current_button

root.mainloop()