Here, The calculator alone itself working
from tkinter import *
def iCalc(source, side):
storeObj = Frame(source, borderwidth=4, bd=4, bg="black")
storeObj.pack(side=side, expand=YES, fill=BOTH)
return storeObj
def button(source, side, text, command=None):
storeObj = Button(source, text=text, command=command)
storeObj.pack(side=side, expand=YES, fill=BOTH)
return storeObj
class app(Frame):
def __init__(self):
Frame.__init__(self)
self.option_add("*Font", "arial 20 bold")
self.pack(expand=YES, fill=BOTH)
self.master.title("Calculator")
display = StringVar()
# relief can be FLAT or RIDGE or RAISED or SUNKEN GROOVE
Entry(self, relief=RIDGE, textvariable=display, justify='right', bd=30, bg='darkgray').pack(side=TOP, expand=YES, fill=BOTH)
for clearBut in (["CE"], ["C"]):
erase = iCalc(self, TOP)
for ichar in clearBut:
button(erase, LEFT, ichar, lambda storeObj=display, q=ichar: storeObj.set(""))
for numBut in ("789/", "456*", "123-", "0.+"):
functionNum = iCalc(self, TOP)
for char in numBut:
button(functionNum, LEFT, char, lambda storeObj=display, q=char: storeObj.set(storeObj.get() + q))
equalButton = iCalc(self, TOP)
for iEqual in "=":
if iEqual == "=":
btniEqual = button(equalButton, LEFT, iEqual)
btniEqual.bind("<ButtonRelease-1>", lambda e, s=self, storeObj=display: s.calc(storeObj), '+')
else:
btniEqual = button(equalButton, LEFT, iEqual, lambda storeObj=display, s='%s' % iEqual: storeObj.set(storeObj.get() + s))
def calc(self, display):
try:
display.set(eval(display.get()))
except:
display.set("ERROR")
if __name__ == '__main__':
app().mainloop()
The calculator in the right frame.
from tkinter import *
import random
import time
root = Tk()
root.geometry("1600x800+0+0")
root.title("Cameron's Chocolate Machine")
text_Input = StringVar()
operator = ""
Tops = Frame(root, width=1600, height=50, bg="powder blue", relief=SUNKEN)
Tops.pack(side=TOP)
f1 = Frame(root, width=1200, height=700, bg="powder blue", relief=SUNKEN)
f1.pack(side=LEFT)
localtime = time.asctime(time.localtime(time.time()))
lblInfo = Label(Tops, font=('simplifica', 50, 'bold'), text="Cameron's Chocolate Machine", fg="Steel Blue", bd=10, anchor='w')
lblInfo.grid(row=0, column=0)
lblInfo = Label(Tops, font=('simplifica', 20), text=localtime, fg="Steel Blue",
bd=10, anchor='w')
lblInfo.grid(row=1, column=0)
def btnClick(numbers):
global operator
operator = operator + str(numbers)
text_Input.set(operator)
def iCalc(source, side):
storeObj = Frame(source, borderwidth=4, bd=4, bg="black")
storeObj.pack(side=side, expand=YES, fill=BOTH)
return storeObj
def button(source, side, text, command=None):
storeObj = Button(source, text=text, command=command)
storeObj.pack(side=side, expand=YES, fill=BOTH)
return storeObj
class app(Frame):
def __init__(self):
Frame.__init__(self)
self.option_add("*Font", "arial 20 bold")
self.pack(expand=YES, fill=BOTH)
self.master.title("Calculator")
display = StringVar()
# relief can be FLAT or RIDGE or RAISED or SUNKEN GROOVE
Entry(self, relief=RIDGE, textvariable=display, justify='right', bd=30, bg='darkgray').pack(side=TOP, expand=YES, fill=BOTH)
for clearBut in (["CE"], ["C"]):
erase = iCalc(self, TOP)
for ichar in clearBut:
button(erase, LEFT, ichar, lambda storeObj=display, q=ichar: storeObj.set(""))
for numBut in ("789/", "456*", "123-", "0.+"):
functionNum = iCalc(self, TOP)
for char in numBut:
button(functionNum, LEFT, char, lambda storeObj=display, q=char: storeObj.set(storeObj.get() + q))
equalButton = iCalc(self, TOP)
for iEqual in "=":
if iEqual == "=":
btniEqual = button(equalButton, LEFT, iEqual)
btniEqual.bind("<ButtonRelease-1>", lambda e, s=self, storeObj=display: s.calc(storeObj), '+')
else:
btniEqual = button(equalButton, LEFT, iEqual, lambda storeObj=display, s='%s' % iEqual: storeObj.set(storeObj.get() + s))
def calc(self, display):
try:
display.set(eval(display.get()))
except:
display.set("ERROR")
app().mainloop()
root.mainloop()
Explanation of the above code:
I put here some explanations of the code, as requested by C S. I will come back later to add more detailed explanations and maybe a video.
from tkinter import *
import time
# initialize the window
root = Tk()
# we put the dimension and position of left corner of the window
root.geometry("1600x800+0+0")
# the title of the window
root.title("Cameron's Chocolate Machine")
# variable to be used later
text_Input = StringVar()
operator = ""
# we have a frame inside the window object root, width 1600 and 50 height
Tops = Frame(root, width=1600, height=50, bg="powder blue", relief=SUNKEN)
# this makes it visible
Tops.pack(side=TOP)
# another frame of 1200 x 700
f1 = Frame(root, width=1200, height=700, bg="powder blue", relief=SUNKEN)
f1.pack(side=LEFT)
# this gets the time
localtime = time.asctime(time.localtime(time.time()))
# here is the big title into a label
lblInfo = Label(Tops, font=('simplifica', 50, 'bold'), text="Cameron's Chocolate Machine", fg="Steel Blue", bd=10, anchor='w')
# this shows the label and put it at row 0, col 0
lblInfo.grid(row=0, column=0)
# This is another label for the time in the row 1, same column as befor
lblInfo = Label(Tops, font=('simplifica', 20), text=localtime, fg="Steel Blue",
bd=10, anchor='w')
lblInfo.grid(row=1, column=0)
# this puts the digit into the text_Input variable = StringVar() we saw before
def btnClick(numbers):
global operator
operator = operator + str(numbers)
text_Input.set(operator)
# This function creates a frame and makes it visible
def iCalc(source, side):
storeObj = Frame(source, borderwidth=4, bd=4, bg="black")
storeObj.pack(side=side, expand=YES, fill=BOTH)
return storeObj
# This is a constructor for each button of the calculator
# when you call this function it returns a button
def button(source, side, text, command=None):
storeObj = Button(source, text=text, command=command)
storeObj.pack(side=side, expand=YES, fill=BOTH)
return storeObj
# this is the main app for the calculator
class app(Frame):
def __init__(self):
# this creates the frame for the calculator
Frame.__init__(self)
self.option_add("*Font", "arial 20 bold")
self.pack(expand=YES, fill=BOTH)
self.master.title("Calculator")
# this is a variable to get the value of the following entry object
display = StringVar()
# relief can be FLAT or RIDGE or RAISED or SUNKEN GROOVE
Entry(self, relief=RIDGE, textvariable=display, justify='right', bd=30, bg='darkgray').pack(side=TOP, expand=YES, fill=BOTH)
for clearBut in (["CE"], ["C"]):
erase = iCalc(self, TOP)
for ichar in clearBut:
button(erase, LEFT, ichar, lambda storeObj=display, q=ichar: storeObj.set(""))
# here we create all the buttons passing
for numBut in ("789/", "456*", "123-", "0.+"): # for each of this strings
functionNum = iCalc(self, TOP) # this is the frame for each string of three symmbols
for char in numBut: # for every number of symbol in each line ("789" for ex.)
# a button is created
button(functionNum, LEFT, char, lambda storeObj=display, q=char: storeObj.set(storeObj.get() + q))
equalButton = iCalc(self, TOP)
for iEqual in "=":
if iEqual == "=":
btniEqual = button(equalButton, LEFT, iEqual)
btniEqual.bind("<ButtonRelease-1>", lambda e, s=self, storeObj=display: s.calc(storeObj), '+')
else:
btniEqual = button(equalButton, LEFT, iEqual, lambda storeObj=display, s='%s' % iEqual: storeObj.set(storeObj.get() + s))
def calc(self, display):
try:
display.set(eval(display.get()))
except:
display.set("ERROR")
app().mainloop()
root.mainloop()
Interested to learn python in detail? Come and Join the python course.