🚀Learning Python GUI Development from Scratch
Hello everyone! Today we are going to discuss a particularly practical topic—developing software with a graphical user interface using Python.
💡 The user interaction interface of a program, known in English as UI (User Interface). When an application’s UI becomes complex, using the command line can be inconvenient for users, and that’s when we need a graphical interface.
🎯Why Do We Need a Graphical User Interface (GUI)?
First, let me ask a question:“What is the difference between command line programs and graphical interface programs?”
For example🌰:
- Command Line Program: For instance, if you wrote a “batch file renaming tool,” you would need to enter commands in a dark terminal (like
<span>python rename.py --folder D:\files --prefix new_</span>), and remember various parameter formats. If you input the parameters incorrectly, you can only look at the error messages and troubleshoot yourself. - Graphical Interface Program: The same functionality can be made into a windowed software—drag a folder selection box on the left, display the file list in the middle, and have a “Start Renaming” button in the lower right corner, allowing users to operate with just a click of the mouse, even if they have no coding knowledge.
✨ Conclusion: When your program needs to be used by non-technical users (like family or colleagues), or when the functionality is complex, a graphical interface (GUI) can significantly enhance the user experience!
🛠️Comparison of the Three Main GUI Development Options in Python
In the Python ecosystem, there are many tools for creating graphical interfaces, but the mainstream choices suitable for most people are actually three types:Tkinter, wxPython, PySide/PyQt. Below, we will compare their characteristics in simple terms to help you choose quickly!
| Option | Overview | Advantages | Disadvantages | Suitable Scenarios |
|---|---|---|---|---|
| Tkinter | Python’s official standard library | • No installation required, comes with Python• Lightweight• Stable and reliable | • Fewer widgets• Outdated interface | Simple small tools |
| wxPython | Based on wxWidgets | • Rich widgets• Native experience | • Average stability• Limited documentation | Tools with more functionality |
| PySide/PyQt | Based on the Qt framework | • Powerful features• Good cross-platform support• Comprehensive documentation | • Large library size• Steeper learning curve | Professional-grade software |
📦Installation Guide
Installing PySide6
pip install pyside6
Or use a domestic mirror for faster access:
pip install pyside6 -i https://pypi.tuna.tsinghua.edu.cn/simple
Installing PyQt6
pip install pyqt6-tools
🌟Why Recommend PySide6?
PySide6 is currently the most balanced choice! It is the Python binding maintained by the Qt official, powerful and well-documented, suitable for developing everything from simple tools to professional software.
🚀Your First PySide6 Program
import sysfrom PySide6.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout
# Create a window classclass MyWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle("My First GUI Program") # Set window title self.setGeometry(300, 300, 400, 200) # Set window position and size (x,y,width,height)
# Create a label (display text) label = QLabel("Hello! This is a GUI made with PySide6!", self) label.setStyleSheet("font-size: 16px; color: blue;") # Set font and color
# Create a button button = QPushButton("Click me to try", self) button.clicked.connect(self.on_button_click) # Call the function below when the button is clicked
# Use a layout manager to arrange (vertically) the label and button layout = QVBoxLayout() layout.addWidget(label) layout.addWidget(button) self.setLayout(layout)
def on_button_click(self): print("Button clicked!") # Console output (can be changed to a popup message)
# Start the programif __name__ == "__main__": app = QApplication(sys.argv) # Create application instance window = MyWindow() # Create window object window.show() # Show window sys.exit(app.exec()) # Run the program
💡 Save it as a .py file and run it, and you will see a window with text and a button! Clicking the button will print information to the console.