[Warning, Ugly Code Ahead...]
I do not quite understand the question, however, I am assuming that you would like to build a calculator application. I have quickly "whipped up" this code. It is compatible with python3, and comes with a graphical user interphase.
Instead of creating that huge function to determine the operator, just use the eval()
function instead in a GUI.
As seen in the function Calculate()
, I am actually getting the text input from the display screen and just evaluating it. I also error check using a try
and except
statement.
Calculator with a GUI:
from tkinter import *
from tkinter import ttk
import time
import random
tk = Tk()
canvas = Canvas(tk, height=400, width=300, bg = 'white')
canvas.pack()
tk.title('Calculator')
tk.resizable(False, False)
class Calculator:
def Title(self):
title = Label(tk, text = 'Calculator', font='Times, 20')
title.place(x = 0, y = 10)
def EntryBar(self):
self.CalculationBox = Text(tk, height=2, width=27, font='Times, 15', bg = 'gray60')
self.CalculationBox.place(x = 0, y = 60)
self.counter = 1
def DisplayCal(self, num):
self.CalculationBox.insert(END, num)
def Calculate(self):
try:
answer = eval(self.CalculationBox.get('1.0', END))
self.CalculationBox.delete("1.0", END)
#print(answer) #For debugging
self.CalculationBox.insert(END, answer)
except Exception:
self.CalculationBox.delete("1.0", END)
self.CalculationBox.insert(END, "Error")
c = Calculator()
class Buttons:
def Buttons(self):
self.ZeroBtn = Button(tk, text = '0', height=2, width=8, bg = 'gray60', fg = 'white', command = lambda: c.DisplayCal(num=0))
self.ZeroBtn.place(x = 120, y = 360)
self.OneBtn = Button(tk, text = '1', height=2, width=8, bg = 'gray60', fg = 'white', command = lambda: c.DisplayCal(num=1))
self.OneBtn.place(x = 53, y = 320)
self.TwoBtn = Button(tk, text = '2', height=2, width=8, bg = 'gray60', fg = 'white', command = lambda: c.DisplayCal(num=2))
self.TwoBtn.place(x = 120, y = 320)
self.ThreeBtn = Button(tk, text = '3', height=2, width=8, bg = 'gray60', fg = 'white', command = lambda: c.DisplayCal(num=3))
self.ThreeBtn.place(x = 187, y = 320)
self.FourBtn = Button(tk, text = '4', height=2, width=8, bg = 'gray60', fg = 'white', command = lambda: c.DisplayCal(num=4))
self.FourBtn.place(x = 53, y = 280)
self.FiveBtn = Button(tk, text = '5', height=2, width=8, bg = 'gray60', fg = 'white', command = lambda: c.DisplayCal(num=5))
self.FiveBtn.place(x = 120, y = 280)
self.SixBtn = Button(tk, text = '6', height=2, width=8, bg = 'gray60', fg = 'white', command = lambda: c.DisplayCal(num=6))
self.SixBtn.place(x = 187, y = 280)
self.SevenBtn = Button(tk, text = '7', height=2, width=8, bg = 'gray60', fg = 'white', command = lambda: c.DisplayCal(num=7))
self.SevenBtn.place(x = 53, y = 240)
self.EightBtn = Button(tk, text = '8', height=2, width=8, bg = 'gray60', fg = 'white', command = lambda: c.DisplayCal(num=8))
self.EightBtn.place(x = 120, y = 240)
self.NineBtn = Button(tk, text = '9', height=2, width=8, bg = 'gray60', fg = 'white', command = lambda: c.DisplayCal(num=9))
self.NineBtn.place(x = 187, y = 240)
self.AddBtn = Button(tk, text = '+', height=2, width=8, bg = 'gray60', fg = 'white', command = lambda: c.DisplayCal(num='+'))
self.AddBtn.place(x = 53, y = 200)
self.MultBtn = Button(tk, text = 'x', height=2, width=8, bg = 'gray60', fg = 'white', command = lambda: c.DisplayCal(num='*'))
self.MultBtn.place(x = 120, y = 200)
self.DivBtn = Button(tk, text = '÷', height=2, width=8, bg = 'gray60', fg = 'white', command = lambda: c.DisplayCal(num='/'))
self.DivBtn.place(x = 187, y = 200)
self.SubBtn = Button(tk, text = '-', height=2, width=8, bg = 'gray60', fg = 'white', command = lambda: c.DisplayCal(num='-'))
self.SubBtn.place(x = 53, y = 160)
self.EquBtn = Button(tk, text = '=', height=2, width=8, bg = 'gray60', fg = 'white', command = c.Calculate)
self.EquBtn.place(x = 187, y = 160)
self.ClsBtn = Button(tk, text='Clear', height=2, width=8, bg = 'gray60', fg = 'white', command = lambda: c.CalculationBox.delete('1.0',END))
self.ClsBtn.place(x = 120, y = 160)
b = Buttons()
def main():
c.Title()
c.EntryBar()
b.Buttons()
main()
tk.mainloop()