Spex3
    

Python for GUI Development: Tkinter & PyQt πŸ–₯️🐍


    

    

Python provides multiple libraries for Graphical User Interface (GUI) development, with Tkinter and PyQt being the most popular.

πŸ”Ή Tkinter – Built-in, lightweight, easy to use
πŸ”Ή PyQt – Feature-rich, powerful, modern UI

Let’s explore both! πŸš€

1️⃣ Tkinter – Built-in GUI Toolkit 🎨
πŸ”Ή Tkinter comes pre-installed with Python.
πŸ”Ή Great for simple desktop applications.
πŸ”Ή Provides basic widgets like buttons, labels, and text fields.

Basic Tkinter App

import tkinter as tk

# Create main window
root = tk.Tk()
root.title("Tkinter Example")

# Create a label
label = tk.Label(root, text="Hello, Tkinter!", font=("Arial", 16))
label.pack(pady=10)

# Create a button
def on_click():
label.config(text="Button Clicked!")

button = tk.Button(root, text="Click Me", command=on_click)
button.pack(pady=5)

# Run the application
root.mainloop()
βœ… Run the script – A simple window with a button will appear!

βœ… Key Features:

βœ” Comes with Python (no extra installation needed)
βœ” Lightweight and easy to learn
βœ” Ideal for small GUI apps

2️⃣ PyQt – Advanced GUI Framework πŸ—οΈ

πŸ”Ή PyQt is a powerful, professional GUI framework.
πŸ”Ή Based on Qt (a popular cross-platform GUI toolkit).
πŸ”Ή Supports modern UI design with advanced widgets.

Installing PyQt

pip install pyqt6
Basic PyQt App

from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout
import sys

class MyApp(QWidget):
def __init__(self):
super().__init__()

# Window properties
self.setWindowTitle("PyQt Example")
self.setGeometry(100, 100, 300, 200)

# Create layout
layout = QVBoxLayout()

# Create label
self.label = QLabel("Hello, PyQt!", self)
layout.addWidget(self.label)

# Create button
button = QPushButton("Click Me")
button.clicked.connect(self.on_click)
layout.addWidget(button)

self.setLayout(layout)

def on_click(self):
self.label.setText("Button Clicked!")

# Run the application
app = QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec())

βœ… Run the script – A modern, interactive GUI window will appear!

βœ… Key Features:
βœ” Supports advanced widgets & layouts
βœ” Cross-platform (Windows, macOS, Linux)
βœ” Ideal for large-scale GUI applications

3️⃣ Tkinter vs. PyQt – When to Choose What?

Feature Tkinter PyQt

Installation Built into Python Requires installation (pip install pyqt6)

Ease of Use Simple & beginner-friendly More complex, but powerful

Customization Basic styling Highly customizable UI

Use Case Small apps, utilities Professional applications


    Date: 2025-03-24 00:00:00.000000