Modify/Fix an Existing Program

Introduction

Sometimes you must modify/fix someone else's code. Sometimes you find code on the internet which is close to a solution for a problem you are working on. Sometimes you just want to tinker.

This is such a problem.

The Existing code is a GUI (uses Tk) and contains a calculator. Your task, should you accept it, is to modify/fix the existing code. Remove the calculator into its own program. Then make it better.

Note:

Project Description

Part A: Move existing calculator code into its own program
Part B: Modify existing codefixed code
Part C: Next modify fixed codeimproved code
Part D: Improved error reporting
(a second text display for error messages?)
Part E: Add the "%" (modulus) operator
Part F: Add the "//" (floor division) operator
Part G: Add the capability to enter binary, octal, decimal, and hexadecimal numbers
For example: (binary, 0b011) (octal, 0o056) (decimal, 123) (hexadecimal, 0x1a3)
See below for examples of binary, octal, decimal, hexadecimal
Extra buttons for binary/octal/decimal/hexadecimal input modes?
Disable input buttons based on the mode?

fixed code
image missing
improved code
image missing

Examples of Binary, Octal, Decimal, and Hexadecimal Numbers (as Strings)

xyz> python3
>>> 33 + 0b011             <--- binary
36
>>> 33 + 0o011             <--- octal
42
>>> 33 + 011               <--- decimal
  File "<stdin>", line 1
    33 + 011
           ^
SyntaxError: invalid token
>>> 33 + 11                <--- decimal
44
>>> 33 + 0xa3              <--- hexadecimal
196
>>>quit()

Existing Code

#! /usr/bin/python3
# ===================================================================
# Calculator Code To Modify/Fix
# ===================================================================

from tkinter import *          # Python 3
import random
import time;

root = Tk()
root.geometry("1600x800+0+0")
root.title("Restaurant Management System")

text_Input = StringVar()
operator = ''

Tops = Frame(root,width=1600,height=50,bg='powder Blue',relief=SUNKEN)
Tops.pack(side=TOP)

f1 = Frame(root,width=800,height=700,bg='powder Blue',relief=SUNKEN)
f1.pack(side=LEFT)

f2 = Frame(root,width=300,height=700,bg='powder Blue',relief=SUNKEN)
f2.pack(side=RIGHT)
# ==========Time=====================================================
localtime=time.asctime(time.localtime(time.time()))
# ==========Info=====================================================
lbInfo = Label(Tops,font=('arial',50,'bold'),
               text='Restauraunt Management System',
               fg='steel Blue',bd=10,anchor='w')
lbInfo.grid(row=0,column=0)
lbInfo = Label(Tops,font=('arial',20,'bold'),text=localtime,
               fg='steel Blue',bd=10,anchor='w')
lbInfo.grid(row=1,column=0)
# ==========Calculator===============================================
def btnClick(numbers):
    global operator
    operator = operator + str(numbers)
    text_Input.set(operator)
     
def btnClearDisplay():
    global operator 
    operator=''
    text_Input.set('')

def btnEqualsInput():
    global operator
    sumup = str(eval(operator))
    text_Input.set(sumup)
    operator=''       

txtDisplay = Entry(f2,font=('arial',20,'bold'), textvariable=text_Input,
                    bd=30,insertwidth=4,bg="powder blue",justify='right')
txtDisplay.grid(columnspan=4)

btn7=Button(f2,padx=16,pady=16,bd=8,fg='black',font=('arial',20,'bold'),
            text='7',bg='powder blue',command=lambda: btnClick(7)).grid(row=2,column=0)

btn8=Button(f2,padx=16,pady=16,bd=8,fg='black',font=('arial',20,'bold'),
            text='8',bg='powder blue',command=lambda: btnClick(8)).grid(row=2,column=1)

btn9=Button(f2,padx=16,pady=16,bd=8,fg='black',font=('arial',20,'bold'),
            text='9',bg='powder blue',command=lambda: btnClick(9)).grid(row=2,column=2)

addition=Button(f2,padx=16,pady=16,bd=8,fg='black',font=('arial',20,'bold'),
            text='+',bg='powder blue',command=lambda: btnClick('+')).grid(row=2,column=3)
# -------------------------------------------------------------------
btn4=Button(f2,padx=16,pady=16,bd=8,fg='black',font=('arial',20,'bold'),
            text='4',bg='powder blue',command=lambda: btnClick(4)).grid(row=3,column=0)

btn5=Button(f2,padx=16,pady=16,bd=8,fg='black',font=('arial',20,'bold'),
            text='5',bg='powder blue',command=lambda: btnClick(5)).grid(row=3,column=1)

btn6=Button(f2,padx=16,pady=16,bd=8,fg='black',font=('arial',20,'bold'),
            text='6',bg='powder blue',command=lambda: btnClick(6)).grid(row=3,column=2)

subtraction=Button(f2,padx=16,pady=16,bd=8,fg='black',font=('arial',20,'bold'),
            text='-',bg='powder blue',command=lambda: btnClick('+')).grid(row=3,column=3)
# -------------------------------------------------------------------
btn1=Button(f2,padx=16,pady=16,bd=8,fg='black',font=('arial',20,'bold'),
            text='1',bg='powder blue',command=lambda: btnClick(1)).grid(row=4,column=0)

btn2=Button(f2,padx=16,pady=16,bd=8,fg='black',font=('arial',20,'bold'),
            text='2',bg='powder blue',command=lambda: btnClick(2)).grid(row=4,column=1)

btn3=Button(f2,padx=16,pady=16,bd=8,fg='black',font=('arial',20,'bold'),
            text='3',bg='powder blue',command=lambda: btnClick(3)).grid(row=4,column=2)

multiply=Button(f2,padx=16,pady=16,bd=8,fg='black',font=('arial',20,'bold'),
            text='*',bg='powder blue',command=lambda: btnClick('*')).grid(row=4,column=3)
# -------------------------------------------------------------------
btn0=Button(f2,padx=16,pady=16,bd=8,fg='black',font=('arial',20,'bold'),
            text='0',bg='powder blue',command=lambda: btnClick(0)).grid(row=5,column=0)

btnClear=Button(f2,padx=16,pady=16,bd=8,fg='black',font=('arial',20,'bold'),
            text='C',bg='powder blue',command=btnClearDisplay).grid(row=5,column=1)

btnEquals=Button(f2,padx=16,pady=16,bd=8,fg='black',font=('arial',20,'bold'),
            text='=',bg='powder blue',command=btnEqualsInput).grid(row=5,column=2)

division=Button(f2,padx=16,pady=16,bd=8,fg='black',font=('arial',20,'bold'),
            text='/',bg='powder blue',command=lambda: btnClick('/')).grid(row=5,column=3)
# -------------------------------------------------------------------

root.mainloop()