Code With Python
39K subscribers
841 photos
24 videos
22 files
746 links
This channel delivers clear, practical content for developers, covering Python, Django, Data Structures, Algorithms, and DSA – perfect for learning, coding, and mastering key programming skills.
Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
Code With Python
Photo
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout, 
QLabel, QLineEdit, QPushButton)

class ConverterApp(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Temperature Converter")
self.setup_ui()

def setup_ui(self):
# Create widgets
self.celsius_input = QLineEdit()
self.fahrenheit_input = QLineEdit()
self.convert_btn = QPushButton("Convert")
self.result_label = QLabel("Enter temperature to convert")

# Set up layout
layout = QVBoxLayout()
layout.addWidget(QLabel("Celsius:"))
layout.addWidget(self.celsius_input)
layout.addWidget(QLabel("Fahrenheit:"))
layout.addWidget(self.fahrenheit_input)
layout.addWidget(self.convert_btn)
layout.addWidget(self.result_label)

# Connect button click
self.convert_btn.clicked.connect(self.convert)

self.setLayout(layout)

def convert(self):
try:
if self.celsius_input.text():
# Celsius to Fahrenheit
celsius = float(self.celsius_input.text())
fahrenheit = (celsius * 9/5) + 32
self.fahrenheit_input.setText(f"{fahrenheit:.2f}")
self.result_label.setText("Conversion complete!")
elif self.fahrenheit_input.text():
# Fahrenheit to Celsius
fahrenheit = float(self.fahrenheit_input.text())
celsius = (fahrenheit - 32) * 5/9
self.celsius_input.setText(f"{celsius:.2f}")
self.result_label.setText("Conversion complete!")
except ValueError:
self.result_label.setText("Please enter a valid number!")

if __name__ == "__main__":
app = QApplication([])
window = ConverterApp()
window.show()
app.exec_()


---

## 🔹 PyQt5 Designer Tool
Qt Designer lets you create UIs visually:

1. Launch Designer:
   pyqt5-tools designer

2. Design your interface (saves as .ui file)
3. Convert to Python code:
   pyuic5 input.ui -o output.py


Example Usage:
from PyQt5 import uic
class MyApp(QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi('design.ui', self) # Load UI file


---

## 🔹 Event Handling Basics
PyQt5 uses signals and slots for interactivity:

# Connecting signals to slots
button.clicked.connect(self.on_button_click)
checkbox.stateChanged.connect(self.on_checkbox_change)
line_edit.textChanged.connect(self.on_text_change)

# Example slot methods
def on_button_click(self):
print("Button clicked!")

def on_checkbox_change(self, state):
print("Checkbox state:", state)

def on_text_change(self, text):
print("Text changed to:", text)


---

## 🔹 Best Practices for Beginners
1. Organize code in classes/methods
2. Use layouts instead of absolute positioning
3. Name widgets clearly (e.g., self.login_btn)
4. Separate UI code from business logic
5. Handle errors gracefully in event handlers

---

### 📌 What's Next?
In Part 2, we'll cover:
➡️ Advanced Widgets (Tables, Trees, Tabs)
➡️ Custom Signals
➡️ Styling with QSS
➡️ Multiple Windows

#PyQt5Tutorial #GUIPython #LearnToCode 🚀

Practice Exercise:
1. Create a simple calculator app
2. Build a text editor with save/load buttons
3. Make a color picker that changes window background