Python is an easy-to-learn and powerful programming language suitable for development in various fields. In Python, there are a variety of graphical user interface (GUI) libraries available that help developers create interactive desktop applications. This article will introduce some commonly used Python GUI libraries and provide specific code examples.
import tkinter as tk def on_button_click(): label.config(text="Hello, GUI!") window = tk.Tk() window.title("My GUI App") button = tk.Button(window, text="Click Me", command=on_button_click) button.pack() label = tk.Label(window, text="Welcome to my GUI app!") label.pack() window.mainloop()
from PyQt5 import QtWidgets class MyWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("My GUI App") self.button = QtWidgets.QPushButton("Click Me", self) self.button.clicked.connect(self.on_button_click) self.label = QtWidgets.QLabel("Welcome to my GUI app!", self) self.layout = QtWidgets.QVBoxLayout() self.layout.addWidget(self.button) self.layout.addWidget(self.label) self.central_widget = QtWidgets.QWidget() self.central_widget.setLayout(self.layout) self.setCentralWidget(self.central_widget) def on_button_click(self): self.label.setText("Hello, GUI!") app = QtWidgets.QApplication([]) window = MyWindow() window.show() app.exec_()
from PySide2 import QtWidgets class MyWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("My GUI App") self.button = QtWidgets.QPushButton("Click Me", self) self.button.clicked.connect(self.on_button_click) self.label = QtWidgets.QLabel("Welcome to my GUI app!", self) self.layout = QtWidgets.QVBoxLayout() self.layout.addWidget(self.button) self.layout.addWidget(self.label) self.central_widget = QtWidgets.QWidget() self.central_widget.setLayout(self.layout) self.setCentralWidget(self.central_widget) def on_button_click(self): self.label.setText("Hello, GUI!") app = QtWidgets.QApplication([]) window = MyWindow() window.show() app.exec_()
Summary:
In Python, there are a variety of GUI libraries to choose from, each with different Features and uses. The above introduces some commonly used GUI libraries, including Tkinter, PyQt and PySide, and provides specific code examples. Developers can choose the appropriate libraries based on their needs and preferences and use them to create beautiful and interactive desktop applications. The power and flexibility of these libraries make the development process more efficient and enjoyable.
The above is the detailed content of What are the options for GUI libraries in Python?. For more information, please follow other related articles on the PHP Chinese website!