Skip to main content

How to create a simple GUI calculator using Tkinter in Python

In this blog post, I will show you how to create a simple GUI calculator using Tkinter in Python. Tkinter is a standard library module that provides a graphical user interface (GUI) for Python programs. It is easy to use and comes with many widgets that can be used to create various types of GUI applications.


To create a calculator, we will need the following widgets:


- A `Label` to display the result of the calculation

- A `Entry` to enter the numbers and operators

- A `Button` to perform the calculation

- A `Frame` to organize the buttons in a grid layout


The steps to create the calculator are as follows:


1. Import the Tkinter module and create a root window

2. Create a label and an entry and place them at the top of the window

3. Create a frame and place it below the entry

4. Create 16 buttons for the digits (0-9), the decimal point (.), and the operators (+, -, *, /, =, C) and place them in the frame using a grid layout

5. Define a function that will be called when a button is clicked. The function will update the entry with the button value or perform the calculation depending on the button

6. Bind the function to each button using the `command` option

7. Start the main loop of the window


The code for the calculator is given below:

# Import Tkinter module
from tkinter import *

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

# Create a label to display the result
result = Label(root, text="0", font=("Arial", 20), bg="white", anchor=E)
result.grid(row=0, column=0, columnspan=4, sticky="nsew", padx=5, pady=5)

# Create an entry to enter the numbers and operators
entry = Entry(root, font=("Arial", 20), bg="white", justify=RIGHT)
entry.grid(row=1, column=0, columnspan=4, sticky="nsew", padx=5, pady=5)

# Create a frame to hold the buttons
frame = Frame(root)
frame.grid(row=2, column=0, columnspan=4, sticky="nsew")

# Create 16 buttons for the digits and operators
buttons = ["7", "8", "9", "+",
"4", "5", "6", "-",
"1", "2", "3", "*",
"0", ".", "=", "/",
"C"]

# Place the buttons in the frame using a grid layout
for i in range(len(buttons)):
button = Button(frame, text=buttons[i], font=("Arial", 20), bg="lightgray")
button.grid(row=i//4+2, column=i%4, sticky="nsew", padx=5, pady=5)

# Define a function that will be called when a button is clicked
def click(event):
# Get the button value
value = event.widget.cget("text")

# Get the current entry text
text = entry.get()

# If the button is C, clear the entry and result
if value == "C":
entry.delete(0, END)
result.config(text="0")

# If the button is =, evaluate the expression and display the result
elif value == "=":
try:
answer = eval(text)
result.config(text=str(answer))
except:
result.config(text="Error")

# Otherwise, append the button value to the entry
else:
entry.insert(END, value)

# Bind the function to each button using the command option
for button in frame.winfo_children():
button.config(command=lambda event=button: click(event))

# Start the main loop of the window
root.mainloop()
```

I hope this blog post was helpful for you to learn how to create a simple GUI calculator using Tkinter in Python. Happy coding!
```


Comments

Popular posts from this blog

How to create a login page in Tkinter [PYTHON] {VS CODE}

if You want to create a login page in tkinter here's the  tutorial   Insure that you have installed Tkinter Module VIDEO: Source code: from tkinter import * root = Tk() root.geometry( "700x455" ) def k():     Label(text= "Login UnSuccesful" ,font= "timesnewroman 12 bold" ).grid(row= 12 ,column= 3 ) i = Label(text= "User ID" ,font= "comicsansm 15 bold" ).grid(row= 2 ,column= 2 ) j = Label(text= "Password" ,font= "comicsansm 15 bold" ).grid(row= 3 ,column= 2 ) Label(text= "LOGIN SETUP" ,font= "callebri 13 bold" ,padx= 540 ).grid(row= 0 ,column= 3 ,columnspan= 9 ) user = Entry(textvariable=i).grid(row= 2 ,column= 3 ) passwd = Entry(textvariable=j).grid(row= 3 ,column= 3 ) Button(text= "SUBMIT" ,command=k,font= "helvatica 10 bold" ).grid(row = 6 , column= 3 ) root.mainloop()

Creating a web app In Python

Python is a versatile and powerful programming language that can be used for various applications, such as data analysis, machine learning, web development and more. In this blog post, we will focus on how to create a web app in Python using some popular frameworks and tools. A web app is a software application that runs on a web server and can be accessed by users through a web browser. A web app typically consists of two main components: the front-end and the back-end. The front-end is the part of the app that the user interacts with, such as the user interface, the layout, the graphics and the functionality. The back-end is the part of the app that handles the logic, the data processing, the database and the communication with other services. To create a web app in Python, we need to choose a framework that provides us with the tools and libraries to build both the front-end and the back-end of our app. There are many frameworks available for Python web development, such as Django, ...