Python Programmer Logo Python Programmer Python Programmer Logo


GUI Calculator Tutorial

Below is the python file I used in this video and the actual video


The video file

File Download Date Modified
GUI Calculator Tutorial Download File 23 Feb 2024

The actual code in the file


"""Today we're going to show you guys how to build a
GUI calculator using tkinter and the math libraries"""

#First we need to import the libraries

import tkinter, math

#We then need to set up a global variable
expression = ""

#A main function needs to be created

def main():

    #Some functions for the buttons are now needed
    def press(num):
        global expression
        expression += str(num)
        equation.set(expression)

    def equal():
        try:
            global expression
            total = str(eval(expression))
            equation.set(total)
        except:
            equation.set(" error ")
            expression = ""

    def clear():
        global expression
        expression = ""
        equation.set(expression)

    def back():
        global expression
        expression = expression
        equation.set(expression)

    def pi():
        global expression
        expression = str(math.pi)
        equation.set(expression)

    #Now a tkinter window needs to be setup
    window = tkinter.Tk()
    window.title("GUI calculator")

    #We need to setup a couple of frames to organise it
    frame1 = tkinter.Frame(master=window, width=200, height=100)
    frame2 = tkinter.Frame(master=window, width=200, height=400)
    frame1.grid(row=0, column=0)
    frame2.grid(row=1, column=0)
    """The two lines at the end are used to place the
    frames onto the window with the grid method"""

    #A StringVar variable can be set up now
    equation = tkinter.StringVar()

    #We have to configure the rows and columns
    frame2.rowconfigure([0,1,2,3,4],minsize=50,weight=1)
    frame2.columnconfigure([0,1,2,3],minsize=50,weight=1)

    #Now we can setup the display
    display = tkinter.Entry(master=frame1, textvariable=equation)
    display.pack()
    """Pack is another method use to display elements
    onto the window or other widgets"""

    #Now we setup each button individually
    clear = tkinter.Button(master=frame2,width=2,height=2,text=
                   "C",command=clear)
    pi = tkinter.Button(master=frame2,width=2,height=2,text=
                "\u03C0",command=pi)
    back = tkinter.Button(master=frame2,width=2,height=2,text=
                  "B",command=back)
    div = tkinter.Button(master=frame2,width=2,height=2,text=
                 chr(247),command=lambda: press("/"))
    number1 = tkinter.Button(master=frame2,width=2,height=2,text=
                     "1",command=lambda: press("1"))
    number2 = tkinter.Button(master=frame2,width=2,height=2,text=
                     "2",command = lambda: press("2"))
    number3 = tkinter.Button(master=frame2,width=2,height=2,text=
                     "3",command = lambda: press("3"))
    number4 = tkinter.Button(master=frame2,width=2,height=2,text=
                     "4",command=lambda: press("4"))
    number5 = tkinter.Button(master=frame2,width=2,height=2,text=
                     "5",command=lambda: press("5"))
    number6 = tkinter.Button(master=frame2,width=2,height=2,text=
                     "6",command=lambda: press("6"))
    number7 = tkinter.Button(master=frame2,width=2,height=2,text=
                     "7",command=lambda: press("7"))
    number8 = tkinter.Button(master=frame2,width=2,height=2,text=
                     "8",command=lambda: press("8"))
    number9 = tkinter.Button(master=frame2,width=2,height=2,text=
                     "9",command=lambda: press("9"))
    number0 = tkinter.Button(master=frame2,width=2,height=2,text=
                     "0",command=lambda: press("0"))
    minus = tkinter.Button(master=frame2,width=2,height=2,text=
                   "-",command=lambda: press("-"))
    dot = tkinter.Button(master=frame2,width=2,height=2,text=
                 ".",command=lambda: press("."))
    multiply = tkinter.Button(master=frame2,width=2,height=
                      2,text="*",command=lambda: press("*"))
    add = tkinter.Button(master=frame2,width=2,height=2,text=
                 "+",command=lambda: press("+"))
    takeaway = tkinter.Button(master=frame2,width=2,height=
                      2,text="-",command=lambda: press("-"))
    equal = tkinter.Button(master=frame2,width=2,height=2,text=
                   "=",command=equal)

    #Now we have to use the grid method for each button
    clear.grid(row=0, column=0)
    pi.grid(row=0, column=1)
    back.grid(row=0, column=2)
    div.grid(row=0, column=3)
    number1.grid(row=1, column=0)
    number2.grid(row=1, column=1)
    number3.grid(row=1, column=2)
    number4.grid(row=2, column=0)
    number5.grid(row=2, column=1)
    number6.grid(row=2, column=2)
    number7.grid(row=3, column=0)
    number8.grid(row=3, column=1)
    number9.grid(row=3, column=2)
    number0.grid(row=4, column=1)
    minus.grid(row=4, column=0)
    dot.grid(row=4, column=2)
    multiply.grid(row=1, column=3)
    add.grid(row=2, column=3)
    takeaway.grid(row=3, column=3)
    equal.grid(row=4, column=3)

    #So now we just need one more line
    window.mainloop()

if __name__ == "__main__":
    main()

#Theses last 2 lines allow us to run the program