0% found this document useful (0 votes)
30 views2 pages

Gui Calculator

This document provides a step-by-step guide to creating a basic graphical user interface (GUI) calculator application in Python using Tkinter. It involves importing the Tkinter library, creating a Calculator class with an entry widget and buttons, and defining a method to handle button clicks by performing actions like appending values, evaluating expressions, and clearing the input. The main function creates an instance of the Calculator class and runs the main event loop.

Uploaded by

Mahboob Riaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

Gui Calculator

This document provides a step-by-step guide to creating a basic graphical user interface (GUI) calculator application in Python using Tkinter. It involves importing the Tkinter library, creating a Calculator class with an entry widget and buttons, and defining a method to handle button clicks by performing actions like appending values, evaluating expressions, and clearing the input. The main function creates an instance of the Calculator class and runs the main event loop.

Uploaded by

Mahboob Riaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Creating a graphical user interface (GUI) calculator in Python using object-oriented

programming (OOP) involves several steps. We'll be using the Tkinter library, which is a
standard GUI library for Python.

Here's a step-by-step guide to creating a basic GUI calculator:

Step 1: Import Necessary Libraries

pythonCopy code
import tkinter as tk

Step 2: Create a Calculator Class

pythonCopy code
class Calculator:
def __init__(self, master):
self.master = master
master.title("Simple Calculator")

# Entry widget to display input/output


self.entry = tk.Entry(master, width=30, borderwidth=5)
self.entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)

# Buttons
buttons = [
('7', 1, 0), ('8', 1, 1), ('9', 1, 2), ('/', 1, 3),
('4', 2, 0), ('5', 2, 1), ('6', 2, 2), ('*', 2, 3),
('1', 3, 0), ('2', 3, 1), ('3', 3, 2), ('-', 3, 3),
('0', 4, 0), ('.', 4, 1), ('=', 4, 2), ('+', 4, 3),
('C', 5, 0), ('AC', 5, 1)
]

for (text, row, col) in buttons:


button = tk.Button(master, text=text, width=5, height=2, command=lambda t=text:
self.on_button_click(t))
button.grid(row=row, column=col, padx=5, pady=5)

Step 3: Implement Button Click Functionality

pythonCopy code
def on_button_click(self, value):
1

if value == '=':
Page

try:
result = eval(self.entry.get())
self.entry.delete(0, tk.END)
self.entry.insert(tk.END, str(result))
except:
self.entry.delete(0, tk.END)
self.entry.insert(tk.END, "Error")
elif value == 'C':
current_value = self.entry.get()[:-1]
self.entry.delete(0, tk.END)
self.entry.insert(tk.END, current_value)
elif value == 'AC':
self.entry.delete(0, tk.END)
else:
current_value = self.entry.get()
self.entry.delete(0, tk.END)
self.entry.insert(tk.END, current_value + value)

Step 4: Create the Main Function

pythonCopy code
def main():
root = tk.Tk()
calculator = Calculator(root)
root.mainloop()

if __name__ == "__main__":
main()

Explanation of Steps:

1. Import Libraries: We import the Tkinter library which provides functions for creating GUIs.
2. Create Calculator Class: We create a class called Calculator which represents our calculator
application. In the constructor __init__, we initialize the calculator window (master), set its title,
create an entry widget to display input/output, and create buttons for digits, operators, and
control functions.
3. Implement Button Click Functionality: We define a method on_button_click to handle button
clicks. Depending on the button clicked, it performs different actions such as appending the
clicked value to the input, evaluating the expression, clearing the input, etc.
4. Create the Main Function: We define a main function which creates an instance of the
Calculator class and runs the main event loop using root.mainloop().
2
Page

You might also like