Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't find soulution tkinter KeyError: 0

from tkinter import *
from tkinter import font
window = Tk ()
window.title(" ")
button = {}
name = -1
def click(number):
    button[number].config(bg="gray")
    print(number)
for x in range(15):
    for y in range(15):
        name = name + 1
        button[name] = Button(window, command=click(name), bg="white")
        button[name].config(height=2, width=4)
        button[name].grid(column = x, row = y)

window.mainloop()

I get error line 13, in module and line 8, in click. Not sure why this is happening, before I added the command=click(name) to Button it worked, but after I added it it stopped working

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Adding in command=click(name) doesn’t work as you are trying to assign command to the value returned from click(name) (you are accidentally running the function, which is then causing the KeyError).

Instead, try doing command=lambda: click(name)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading