Search code examples
pythonpython-3.xtkintergrid-layout

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


I have this code base, I would like to change those button1,2,3,4 with a matrix of buttons and put it in my leftframe, it is that posible? I have tried many things but I can't please help

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()

this is how looks the main root [image][1] I am new in python I saw a tutorial to make this frame, I want to use a matrix instead of single buttons buttons because I need to implement min max algorithm.

I have tried

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)


but When I use those buttons my command function doesnt work [1]: https://i.sstatic.net/ToOF4.png


Solution

  • Potential problem is that you have created and used grid on the same line. Try this minimal example to see if you can adapt it to your needs.

    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()