Ad Code

GUI Calculator Using Python Tkinter | Python Series.


GUI Calculator

Welcome to CodingFizz,


In this tutorial, we will make a basic and simple calculator using python tkinter.


What is Tkinter?

Tkinter is a python interface. It's used to create Graphic User Interface(GUIs).

 

To Create a Simple GUI Calculator:

  • Import Tkinter.
  • Create a Window for adding widgets.
  • Add Widgets like Text, Button, Frame, etc.
  • Add event handling.


Source Code:

Code without function:

 from tkinter import *

 def click(event):
    global scvalue
    text = event.widget.cget("text")
    if text == "=":
        if scvalue.get().isdigit():
            value = int(scvalue.get())
        else:
            try:
                value = eval(screen.get())

            except Exception as e:
                print(e)
                value = "Error"


        scvalue.set(value)

        screen.update()

    elif text == "C":
        scvalue.set("")
        screen.update()

    else:
        scvalue.set(scvalue.get() + text)
        screen.update()

 root = Tk()
 root.geometry("310x370")
 root.title("Calculator By Codingfizz")
 root.wm_iconbitmap("1.ico")

 scvalue = StringVar()
 scvalue.set("")
 screen = Entry(root, textvar=scvalue, font="lucida 40 bold")
 screen.pack(fill=X, ipadx=8, pady=10, padx=10)

 f = Frame(root, bg="light gray")

 b = Button(f, text="9", padx=18, pady=15, font="lucida  10 bold")
 b.pack(side=LEFT, padx=10, pady=5)
 b.bind("<Button-1>", click)

 b = Button(f, text="8", padx=18, pady=15, font="lucida  10 bold")
 b.pack(side=LEFT, padx=10, pady=5)
 b.bind("<Button-1>", click)

 b = Button(f, text="7", padx=18, pady=15, font="lucida  10 bold")
 b.pack(side=LEFT, padx=10, pady=5)
 b.bind("<Button-1>", click)

 b = Button(f, text="C", padx=18, pady=15, font="lucida  10 bold")
 b.pack(side=LEFT, padx=10, pady=5)
 b.bind("<Button-1>", click)

 f.pack()


 f = Frame(root, bg="light gray")

 b = Button(f, text="6", padx=18, pady=15, font="lucida  10 bold")
 b.pack(side=LEFT, padx=10, pady=5)
 b.bind("<Button-1>", click)

 b = Button(f, text="5", padx=18, pady=15, font="lucida  10 bold")
 b.pack(side=LEFT, padx=10, pady=5)
 b.bind("<Button-1>", click)

 b = Button(f, text="4", padx=18, pady=15, font="lucida  10 bold")
 b.pack(side=LEFT, padx=10, pady=5)
 b.bind("<Button-1>", click)

 b = Button(f, text="+", padx=18, pady=15, font="lucida  10 bold")
 b.pack(side=LEFT, padx=10, pady=5)
 b.bind("<Button-1>", click)

 f.pack()


 f = Frame(root, bg="light gray")

 b = Button(f, text="3", padx=18, pady=15, font="lucida  10 bold")
 b.pack(side=LEFT, padx=11, pady=5)
 b.bind("<Button-1>", click)

 b = Button(f, text="2", padx=18, pady=15, font="lucida  10 bold")
 b.pack(side=LEFT, padx=11, pady=5)
 b.bind("<Button-1>", click)
 
 b = Button(f, text="1", padx=18, pady=15, font="lucida  10 bold")
 b.pack(side=LEFT, padx=9, pady=5)
 b.bind("<Button-1>", click)

 b = Button(f, text="-", padx=19, pady=15, font="lucida  10 bold")
 b.pack(side=LEFT, padx=10, pady=5)
 b.bind("<Button-1>", click)

 f.pack()

 f = Frame(root, bg="light gray")

 b = Button(f, text="0", padx=18, pady=15, font="lucida  10 bold")
 b.pack(side=LEFT, padx=11, pady=10)
 b.bind("<Button-1>", click)

 b = Button(f, text="=", padx=18, pady=15, font="lucida  10 bold")
 b.pack(side=LEFT, padx=11, pady=10)
 b.bind("<Button-1>", click)

 b = Button(f, text="/", padx=18, pady=15, font="lucida  10 bold")
 b.pack(side=LEFT, padx=11, pady=10)
 b.bind("<Button-1>", click)

 b = Button(f, text="*", padx=18, pady=15, font="lucida  10 bold")
 b.pack(side=LEFT, padx=11, pady=10)
 b.bind("<Button-1>", click)

 f.pack()

 root.mainloop()

Code with function:

 from tkinter import *

 def click(event):
    global scvalue
    text = event.widget.cget("text")
    if text == "=":
        if scvalue.get().isdigit():
            value = int(scvalue.get())
        else:
            try:
                value = eval(screen.get())

            except Exception as e:
                print(e)
                value = "Error"
        scvalue.set(value)
        screen.update()

    elif text == "C":
        scvalue.set("")
        screen.update()

    else:
        scvalue.set(scvalue.get() + text)
        screen.update()

 root = Tk()

 root.geometry("310x370")
 root.title("Calculator By Codingfizz")
 root.wm_iconbitmap("1.ico")

 scvalue = StringVar()
 scvalue.set("")
 screen = Entry(root, textvar=scvalue, font="lucida 40 bold")
 screen.pack(fill=X, ipadx=8, pady=10, padx=10)

 def button(t1,t2,t3,t4):
    f = Frame(root, bg="light gray")

    b = Button(f, text=t1, padx=18, pady=15, font="lucida  10 bold")
    b.pack(side=LEFT, padx=10, pady=5)
    b.bind("<Button-1>", click)

    b = Button(f, text=t2, padx=18, pady=15, font="lucida  10 bold")
    b.pack(side=LEFT, padx=10, pady=5)
    b.bind("<Button-1>", click)

    b = Button(f, text=t3, padx=18, pady=15, font="lucida  10 bold")
    b.pack(side=LEFT, padx=10, pady=5)
    b.bind("<Button-1>", click)

    b = Button(f, text=t4, padx=18, pady=15, font="lucida  10 bold")
    b.pack(side=LEFT, padx=10, pady=5)
    b.bind("<Button-1>", click)

    f.pack()


 button("9","8","7","C")
 button("6","5","4","+")
 button("3","2","1","-")
 button("0","=","/","*")

 root.mainloop()

Output:


Snapshot

Video


Post a Comment

0 Comments

Ad Code