[Python] How to edit an element inside of a tkinter Grid?

Discussão em 'Python' iniciado por Stack, Junho 22, 2025.

  1. Stack

    Stack Membro Participativo

    • I'm working on a project to design a chessboard.

    I have successfully created the Board, my goal currently is to be able to edit the board.

    • my original idea is to use buttons with the Tkinter grid to make the moves work.

    I decided to make the board using:

    window.geometry("800x500")
    window.minsize(800, 500) #Locking the size of the Display
    window.maxsize(800, 500)

    #Adding the board
    SeenBoard = tk.Frame(window, width=300, height=300,padx=5,pady=5) #Adds padding to the grid
    SeenBoard.pack(side=tk.LEFT) #the left isnt needed yet, but will be later

    for row in range(8): # 8 Rows along
    for column in range(8): #8 Columns down
    tk.Button(SeenBoard,text=f"{row},{column}",width=5,height=3).grid(row=row, column=column) #Creates each button

    window.mainloop() #Idk what this really does? it just kinda works.


    My problem is that with this way of making the board, I cannot edit the buttons, nor locate them.

    • I wish to be able to change the colour of each button, to alternate between green and white, for that grid pattern feel.
    • In order to do this I only can think of two ways at least, and that would be to somehow give each of the buttons an identifier beforehand, such as labelling them all as Grid1_1.button(...)
    • My problem with doing it manually is that I want to keep the code concise and not repetitive, so anyway in which I could add lots of them at once would solve my problem.

    Second solution to my knowledge:

    • somehow having the ability with tkinter to select a specific grid slot, such as 2,2. and then trying to modify it with that, I heard about grid slaves, but I'm too small brained to understand how it works.

    My request if possible:

    • if possible I would like to know the solution or even the concept behind trying to alternate the colour of the board in a pattern. any method
    • any solution that would allow me to access a button, and change its text/colour.
    • there is also the high chance I'm not being smart, I've worded something wrong or misunderstood it, any guidance would be extremely appreciated!

    Continue reading...

Compartilhe esta Página