-

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

Mpox Explained: History, Spread, Prevention, and Key Do's and Don'ts for Staying Safe Mpox, once known primarily as monkeypox, has resurfaced as a global health concern. This comprehensive guide delves into the origins, historical outbreaks, and transmission of mpox, while offering essential prevention strategies, reasons for concern, and reassuring facts. Stay informed with practical do's and don'ts to protect yourself and your community from this emerging infectious disease.
The Role, Responsibility and Importance of the Governor in the Indian Governance System The Governor is the constitutional head of a state in India, functioning as the representative of the President of India at the state level. The Governor's role encompasses both executive and legislative functions, and their duties are primarily defined by the Constitution of India.
Different types of taxes in India In India, the taxation system is intricate and includes various types of taxes that cater to different sectors of the economy. Here are 20 different types of taxes in India.
The Olympics: A Journey Through History and the Road to Paris 2024 The Olympic Games, often referred to simply as the Olympics, are the world's foremost sports competition, featuring summer and winter games in which thousands of athletes from around the world participate in a variety of competitions. The Games are considered the world's premier sports competition and include more than 200 nations participating.
Understanding the Indian Tax Structure The Indian tax structure is a comprehensive system that funds the government's expenditures and supports the nation's economic development. In this blog, we will explore the various taxes levied in India, their rates, and important aspects to consider. Please note that tax rates and policies are subject to change, and it is essential to consult with a tax professional or refer to the latest government notifications for the most accurate and up-to-date information.
AI in Indian Elections: Unveiling the Challenges and Strategic Solutions Artificial Intelligence (AI) is revolutionizing numerous sectors globally, and elections are no exception. In India, with its vast and diverse electorate, the integration of AI into the electoral process presents both significant opportunities and complex challenges. This blog post delves into the role of AI in Indian elections, highlighting the challenges and proposing strategic solutions to ensure a fair and transparent democratic process.