Reputation: 13
I'm relatively new to Python and I'm currently working on a GUI Tkinter calculator. My questions are:-
1) I want to add a backspace button which removes the last digit of a number. for example 543 becomes 54. Ive added the button but I don't know how to define a function for it. So if anyone could just tell me where to write the function and what to write.
2)My calculator works fine when i click the GUI buttons but it doesnt work when i type the integer input manually. So if anyone could help me with that.
I would be grateful if anyone could give me a solution to these two issues. Here is the code: (P.S the inendation is messed up cause i copy pasted)
from Tkinter import *
from ttk import *
from ttk import Entry
def keyPress(event):
if event.char in ('1','2','3','4','5','6','7','8','9','0' ):
return True
elif event.keysym not in ('Alt_r', 'Alt_L', 'F4'):
return 'break'
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.number1 = []
self.operand_number = ''
self.parent = parent
self.operand1 = ''
self.result = ''
self.initUI()
def calculation(self, number):
self.number1.append(str(number))
self.operand1 = ''.join(self.number1)
self.entry.delete(0,35)
self.entry.insert(0, self.operand1)
print 'pressed : ' + self.operand1
def initUI(self):
self.parent.title("Calculator")
Style().configure("TButton", padding=(0, 5, 0, 5),
font='serif 10')
self.columnconfigure(0, pad=3)
self.columnconfigure(1, pad=3)
self.columnconfigure(2, pad=3)
self.columnconfigure(3, pad=3)
self.rowconfigure(0, pad=3)
self.rowconfigure(1, pad=3)
self.rowconfigure(2, pad=3)
self.rowconfigure(3, pad=3)
self.rowconfigure(4, pad=3)
self.entry = Entry(self)
self.entry.grid(row=0, columnspan=4, sticky=W+E)
self.entry.bind('<KeyPress>', keyPress)
self.entry.focus()
clear = Button(self, text="Clear", command=self.clear)
clear.grid(row=1, column=0)
bck = Button(self, text="Back")
bck.grid(row=1, column=1, columnspan=2, sticky=N+W+S+E)
clo = Button(self, text="Close", command=self.close_window)
clo.grid(row=1,column=3,)
sev = Button(self, text="7", command =lambda: self.calculation(7))
sev.grid(row=2, column=0)
eig = Button(self, text="8", command =lambda: self.calculation(8))
eig.grid(row=2, column=1)
nin = Button(self, text="9", command =lambda: self.calculation(9))
nin.grid(row=2, column=2)
div = Button(self, text="/", command=lambda: self.operation("/"))
div.grid(row=2, column=3)
fou = Button(self, text="4",command =lambda: self.calculation(4))
fou.grid(row=3, column=0)
fiv = Button(self, text="5",command =lambda: self.calculation(5))
fiv.grid(row=3, column=1)
six = Button(self, text="6", command =lambda: self.calculation(6))
six.grid(row=3, column=2)
mul = Button(self, text="*", command=lambda: self.operation("*"))
mul.grid(row=3, column=3)
one = Button(self, text="1", command =lambda: self.calculation(1))
one.grid(row=4, column=0)
two = Button(self, text="2", command =lambda: self.calculation(2))
two.grid(row=4, column=1)
thr = Button(self, text="3", command =lambda: self.calculation(3))
thr.grid(row=4, column=2)
mns = Button(self, text="-", command=lambda: self.operation("-"))
mns.grid(row=4, column=3)
zer = Button(self, text="0", command =lambda: self.calculation(0))
zer.grid(row=5, column=0)
dot = Button(self, text=".", command =lambda: self.calculation("."))
dot.grid(row=5, column=1)
equ = Button(self, text="=" , command =lambda: self.solution())
equ.grid(row=5, column=2)
pls = Button(self, text="+", command=lambda: self.operation("+"))
pls.grid(row=5, column=3)
self.pack()
def close_window(self):
self.parent.destroy()
def operation(self,symbol):
self.symbol = symbol
self.entry.delete(0,35)
self.operand_number = ''.join(self.number1)
del self.number1[:]
def solution(self):
if(self.symbol == '+'):
self.result = self.addition(float(self.operand1), float(self.operand_number))
if(self.symbol == '-'):
self.result = self.subtraction(float(self.operand1), float(self.operand_number))
if(self.symbol == '*'):
self.result = self.multiplication(float(self.operand1), float(self.operand_number))
if(self.symbol == '/'):
self.result = self.division(float(self.operand1), float(self.operand_number))
self.entry.delete(0,35)
self.entry.insert(0, self.result)
self.operand1 = ''
self.operand_number = ''
print self.result
def addition(self,num1,num2):
return num1 + num2
def subtraction(self,num1,num2):
return num2 - num1
def multiplication(self,num1,num2):
return num1 * num2
def division(self,num1,num2):
return num2 / num1
def clear(self):
self.entry.delete(0,35)
del self.number1[:]
def main():
root = Tk()
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
Thanks in advance
Upvotes: 0
Views: 1118
Reputation: 1384
1.The question 1, please check the back function:
def back(self):
strStr = self.entry.get()
self.entry.delete(0, 35)
self.entry.insert(0, strStr[0: len(strStr) - 1])
print(self.entry.get())
2.The question 2, you need change the return
at keyPress
function.
def keyPress(event):
if event.char in ('1','2','3','4','5','6','7','8','9','0' ):
print(event.char)
elif event.keysym not in ('Alt_r', 'Alt_L', 'F4'):
return 'break'
3.The whole code:
from Tkinter import *
from ttk import *
from ttk import Entry
def keyPress(event):
if event.char in ('1','2','3','4','5','6','7','8','9','0' ):
print(event.char)
elif event.keysym not in ('Alt_r', 'Alt_L', 'F4'):
return 'break'
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.number1 = []
self.operand_number = ''
self.parent = parent
self.operand1 = ''
self.result = ''
self.initUI()
def calculation(self, number):
self.number1.append(str(number))
self.operand1 = ''.join(self.number1)
self.entry.delete(0,35)
self.entry.insert(0, self.operand1)
print 'pressed : ' + self.operand1
def initUI(self):
self.parent.title("Calculator")
Style().configure("TButton", padding=(0, 5, 0, 5),
font='serif 10')
self.columnconfigure(0, pad=3)
self.columnconfigure(1, pad=3)
self.columnconfigure(2, pad=3)
self.columnconfigure(3, pad=3)
self.rowconfigure(0, pad=3)
self.rowconfigure(1, pad=3)
self.rowconfigure(2, pad=3)
self.rowconfigure(3, pad=3)
self.rowconfigure(4, pad=3)
self.entry = Entry(self)
self.entry.grid(row=0, columnspan=4, sticky=W+E)
self.entry.bind('<KeyPress>', keyPress)
self.entry.focus()
clear = Button(self, text="Clear", command=self.clear)
clear.grid(row=1, column=0)
bck = Button(self, text="Back", command=self.back)
bck.grid(row=1, column=1, columnspan=2, sticky=N+W+S+E)
clo = Button(self, text="Close", command=self.close_window)
clo.grid(row=1,column=3,)
sev = Button(self, text="7", command =lambda: self.calculation(7))
sev.grid(row=2, column=0)
eig = Button(self, text="8", command =lambda: self.calculation(8))
eig.grid(row=2, column=1)
nin = Button(self, text="9", command =lambda: self.calculation(9))
nin.grid(row=2, column=2)
div = Button(self, text="/", command=lambda: self.operation("/"))
div.grid(row=2, column=3)
fou = Button(self, text="4",command =lambda: self.calculation(4))
fou.grid(row=3, column=0)
fiv = Button(self, text="5",command =lambda: self.calculation(5))
fiv.grid(row=3, column=1)
six = Button(self, text="6", command =lambda: self.calculation(6))
six.grid(row=3, column=2)
mul = Button(self, text="*", command=lambda: self.operation("*"))
mul.grid(row=3, column=3)
one = Button(self, text="1", command =lambda: self.calculation(1))
one.grid(row=4, column=0)
two = Button(self, text="2", command =lambda: self.calculation(2))
two.grid(row=4, column=1)
thr = Button(self, text="3", command =lambda: self.calculation(3))
thr.grid(row=4, column=2)
mns = Button(self, text="-", command=lambda: self.operation("-"))
mns.grid(row=4, column=3)
zer = Button(self, text="0", command =lambda: self.calculation(0))
zer.grid(row=5, column=0)
dot = Button(self, text=".", command =lambda: self.calculation("."))
dot.grid(row=5, column=1)
equ = Button(self, text="=" , command =lambda: self.solution())
equ.grid(row=5, column=2)
pls = Button(self, text="+", command=lambda: self.operation("+"))
pls.grid(row=5, column=3)
self.pack()
def close_window(self):
self.parent.destroy()
def operation(self,symbol):
self.symbol = symbol
self.entry.delete(0,35)
self.operand_number = ''.join(self.number1)
del self.number1[:]
def solution(self):
if(self.symbol == '+'):
self.result = self.addition(float(self.operand1), float(self.operand_number))
if(self.symbol == '-'):
self.result = self.subtraction(float(self.operand1), float(self.operand_number))
if(self.symbol == '*'):
self.result = self.multiplication(float(self.operand1), float(self.operand_number))
if(self.symbol == '/'):
self.result = self.division(float(self.operand1), float(self.operand_number))
self.entry.delete(0,35)
self.entry.insert(0, self.result)
self.operand1 = ''
self.operand_number = ''
print self.result
def addition(self,num1,num2):
return num1 + num2
def subtraction(self,num1,num2):
return num2 - num1
def multiplication(self,num1,num2):
return num1 * num2
def division(self,num1,num2):
return num2 / num1
def clear(self):
self.entry.delete(0,35)
del self.number1[:]
def back(self):
strStr = self.entry.get()
self.entry.delete(0, 35)
self.entry.insert(0, strStr[0: len(strStr) - 1])
print(self.entry.get())
def main():
root = Tk()
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
Upvotes: 1