0% found this document useful (0 votes)
27 views

Graphical Calculator

The document describes how to create a graphical user interface (GUI) calculator application in Python using Tkinter and object-oriented programming. It involves importing Tkinter, creating a Calculator class to represent the application, implementing button click handling methods, and creating a main function to run the application.

Uploaded by

Mahboob Riaz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Graphical Calculator

The document describes how to create a graphical user interface (GUI) calculator application in Python using Tkinter and object-oriented programming. It involves importing Tkinter, creating a Calculator class to represent the application, implementing button click handling methods, and creating a main function to run the application.

Uploaded by

Mahboob Riaz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lahore Grammar School

Paragon City

 Please remember this particular code will only be working if you have python libraries
installed

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

Bold, Underline and Italic text is the code it has been broken into three parts :

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):
if value == '=':
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().

By following these steps, you can create a basic GUI calculator using Python's Tkinter library
and object-oriented programming techniques.

You might also like