-

Easy Python Learning: Calculator script in Python with GUI

 Easy Python Learning: Calculator script in Python with GUI

Easy Python Learning: Calculator script in Python with GUI

To create a basic calculator with a graphical user interface (GUI) in Python, we can use the tkinter library. Here's a simple Python script that implements a GUI-based calculator:

import tkinter as tk
def on_button_click(event):
    button_text = event.widget.cget("text")
    if button_text == "=":
        try:
            result = eval(entry.get())
            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)

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

entry = tk.Entry(root, font=("Helvetica", 20))
entry.pack(ipadx=10, ipady=10)

buttons_frame = tk.Frame(root)
buttons_frame.pack()

buttons = [
    "7", "8", "9", "/",
    "4", "5", "6", "*",
    "1", "2", "3", "-",
    "C", "0", "=", "+"
]

row, col = 0, 0
for button in buttons:
    btn = tk.Button(buttons_frame, text=button, font=("Helvetica", 20), padx=20, pady=10)
    btn.grid(row=row, column=col, padx=5, pady=5)
    btn.bind("", on_button_click)
    col += 1
    if col > 3:
        col = 0
        row += 1

root.mainloop()

Save the script in a file with a .py extension, and run it. You will see a GUI window with buttons representing numbers, operators, and the "C" (clear) button. You can click on the buttons to perform calculations just like a regular calculator. The eval() function is used to evaluate the mathematical expression entered in the entry field, and the result is displayed accordingly. Note that using eval() may pose security risks if the calculator is used in an untrusted environment, so exercise caution when using this approach.

You may interested in

Creating a beautiful kaleidoscope using Python's turtle Creating a beautiful kaleidoscope using Python's turtle graphics can be a fun and mesmerizing experience. Below is a Python script to draw a kaleidoscope-like pattern using the turtle module:
Easy Python Learning: Calculator script in Python with GUI To create a basic calculator with a graphical user interface (GUI) in Python, we can use the tkinter library. Here's a simple Python script that implements a GUI-based calculator:
Easy Python Learning: Basic calculator script in Python To create a calculator in Python, Below is a simple script that can perform addition, subtraction, multiplication, and division:

Trending

Understanding the IPO Allotment Process and SEBI Guidelines in India Initial Public Offerings (IPOs) are a significant step for companies seeking to raise capital from the public market. Investors, in turn, are keen on participating in IPOs to gain from potential listing gains and long-term growth. However, the allotment process can be complex, and the Securities and Exchange Board of India (SEBI) has laid down comprehensive guidelines to ensure fairness and transparency. This article explores the IPO allotment process and SEBI’s regulations governing it.
Is USA heading towards recessions? What data says ? While the U.S. economy continues to grow, certain indicators, such as the rising unemployment rate and increased recession probabilities from reputable sources, suggest caution. The situation remains fluid, and close monitoring of economic indicators is essential to assess the potential for a recession in the near future.
Reason behind Indian market fall what investor should do In a volatile market like today's, investors can adopt a strategic and balanced approach to protect their portfolios while seeking long-term growth opportunities. Here are some key actions to consider:
What is an IPO and How It Works? An Initial Public Offering (IPO) is one of the most significant events in the financial world, marking a company's transition from being privately held to publicly traded. For businesses looking to raise capital, an IPO offers a unique opportunity to tap into a wide pool of investors. But for potential investors, an IPO can be an exciting opportunity to buy shares in a company at the early stages of its public market journey. In this blog, we'll break down what an IPO is, how it works, its significance, and the various aspects surrounding this process.
Why Financial Education is More Important Than Academic Education and Why It’s Missing in the Indian Education System Despite the growing importance of managing money and understanding personal finance, most educational systems, including India's, have largely overlooked this essential skill. Financial education equips individuals with the knowledge to manage money effectively, make informed decisions, and plan for a secure future.
Understanding Authentication Methods: OTP, TOTP, Passwords, PINs, Patterns, and More in 2025 In today’s digital age, safeguarding personal and organizational data is paramount. To achieve this, various methods of authentication are used to ensure that only authorized individuals can access sensitive information or services. Among these methods are One-Time Passwords (OTP), Time-Based One-Time Passwords (TOTP), Passwords, PINs, and Patterns. Each of these authentication techniques has distinct characteristics, use cases, and security considerations.