I'm trying to make a calculator with Tkinter but I'm having trouble using a for loop to make the number buttons. I'm using a for loop as they are nearly identical. Here's the loop:
class Calculation:
def __init__(self,master):
i=1
rowi=1
columni=0
self.num = ""
self.button = []
frame=Frame(master)
frame.grid()
self.returnScreen = Label(frame, background = "White", text="")
self.returnScreen.grid(row=0, column=0,columnspan=3)
for i in range(9):
if columni < 3:
self.button.append(Button(frame, text=str(i), width=5, command=lambda i=i:self.numberPressed(i))
**self**.button[i].grid(row=rowi, column=columni)
rowi = rowi+1
columni = columni+1
else:
columni=0
The problem is highlighted with asterisks, that's where the red highlight goes. What is it trying to point out?
For what I'm trying to do, here's the code for the buttons that I commented out:
#self.button1 = Button(frame, text="1", width=5, command=lambda:self.numberPressed(1))
#self.button1.grid(row=1, column=0)
#self.button2 = Button(frame, text="2", width=5,command=lambda:self.numberPressed(2))
#self.button2.grid(row=1, column=1)
#self.button3 = Button(frame, text="3", width=5,command=lambda:self.numberPressed(3))
#self.button3.grid(row=1, column=2)
#self.button4 = Button(frame, text="4", width=5, command=lambda:self.numberPressed(4))
#self.button4.grid(row=2, column=0)
#self.button5 = Button(frame, text="5", width=5, command=lambda:self.numberPressed(5))
#self.button5.grid(row=2, column=1)
#self.button6 = Button(frame, text="6", width=5, command=lambda:self.numberPressed(6))
#self.button6.grid(row=2, column=2)
#self.button7 = Button(frame, text="7", width=5, command=lambda:self.numberPressed(7))
#self.button7.grid(row=3, column=0)
#self.button8 = Button(frame, text="8", width=5, command=lambda:self.numberPressed(8))
#self.button8.grid(row=3, column=1)
#self.button9 = Button(frame, text="9", width=5, command=lambda:self.numberPressed(9))
#self.button9.grid(row=3, column=2)