Basic Graphical User Interface Calculator in Python

Posted by


In this tutorial, we will learn how to create a simple GUI calculator using Python. We will be using the tkinter library, which is a built-in library in Python for creating graphical user interfaces.

Step 1: Import the necessary libraries

First, we need to import the tkinter library and the messagebox module that we will use to display error messages.

from tkinter import *
from tkinter import messagebox

Step 2: Create the main window

Next, we will create the main window for our calculator using the Tk class from tkinter.

# Create the main window
root = Tk()
root.title("Simple Calculator")

Step 3: Create the display field

Now, we will create a text box to display the numbers and the result of the calculations.

display = Entry(root, width=20, borderwidth=5)
display.grid(row=0, column=0, columnspan=4)

Step 4: Define the button functions

Next, we will define the functions for the different calculator operations such as addition, subtraction, multiplication, and division.

def button_click(number):
    current = display.get()
    display.delete(0, END)
    display.insert(0, str(current) + str(number)

def button_clear():
    display.delete(0, END)

def button_add():
    first_number = display.get()
    global f_num
    f_num = int(first_number)
    display.delete(0, END)

def button_equal():
    second_number = display.get()
    display.delete(0, END)
    result = f_num + int(second_number)
    display.insert(0, result)

Step 5: Create the calculator buttons

Now, we will create the buttons for the numbers, operators, and the equals sign.

# Create the number buttons
button_1 = Button(root, text='1', command=lambda: button_click(1))
button_2 = Button(root, text='2', command=lambda: button_click(2))
button_3 = Button(root, text='3', command=lambda: button_click(3))
button_4 = Button(root, text='4', command=lambda: button_click(4))
button_5 = Button(root, text='5', command=lambda: button_click(5))
button_6 = Button(root, text='6', command=lambda: button_click(6))
button_7 = Button(root, text='7', command=lambda: button_click(7))
button_8 = Button(root, text='8', command=lambda: button_click(8))
button_9 = Button(root, text='9', command=lambda: button_click(9))
button_0 = Button(root, text='0', command=lambda: button_click(0))

# Create the operator buttons
button_add = Button(root, text='+', command=button_add)
button_equal = Button(root, text='=', command=button_equal)
button_clear = Button(root, text='C', command=button_clear)

Step 6: Display the buttons on the window

Finally, we will use the grid method to display the buttons on the calculator window.

# Display the number buttons
button_1.grid(row=1, column=0)
button_2.grid(row=1, column=1)
button_3.grid(row=1, column=2)

button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)

button_7.grid(row=3, column=0)
button_8.grid(row=3, column=1)
button_9.grid(row=3, column=2)

button_0.grid(row=4, column=0)

# Display the operator buttons
button_add.grid(row=1, column=3)
button_equal.grid(row=4, column=2)
button_clear.grid(row=4, column=1)

Step 7: Run the main loop

Finally, we will run the main loop for the calculator window.

root.mainloop()

That’s it! You have now created a simple GUI calculator in Python using tkinter. Feel free to play around with the code and add more functionality to the calculator, such as additional operators or advanced calculations.

0 0 votes
Article Rating
35 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@ChudiO
2 months ago

Really good tutorial, has helped improve my Python knowledge 😊

@AbhayGupta-oz1ix
2 months ago

This is what i was looking for. Thanks to tutor.

@xenpai994
2 months ago

Can someone send me the code

@SukhwinderSingh-qf2vc
2 months ago

There is problem
Text is not defined

@MAhmed12_5
2 months ago

bro thank you very much. you made my day. keep up

@apertaplays
2 months ago

I can create a calculator without gui.
Creating a gui is way harder, its different.

@jasongonzalez502
2 months ago

Can anyone explain to me how he navigates the code by highlighting a single character then just skimming it? I'm a bit new to coding and am still learning much. – Thanks!

@maxsperling3749
2 months ago

Are u German? I think i saw a "ö" or is this also on english keyboards .-.

@MMMdragon
2 months ago

when you put root = tk.Tk() it worked perfectly for you but for me it's says AttributeError: module 'tkinter' has no attribute 'tk'

@pan_ching
2 months ago

How do you do the copy paste trick??? damn. It seems like it's a shortcut key or somethin' it was so quick ahahahhah

@dedehurairrah4345
2 months ago

When i click the number are error, can you repair my code?

@PCANDPOCO
2 months ago

thank you very much

@RahulKumar-cm1sy
2 months ago

Thank you sir ❤

@PeguShorts
2 months ago

5:20

@rushikeshmali3057
2 months ago

Thanks for beautiful guidens

@azeezrahman9660
2 months ago

Minus botton not show

@soufatldude700
2 months ago

do you need pycharm for this, or can you use the normal python application?

@tummatantewadonprasit639
2 months ago

Tysm❤❤😊

@HardeepSingh-ec4zq
2 months ago

How can I add percentage button in this program

@ethan_haymore3907
2 months ago

everything worked but the buttons only work individually instead of 1+2 it would just say the number by itself when clicked instead of holding memory. any tips or advice