I am making a program using dictionaries to store buttons in tkinter. However, an error comes up when i run it:
KeyError: 'Science, sheet'
On this line:
btn[a].grid_forget()
I do not understand this error though since I have printed the dictionary and this key does exist. Here is my code:
import tkinter
Homework = {"English, essay": "10/11/17", "Science, sheet": "11/12/17"}
def Done(a):
lbl[a].grid_forget()
btn[a].grid_forget()
def MakeGui():
global lbl, btn
window = tkinter.Tk()
window.title("Homework")
window.geometry("800x400")
Counter = 0
btn = {}
lbl = {}
for item in Homework:
Counter += 1
lbl[item] = tkinter.Label(window, text=(item + " " + Homework[item]))
lbl[item].grid(row=Counter, column=0)
btn[item] = tkinter.Button(window, text=("Done"), command=Done(item))
btn[item].grid(row=Counter, column=5)
print (btn, lbl)
window.mainloop()
MakeGui()
The print of btn and lbl results in:
{'Science, sheet': <tkinter.Button object at 0x02E5F0D0>} {'Science, sheet': <tkinter.Label object at 0x02E59DD0>}
{'Science, sheet': <tkinter.Button object at 0x02E5F0D0>, 'English, essay': <tkinter.Button object at 0x02E97670>} {'Science, sheet': <tkinter.Label object at 0x02E59DD0>, 'English, essay': <tkinter.Label object at 0x02E66F50>}
This may be me being stupid but to me it looks like the dictionaries do contain the keys 'Science, sheet'.
My goal for this program is when you click one of the "Done" buttons both it and the label to the left of it disappear. Any help is appreciated.