0% found this document useful (0 votes)
3 views3 pages

Python Calculator Guide

The document outlines two basic project ideas: a Calculator using Tkinter and a To-Do List App. It provides step-by-step instructions and sample code for creating a calculator with both Tkinter and PyQt5. The To-Do List App includes functionality for task management and optional GUI integration using Tkinter.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Python Calculator Guide

The document outlines two basic project ideas: a Calculator using Tkinter and a To-Do List App. It provides step-by-step instructions and sample code for creating a calculator with both Tkinter and PyQt5. The To-Do List App includes functionality for task management and optional GUI integration using Tkinter.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

🟢 BASIC PROJECTS

1. Calculator (Tkinter)

Steps:

1. Import Tkinter.
2. Create root window.
3. Add entry widget for input/output.
4. Add buttons for digits and operations.
5. Define functions for operations (add, subtract, etc).
6. Bind buttons to corresponding functions.
7. Run main loop.

Sample Code:

import tkinter as tk

def click(event):
text = event.widget.cget("text")
if text == "=":
try:
result = eval(entry.get())
entry.delete(0, tk.END)
entry.insert(tk.END, result)
except:
entry.delete(0, tk.END)
entry.insert(tk.END, "Error")
elif text == "C":
entry.delete(0, tk.END)
else:
entry.insert(tk.END, text)

root = tk.Tk()
root.title("Tkinter Calculator")
entry = tk.Entry(root, font="Arial 20")
entry.pack(fill="both", ipadx=8)

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

for row in buttons:


frame = tk.Frame(root)
for btn in row:

1
b = tk.Button(frame, text=btn, font="Arial 15", padx=10, pady=10)
b.pack(side="left", expand=True, fill="both")
b.bind("<Button-1>", click)
frame.pack(expand=True, fill="both")

root.mainloop()

2. To-Do List App

Steps:

1. Use list to store tasks.


2. Add CLI input options to add/remove/show tasks.
3. Save tasks to a text file.
4. Load tasks from file on start.
5. (Optional) Add Tkinter GUI with listbox and buttons.

...

✅ PyQt5 Calculator Code

from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QPushButton,


QLineEdit, QVBoxLayout

class Calculator(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('PyQt5 Calculator')
self.create_ui()

def create_ui(self):
self.layout = QVBoxLayout()
self.display = QLineEdit()
self.layout.addWidget(self.display)

grid = QGridLayout()
buttons = [
('7', 0, 0), ('8', 0, 1), ('9', 0, 2), ('/', 0, 3),
('4', 1, 0), ('5', 1, 1), ('6', 1, 2), ('*', 1, 3),
('1', 2, 0), ('2', 2, 1), ('3', 2, 2), ('-', 2, 3),
('C', 3, 0), ('0', 3, 1), ('=', 3, 2), ('+', 3, 3),
]

for (text, row, col) in buttons:


btn = QPushButton(text)
btn.clicked.connect(self.on_button_click)
grid.addWidget(btn, row, col)

self.layout.addLayout(grid)

2
self.setLayout(self.layout)

def on_button_click(self):
sender = self.sender()
text = sender.text()

if text == 'C':
self.display.clear()
elif text == '=':
try:
result = eval(self.display.text())
self.display.setText(str(result))
except:
self.display.setText("Error")
else:
self.display.setText(self.display.text() + text)

if __name__ == '__main__':
app = QApplication([])
calc = Calculator()
calc.show()
app.exec_()

Let me know which one you'd like the full source code for or want to explore in-depth!

You might also like