[Python] How to make my buttons from tkinter work repeatedly?

Discussão em 'Python' iniciado por Stack, Maio 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to make a grid for simulation of Langton's Ant. I want to have a grid, which I can modify before the start of the simulation, but I can't make buttons which use a function again after clicking.

    from tkinter import *

    window = Tk()
    frame = Frame(window).grid()
    files = []
    btn=[]
    status=[]


    def btn_color(status):
    if status==1:
    return("black")
    if status==0:
    return("white")


    def click_update(status,btn):
    return(lambda: click_update1(status,btn))


    def click_update1(status,btn):
    status=abs(status-1)
    if status==1:
    btn.config(bg="black")
    if status==0:
    btn.config(bg="white")
    for i in range(2500):
    files.append("button"+str(1))
    for i in range(len(files)):
    status.append(int(0))
    btn.append(Button(frame,text="k", font=("Arial",3),width=2,height=2,repeatdelay=100,repeatinterval=100))
    btn.grid(row=int(i//50),column=int(i%50),sticky="we")
    for i in range(len(files)):
    btn.config(bg=btn_color(status), command=click_update(status,btn))
    window.mainloop()

    Continue reading...

Compartilhe esta Página