Python Calculator Gui

How to Make a Python Calculator Gui

We Are Making a Calculator In Python

Today we will learn to create a step by step calculator in Python. We all have this need sometimes and we use phone or calculator device to calculate, so being a coder, why should we make it programable by ourselves?

Lets start the code:-

How to Make a Python Calculator Gui

 

1.  Import Tkinter Module:

import tkinter as tk

In this line we are import the Tkinter module, which is used for creating the fram .

Calculator gyi using python

2. Button Click Handling:

def on_click(button_text):

This Python function does the work of clicking a button.

How to Make a Python Calculator Gui

3. Main Window and Entry Widget:

root = tk.Tk()
entry = tk.Entry(root, width=20, font=("Arial", 20), bd=5, relief=tk.GROOVE)

In This line, we create the main window screen “root” and an entry widgets “entry” for input and result display.

4. Button Creation and Placement:

for button in buttons:
    tk.Button(root, text=button, width=5, height=2, font=("Arial", 14), command=lambda b=button: on_click(b)).grid(row=row_val, column=col_val, padx=5, pady=5)

Button are created in the loop, and there position are defined based on the grides frame. 

5. main loop:-

root.mainloop()

This code starts the loop that updates the user interface to look and work better.

How to Make a Python Calculator Gui

Here is full code :-

import tkinter as tk

def on_click(button_text):
    current_text = entry.get()

    if button_text == "=":
        try:
            result = eval(current_text)
            entry.delete(0, tk.END)
            entry.insert(tk.END, str(result))
        except Exception as e:
            entry.delete(0, tk.END)
            entry.insert(tk.END, "Error")
    elif button_text == "C":
        entry.delete(0, tk.END)
    else:
        entry.insert(tk.END, button_text)

# Create the main window
root = tk.Tk()
root.title("Calculator By CodeWithRandom")

# Entry widget to display the current input and result
entry = tk.Entry(root, width=20, font=("Arial", 20), bd=5, relief=tk.GROOVE)
entry.grid(row=0, column=0, columnspan=4, padx=30, pady=30)

# Define the buttons and their positions
buttons = [
    '7', '8', '9', '/',
    '4', '5', '6', '*',
    '1', '2', '3', '-',
    '0', 'C', '=', '+'
]

row_val = 1
col_val = 0

# Create and place the buttons on the grid
for button in buttons:
    tk.Button(root, text=button, width=5, height=2, font=("Arial", 14), command=lambda b=button: on_click(b)).grid(row=row_val, column=col_val, padx=5, pady=5)
    col_val += 1
    if col_val > 3:
        col_val = 0
        row_val += 1

# Run the main loop
root.mainloop()

Final OutPut Of Python Calculator Gui:-

 

 

Thanks of reading my article . stay with us!

Author :- Ashutosh Mishra

visit my more Article:- Codewithrandom

FAQs:-

Which module is used to make this calculator?

tkinter module has been used to make this calculator.
import tkinter as tk

Which function will you use to create a po-pup window?

To create popup window use this function:-
tk.Tk()
root.title("Calculator By CodeWithRandom")