I am working on a Python GUI Calculator, and after trying a bunch of times, for some reason multiplication and division do not work for some reason. I revisited the tutorial a dozen times and even asked a friend but they do not know what is going on. I would really be grateful for any help.
This has been a little frustrating for a while.
from tkinter import *
root = Tk()
root.title("Calculator")
box = Entry(root, width=40, borderwidth=6)
box.grid(row=0, column=0, columnspan=3, padx=15, pady=15)
def buttonClick(number):
current = box.get()
box.delete(0, END)
box.insert(0, current str(number))
def buttonClear():
box.delete(0, END)
def buttonAdd():
firstNumber = box.get()
global numOne
global math
math = "addition"
numOne = int(firstNumber)
box.delete(0, END)
def buttonEqual():
secondNumber = box.get()
box.delete(0, END)
if math == "addition":
box.insert(0, numOne int(secondNumber))
if math == "subtraction":
box.insert(0, numOne - int(secondNumber))
if math == "multiplication":
box.insert(0, numOne * int(secondNumber))
if math == "division":
box.insert(0, numOne / int(secondNumber))
def buttonSubtract():
firstNumber = box.get()
global numOne
global math
math = "subtraction"
numOne = int(firstNumber)
box.delete(0, END)
def buttonMultiply():
firstNumber = box.get()
global numOne
global math
math = "multiplication"
numOne = int(firstNumber)
box.delete(0, END)
def buttonDivide():
firstNumber = box.get()
global numOne
global math
math = "division"
numOne = int(firstNumber)
box.delete(0, END)
Button1 = Button(root,
text="1",
padx=37,
pady=22,
command=lambda: buttonClick(1))
Button2 = Button(root,
text="2",
padx=37,
pady=22,
command=lambda: buttonClick(2))
Button3 = Button(root,
text="3",
padx=37,
pady=22,
command=lambda: buttonClick(3))
Button4 = Button(root,
text="4",
padx=37,
pady=22,
command=lambda: buttonClick(4))
Button5 = Button(root,
text="5",
padx=37,
pady=22,
command=lambda: buttonClick(5))
Button6 = Button(root,
text="6",
padx=37,
pady=22,
command=lambda: buttonClick(6))
Button7 = Button(root,
text="7",
padx=37,
pady=22,
command=lambda: buttonClick(7))
Button8 = Button(root,
text="8",
padx=37,
pady=22,
command=lambda: buttonClick(8))
Button9 = Button(root,
text="9",
padx=37,
pady=22,
command=lambda: buttonClick(9))
Button0 = Button(root,
text="0",
padx=37,
pady=22,
command=lambda: buttonClick(0))
ButtonAdd = Button(root, text=" ", padx=45, pady=22, command=buttonAdd)
ButtonEqual = Button(root, text="=", padx=45, pady=22, command=buttonEqual)
ButtonClear = Button(root,
text="Clear",
padx=37,
pady=22,
command=lambda: buttonClear())
ButtonSubtract = Button(root,
text="-",
padx=45,
pady=22,
command=buttonSubtract)
ButtonMultiply = Button(root,
text="*",
padx=45,
pady=22,
command=buttonMultiply)
ButtonDivide = Button(root, text="/", padx=45, pady=22, command=buttonDivide)
Button1.grid(row=3, column=0)
Button2.grid(row=3, column=1)
Button3.grid(row=3, column=2)
Button4.grid(row=2, column=0)
Button5.grid(row=2, column=1)
Button6.grid(row=2, column=2)
Button7.grid(row=1, column=0)
Button8.grid(row=1, column=1)
Button9.grid(row=1, column=2)
Button0.grid(row=4, column=1)
ButtonAdd.grid(row=1, column=4)
ButtonEqual.grid(row=4, column=2)
ButtonClear.grid(row=4, column=0)
ButtonSubtract.grid(row=2, column=4)
ButtonMultiply.grid(row=4, column=4)
ButtonDivide.grid(row=3, column=4)
root.mainloop()
CodePudding user response:
When I tried it the first time it did not work for multiplication. After I fixed the indentation for the if statements it worked. Try that.
CodePudding user response:
You need to correct the part of if loop on buttonEqual function so that every statement get a test separately, Here is my solution:
def buttonEqual():
secondNumber = box.get()
box.delete(0, END)
if math == "addition":
box.insert(0, numOne int(secondNumber))
if math == "subtraction":
box.insert(0, numOne - int(secondNumber))
if math == "multiplication":
box.insert(0, numOne*int(secondNumber))
if math == "division":
box.insert(0, numOne/int(secondNumber))
CodePudding user response:
Your layout was annoying me, so thought I'd share this. I get the screenWidth then position the buttons screenWidth / 4. Also added the layout in order at the bottom.
from tkinter import *
root = Tk()
root.title("Calculator")
box = Entry(root, width=40, borderwidth=10)
box.grid(row=0, column=0, columnspan=4, padx=0, pady=0)
root.update()
width = box.winfo_width()
height = box.winfo_height()
def buttonClick(number):
current = box.get()
box.delete(0, END)
box.insert(0, str(current) str(number))
def buttonClear():
box.delete(0, END)
def buttonAdd():
firstNumber = box.get()
global numOne
global math
math = "addition"
numOne = int(firstNumber)
box.delete(0, END)
def buttonEqual():
secondNumber = box.get()
box.delete(0, END)
if math == "addition":
box.insert(0, numOne int(secondNumber))
if math == "subtraction":
box.insert(0, numOne - int(secondNumber))
if math == "multiplication":
print(numOne * int(secondNumber))
box.insert(0, numOne * int(secondNumber))
if math == "division":
box.insert(0, numOne / int(secondNumber))
def buttonSubtract():
firstNumber = box.get()
global numOne
global math
math = "subtraction"
numOne = int(firstNumber)
box.delete(0, END)
def buttonMultiply():
firstNumber = box.get()
global numOne
global math
math = "multiplication"
numOne = int(firstNumber)
box.delete(0, END)
def buttonDivide():
firstNumber = box.get()
global numOne
global math
math = "division"
numOne = int(firstNumber)
box.delete(0, END)
Button1 = Button(root,
text="1",
padx=width / 4,
pady=22,
command=lambda: buttonClick(1))
Button2 = Button(root,
text="2",
padx=width / 4,
pady=22,
command=lambda: buttonClick(2))
Button3 = Button(root,
text="3",
padx=width / 4,
pady=22,
command=lambda: buttonClick(3))
Button4 = Button(root,
text="4",
padx=width / 4,
pady=22,
command=lambda: buttonClick(4))
Button5 = Button(root,
text="5",
padx=width / 4,
pady=22,
command=lambda: buttonClick(5))
Button6 = Button(root,
text="6",
padx=width / 4,
pady=22,
command=lambda: buttonClick(6))
Button7 = Button(root,
text="7",
padx=width / 4,
pady=22,
command=lambda: buttonClick(7))
Button8 = Button(root,
text="8",
padx=width / 4,
pady=22,
command=lambda: buttonClick(8))
Button9 = Button(root,
text="9",
padx=width / 4,
pady=22,
command=lambda: buttonClick(9))
Button0 = Button(root,
text="0",
padx=width / 4,
pady=22,
command=lambda: buttonClick(0))
ButtonAdd = Button(root, text=" ", padx=width / 4, pady=22, command=buttonAdd)
ButtonSubtract = Button(root,
text="-",
padx=width / 4,
pady=22,
command=buttonSubtract)
ButtonMultiply = Button(root,
text="*",
padx=width / 4,
pady=22,
command=buttonMultiply)
ButtonDivide = Button(root, text="/", padx=width / 4, pady=22, command=buttonDivide)
ButtonEqual = Button(root, text="=", padx=width / 4, pady=22, command=buttonEqual)
ButtonClear = Button(root,
text="C",
padx=width / 4,
pady=22,
command=lambda: buttonClear())
Button7.grid(row=1, column=0)
Button8.grid(row=1, column=1)
Button9.grid(row=1, column=2)
ButtonAdd.grid(row=1, column=3)
Button4.grid(row=2, column=0)
Button5.grid(row=2, column=1)
Button6.grid(row=2, column=2)
ButtonSubtract.grid(row=2, column=3)
Button1.grid(row=3, column=0)
Button2.grid(row=3, column=1)
Button3.grid(row=3, column=2)
ButtonDivide.grid(row=3, column=3)
ButtonClear.grid(row=4, column=0)
Button0.grid(row=4, column=1)
ButtonEqual.grid(row=4, column=2)
ButtonMultiply.grid(row=4, column=3)
root.mainloop()