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)
``````

## All 5 Replies

We can not know as we have not even the error message you get and can not run the code as it is not runnable. But as it is only syntax error in previous line, it is easy to see by just trying to check the syntax. One ) is missing

The number in Button statement seems to be rowi*2+columni except it is one less if row is 3. So I would use just double for loop for rowi and columni.

I've changed the loop to a nested for loop:

``````from math import *
from Tkinter import * #imports module

class Calculation:
def __init__(self,master):
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(10):
for r in range(4):
for c in range(4):
self.button.append(Button(frame, text=str(i), width=5, command=lambda i=i:self.numberPressed(i)))
self.button[i].grid(row=r, column=c)

def numberPressed(self,number):
if self.num == "":
numb=str(number)
self.returnScreen["text"]=numb
self.num = numb
print self.num
else:
numb=str(self.num)+str(number)
self.returnScreen["text"]=numb
self.num = numb
print self.num

calc = Tk() #Makes 'calc the root window

calculator = Calculation(calc)

calc.mainloop()
``````

Here's the part of the code that makes the above snippet run. When it runs, all that shows up is a single button with 0 as the number assigned. I only want the numbers 1-9. How would I fix my code to make this loop work? To see how it should work, replace the loop with:

``````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)
``````

Just a few changes will do ...

``````from math import *
from Tkinter import * #imports module

class Calculation:
def __init__(self,master):
self.num = ""
self.button = [None]  # element at index 0 is None

frame=Frame(master)
frame.grid()

self.returnScreen = Label(frame, background = "White", text="")
self.returnScreen.grid(row=0, column=0,columnspan=3)

r = 1
c = 0
for i in range(1, 10):
self.button.append(Button(frame, text=str(i), width=5, command=lambda i=i:self.numberPressed(i)))
self.button[i].grid(row=r, column=c)
c += 1
if c > 2:
r += 1
c = 0

def numberPressed(self,number):
if self.num == "":
numb=str(number)
self.returnScreen["text"]=numb
self.num = numb
print self.num
else:
numb=str(self.num)+str(number)
self.returnScreen["text"]=numb
self.num = numb
print self.num

calc = Tk() #Makes 'calc the root window

calculator = Calculation(calc)

calc.mainloop()
``````

Your original problem was a missing closing parens in this statement

``````self.button.append(Button(frame, text=str(i), width=5, command=lambda i=i:self.numberPressed(i))
``````

In the future, remember to check the statement before the one indicated for syntax errors as it happens a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.