Creating a GUI Calculator in Python

Posted by


To create a GUI calculator in Python, you can use the Tkinter library which is a built-in package for creating graphical user interfaces. In this tutorial, I will guide you through the steps to create a simple GUI calculator using Tkinter.

Step 1: Install Tkinter
Tkinter is included with Python installation by default, so you don’t need to install anything extra.

Step 2: Import the necessary modules
To begin, you need to import the Tkinter module and create a main window for your calculator. You can do this by using the following code:

import tkinter as tk

root = tk.Tk()
root.title("Simple Calculator")

Step 3: Create functions for calculator operations
Next, you’ll need to create functions for different calculator operations like addition, subtraction, multiplication, and division. Below is an example of how you can define these functions:

def add():
    result = float(num1.get()) + float(num2.get())
    result_label.config(text="Result: " + str(result))

def subtract():
    result = float(num1.get()) - float(num2.get())
    result_label.config(text="Result: " + str(result))

def multiply():
    result = float(num1.get()) * float(num2.get())
    result_label.config(text="Result: " + str(result))

def divide():
    result = float(num1.get()) / float(num2.get())
    result_label.config(text="Result: " + str(result))

Step 4: Create input fields and buttons
Now, you can create input fields for entering numbers and buttons for performing different operations. Here’s how you can create them:

num1_label = tk.Label(root, text="Enter first number:")
num1_label.pack()

num1 = tk.Entry(root)
num1.pack()

num2_label = tk.Label(root, text="Enter second number:")
num2_label.pack()

num2 = tk.Entry(root)
num2.pack()

add_button = tk.Button(root, text="+", command=add)
add_button.pack()

subtract_button = tk.Button(root, text="-", command=subtract)
subtract_button.pack()

multiply_button = tk.Button(root, text="*", command=multiply)
multiply_button.pack()

divide_button = tk.Button(root, text="/", command=divide)
divide_button.pack()

result_label = tk.Label(root, text="Result:")
result_label.pack()

Step 5: Run the GUI calculator
Finally, you can run the GUI calculator by adding the main loop at the end of your script:

root.mainloop()

Now, you can run your Python script and a simple GUI calculator with input fields and buttons for different operations will be displayed on your screen. You can enter numbers in the input fields and click on the buttons to perform addition, subtraction, multiplication, and division.

This is the basic implementation of a GUI calculator using Tkinter in Python. You can further improve and customize the calculator by adding more features like decimal point support, clear button, and more advanced operations. Experiment with the code and have fun creating your own customized GUI calculator!

0 0 votes
Article Rating

Leave a Reply

13 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@mongvicheka7548
7 days ago

Oh my!!!!! I want it

@music____man
7 days ago

code please

@an_account
7 days ago

program he used (i think): Sublime Text

@exploit7021
7 days ago

Which ide ? Get gui in vscode or pycham?

@ΑντώνηςΠετσος
7 days ago

According to pep8 every line of code most be less than 74 chars fix that

@iwebbio
7 days ago

Nice 🙂

@dhruvmishra41313
7 days ago

Which software do u use?

@МихаилМатрёнин-ц5р
7 days ago

Not use grid method, use place, plece is eazy

@flew6176
7 days ago

ahahha you can just stack all button in a list and access with

@atiqsohail2082
7 days ago

Hey I want to learn coding but I don't know what coding language should I learn first

@Alexandre_0292
7 days ago

Cool

@BlueProgamer212
7 days ago

You again >_<. *lack of for-loops and arrays* Yandere Dev

13
0
Would love your thoughts, please comment.x
()
x