I am trying to make a calculator using Tkinter in Python. I use a for loop to draw the buttons in, and I'm trying to use lambda functions so the buttons' actions are only called when pressed instead of calling right when the program starts. When I attempt this, however, a "list index out of range" error is thrown.
This is the relevant section which draws the buttons and sets their processes:
# button layout
# ( ) AC **
# 7 8 9 /
# 4 5 6 *
# 1 2 3 -
# 0 . = +
# list that holds the buttons' file names in order
imgOrder = ["lpr", "rpr", "clr", "pow",
"7", "8", "9", "div",
"4", "5", "6", "mul",
"1", "2", "3", "sub",
"0", "dot", "eql", "add"];
# list containing the actual values assigned to buttons in order
charOrder = ["(", ")", "AC", "**",
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"];
imgIndex = 0;
for rowi in range(1, 6):
for coli in range(4):
# fetch and store the img
img = PhotoImage(file="images/{}.gif".format(imgOrder[imgIndex]));
# create button
button = Button(self, bg="white", image=img,
borderwidth=0, highlightthickness=0, activebackground="white",
command=lambda: self.process(charOrder[imgIndex]));
# set button image
button.image = img;
# put the button in its proper row and column
button.grid(row=rowi, column=coli, sticky=N+S+E+W);
imgIndex += 1;
# pack the GUI
self.pack(fill=BOTH, expand=1);
# processes button presses
def process(self, button):
print("Button {button} pressed!");
# AC clears the display
if (button == "AC"):
# clear
self.display["text"] = "";
# otherwise, the number/operator can be tacked on the end
else:
self.display["text"] += button;
Specifically the line that is throwing the error is the line with the lambda function, but I don't know of any way to only make the buttons register on click, short of writing out this block for every button individually.