Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Python by (16.4k points)

Here, I am attempting to make a GUI calculator in Python for a class task/assignement called 'Chocolate Machine'. 

Yet, I've run into an issue; the code beneath isn't printing the buttons in the GUI! To perceive what I'm discussing, kindly glance at the code beneath the remarked line:

#CODE THAT IS NOT PRINTING THE BUTTON IS BELOW

It's fundamentally fastens 7, 8, 9, and the addition button, which should show up on the top row of each basic calculator. 

Could somebody help and assist me with understanding why this isn't working? I've attempted to utilize pack articulations however they don't work all things considered.

The instructional exercise I have been utilizing is by DJ Oamen on YouTube. Two pictures are attached, the one of the working GUI by DJ Oamen, and the one that doesn't work by me. 

Working GUI Calculator by DJ Oamen:

Not working GUI Calculator

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=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)

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)

txtDisplay = Entry(f2, font=('arial',20,'bold'),textvariable=text_Input, bd=30, 

    insertwidth=4,bg='powder blue', justify='right')

txtDisplay.grid(columnspan=4)

#CODE THAT IS NOT PRINTING THE BUTTON IS BELOW

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="9",bg="powder blue",command=lambda: btnClick("+").grid(row=2,column=3))

root.mainloop()

1 Answer

0 votes
by (26.4k points)

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.

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...