{"id":1178627,"date":"2025-01-15T18:13:22","date_gmt":"2025-01-15T10:13:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1178627.html"},"modified":"2025-01-15T18:13:26","modified_gmt":"2025-01-15T10:13:26","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8pyqt5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1178627.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u7528PyQt5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25113231\/564728b6-c007-40ff-a3df-f137a8693fda.webp\" alt=\"python\u5982\u4f55\u4f7f\u7528PyQt5\" \/><\/p>\n<p><p> <strong>Python \u4f7f\u7528 PyQt5 \u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5PyQt5\u3001\u521b\u5efa\u57fa\u672c\u7684PyQt5\u5e94\u7528\u7a0b\u5e8f\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u3001\u8fde\u63a5\u4fe1\u53f7\u548c\u69fd\u3001\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u3002<\/strong>\u5176\u4e2d\uff0c\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65\uff0c\u5b83\u51b3\u5b9a\u4e86\u5e94\u7528\u7a0b\u5e8f\u7684\u6574\u4f53\u4f53\u9a8c\u548c\u529f\u80fd\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u4e2a\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5PyQt5<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528PyQt5\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyQt5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u811a\u672c\u6765\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QWidget<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<p>window = QWidget()<\/p>\n<p>window.show()<\/p>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u770b\u5230\u4e00\u4e2a\u7a7a\u767d\u7684\u7a97\u53e3\u5f39\u51fa\uff0c\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u57fa\u672c\u7684PyQt5\u5e94\u7528\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aPyQt5\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u51e0\u4e2a\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/strong><\/li>\n<li><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61<\/strong><\/li>\n<li><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/li>\n<li><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f<\/strong><\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QM<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nWindow<\/p>\n<p>class MainWindow(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&#39;PyQt5 Example&#39;)<\/p>\n<p>        self.setGeometry(100, 100, 600, 400)<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u754c\u9762\u8bbe\u8ba1\u662fPyQt5\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u4e2d\u6700\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002\u53ef\u4ee5\u4f7f\u7528\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\u6765\u8bbe\u8ba1\u7528\u6237\u754c\u9762\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u4ee3\u7801\u76f4\u63a5\u8bbe\u8ba1\u754c\u9762<\/strong><\/li>\n<li><strong>\u4f7f\u7528Qt Designer\u5de5\u5177\u8bbe\u8ba1\u754c\u9762<\/strong><\/li>\n<\/ol>\n<p><h4>\u4f7f\u7528\u4ee3\u7801\u76f4\u63a5\u8bbe\u8ba1\u754c\u9762<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee3\u7801\u624b\u52a8\u521b\u5efa\u548c\u5e03\u5c40\u5404\u79cd\u63a7\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget<\/p>\n<p>class MainWindow(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&#39;PyQt5 Example&#39;)<\/p>\n<p>        self.setGeometry(100, 100, 600, 400)<\/p>\n<p>        layout = QVBoxLayout()<\/p>\n<p>        button1 = QPushButton(&#39;Button 1&#39;)<\/p>\n<p>        button2 = QPushButton(&#39;Button 2&#39;)<\/p>\n<p>        layout.addWidget(button1)<\/p>\n<p>        layout.addWidget(button2)<\/p>\n<p>        container = QWidget()<\/p>\n<p>        container.setLayout(layout)<\/p>\n<p>        self.setCentralWidget(container)<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528Qt Designer\u5de5\u5177\u8bbe\u8ba1\u754c\u9762<\/h4>\n<\/p>\n<p><p>Qt Designer\u662f\u4e00\u6b3e\u65b9\u4fbf\u7684\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u53ef\u4ee5\u62d6\u653e\u63a7\u4ef6\u6765\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u3002\u8bbe\u8ba1\u597d\u754c\u9762\u540e\uff0c\u53ef\u4ee5\u5c06\u5176\u4fdd\u5b58\u4e3a<code>.ui<\/code>\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>pyuic5<\/code>\u5de5\u5177\u5c06\u5176\u8f6c\u6362\u4e3aPython\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528Qt Designer\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u754c\u9762\u5e76\u4fdd\u5b58\u4e3a<code>example.ui<\/code>\uff0c\u7136\u540e\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5c06\u5176\u8f6c\u6362\u4e3aPython\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyuic5 -o example_ui.py example.ui<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f6c\u6362\u540e\u7684\u4ee3\u7801\u53ef\u4ee5\u76f4\u63a5\u5bfc\u5165\u5e76\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QMainWindow<\/p>\n<p>from example_ui import Ui_MainWindow<\/p>\n<p>class MainWindow(QMainWindow, Ui_MainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setupUi(self)<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8fde\u63a5\u4fe1\u53f7\u548c\u69fd<\/h3>\n<\/p>\n<p><p>PyQt5\u4f7f\u7528\u4fe1\u53f7\u548c\u69fd\u673a\u5236\u6765\u5904\u7406\u4e8b\u4ef6\u3002\u4fe1\u53f7\u662f\u4e8b\u4ef6\u89e6\u53d1\u65f6\u53d1\u51fa\u7684\u6d88\u606f\uff0c\u69fd\u662f\u5904\u7406\u8fd9\u4e9b\u6d88\u606f\u7684\u51fd\u6570\u3002\u53ef\u4ee5\u901a\u8fc7<code>connect<\/code>\u65b9\u6cd5\u5c06\u4fe1\u53f7\u548c\u69fd\u8fde\u63a5\u8d77\u6765\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton<\/p>\n<p>class MainWindow(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&#39;PyQt5 Example&#39;)<\/p>\n<p>        self.setGeometry(100, 100, 600, 400)<\/p>\n<p>        button = QPushButton(&#39;Click Me&#39;, self)<\/p>\n<p>        button.clicked.connect(self.on_button_clicked)<\/p>\n<p>        self.setCentralWidget(button)<\/p>\n<p>    def on_button_clicked(self):<\/p>\n<p>        print(&#39;Button clicked!&#39;)<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u5e76\u8bbe\u8ba1\u597d\u7528\u6237\u754c\u9762\u540e\uff0c\u53ef\u4ee5\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">app = QApplication(sys.argv)<\/p>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u529f\u80fd\u5b8c\u6574\u7684PyQt5\u5e94\u7528\u7a0b\u5e8f\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u6269\u5c55\u548c\u5b8c\u5584\u5e94\u7528\u7a0b\u5e8f\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5e38\u7528\u63a7\u4ef6\u548c\u5e03\u5c40<\/h3>\n<\/p>\n<p><h4>QPushButton\uff08\u6309\u94ae\uff09<\/h4>\n<\/p>\n<p><p>QPushButton\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u6309\u94ae\u63a7\u4ef6\uff0c\u53ef\u4ee5\u7528\u4e8e\u89e6\u53d1\u4e8b\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton<\/p>\n<p>class MainWindow(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&#39;QPushButton Example&#39;)<\/p>\n<p>        self.setGeometry(100, 100, 300, 200)<\/p>\n<p>        button = QPushButton(&#39;Click Me&#39;, self)<\/p>\n<p>        button.clicked.connect(self.on_button_clicked)<\/p>\n<p>        button.setGeometry(100, 80, 100, 30)<\/p>\n<p>    def on_button_clicked(self):<\/p>\n<p>        print(&#39;Button clicked!&#39;)<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>QLabel\uff08\u6807\u7b7e\uff09<\/h4>\n<\/p>\n<p><p>QLabel\u7528\u4e8e\u663e\u793a\u6587\u672c\u6216\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel<\/p>\n<p>class MainWindow(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&#39;QLabel Example&#39;)<\/p>\n<p>        self.setGeometry(100, 100, 300, 200)<\/p>\n<p>        label = QLabel(&#39;Hello, PyQt5!&#39;, self)<\/p>\n<p>        label.setGeometry(100, 80, 100, 30)<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>QLineEdit\uff08\u5355\u884c\u6587\u672c\u6846\uff09<\/h4>\n<\/p>\n<p><p>QLineEdit\u7528\u4e8e\u8f93\u5165\u548c\u7f16\u8f91\u5355\u884c\u6587\u672c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QMainWindow, QLineEdit<\/p>\n<p>class MainWindow(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&#39;QLineEdit Example&#39;)<\/p>\n<p>        self.setGeometry(100, 100, 300, 200)<\/p>\n<p>        line_edit = QLineEdit(self)<\/p>\n<p>        line_edit.setGeometry(100, 80, 100, 30)<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>QVBoxLayout\uff08\u5782\u76f4\u5e03\u5c40\uff09<\/h4>\n<\/p>\n<p><p>QVBoxLayout\u7528\u4e8e\u5782\u76f4\u6392\u5217\u63a7\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QWidget<\/p>\n<p>class MainWindow(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&#39;QVBoxLayout Example&#39;)<\/p>\n<p>        self.setGeometry(100, 100, 300, 200)<\/p>\n<p>        layout = QVBoxLayout()<\/p>\n<p>        button1 = QPushButton(&#39;Button 1&#39;)<\/p>\n<p>        button2 = QPushButton(&#39;Button 2&#39;)<\/p>\n<p>        layout.addWidget(button1)<\/p>\n<p>        layout.addWidget(button2)<\/p>\n<p>        container = QWidget()<\/p>\n<p>        container.setLayout(layout)<\/p>\n<p>        self.setCentralWidget(container)<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u8fdb\u9636\u529f\u80fd<\/h3>\n<\/p>\n<p><h4>\u4f7f\u7528QTimer\u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>QTimer\u662f\u4e00\u4e2a\u7528\u4e8e\u5b9a\u65f6\u4efb\u52a1\u7684\u7c7b\u3002\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u65f6\u95f4\u95f4\u9694\uff0c\u5e76\u5728\u65f6\u95f4\u95f4\u9694\u5230\u8fbe\u65f6\u89e6\u53d1\u4fe1\u53f7\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtCore import QTimer<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel<\/p>\n<p>class MainWindow(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&#39;QTimer Example&#39;)<\/p>\n<p>        self.setGeometry(100, 100, 300, 200)<\/p>\n<p>        self.label = QLabel(&#39;0&#39;, self)<\/p>\n<p>        self.label.setGeometry(140, 80, 20, 30)<\/p>\n<p>        self.timer = QTimer(self)<\/p>\n<p>        self.timer.timeout.connect(self.update_label)<\/p>\n<p>        self.timer.start(1000)<\/p>\n<p>        self.counter = 0<\/p>\n<p>    def update_label(self):<\/p>\n<p>        self.counter += 1<\/p>\n<p>        self.label.setText(str(self.counter))<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528QFileDialog\u5b9e\u73b0\u6587\u4ef6\u5bf9\u8bdd\u6846<\/h4>\n<\/p>\n<p><p>QFileDialog\u7528\u4e8e\u6253\u5f00\u548c\u4fdd\u5b58\u6587\u4ef6\u5bf9\u8bdd\u6846\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QPushButton<\/p>\n<p>class MainWindow(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&#39;QFileDialog Example&#39;)<\/p>\n<p>        self.setGeometry(100, 100, 300, 200)<\/p>\n<p>        button = QPushButton(&#39;Open File&#39;, self)<\/p>\n<p>        button.setGeometry(100, 80, 100, 30)<\/p>\n<p>        button.clicked.connect(self.open_file_dialog)<\/p>\n<p>    def open_file_dialog(self):<\/p>\n<p>        options = QFileDialog.Options()<\/p>\n<p>        options |= QFileDialog.DontUseNativeDialog<\/p>\n<p>        file, _ = QFileDialog.getOpenFileName(self, &quot;Open File&quot;, &quot;&quot;, &quot;All Files (*);;Python Files (*.py)&quot;, options=options)<\/p>\n<p>        if file:<\/p>\n<p>            print(file)<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528QThread\u5b9e\u73b0\u591a\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><p>QThread\u7528\u4e8e\u591a\u7ebf\u7a0b\u7f16\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtCore import QThread, pyqtSignal<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel<\/p>\n<p>class Worker(QThread):<\/p>\n<p>    update_signal = pyqtSignal(int)<\/p>\n<p>    def run(self):<\/p>\n<p>        for i in range(5):<\/p>\n<p>            self.sleep(1)<\/p>\n<p>            self.update_signal.emit(i + 1)<\/p>\n<p>class MainWindow(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.setWindowTitle(&#39;QThread Example&#39;)<\/p>\n<p>        self.setGeometry(100, 100, 300, 200)<\/p>\n<p>        self.label = QLabel(&#39;0&#39;, self)<\/p>\n<p>        self.label.setGeometry(140, 80, 20, 30)<\/p>\n<p>        self.worker = Worker()<\/p>\n<p>        self.worker.update_signal.connect(self.update_label)<\/p>\n<p>        self.worker.start()<\/p>\n<p>    def update_label(self, value):<\/p>\n<p>        self.label.setText(str(value))<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<p>window = MainWindow()<\/p>\n<p>window.show()<\/p>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u90e8\u7f72\u548c\u6253\u5305<\/h3>\n<\/p>\n<p><h4>\u4f7f\u7528PyInstaller\u6253\u5305<\/h4>\n<\/p>\n<p><p>PyInstaller\u662f\u4e00\u4e2a\u7528\u4e8e\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u72ec\u7acb\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u6253\u5305PyQt5\u5e94\u7528\u7a0b\u5e8f\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5PyInstaller<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install pyinstaller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6253\u5305\u5e94\u7528\u7a0b\u5e8f<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --windowed your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u547d\u4ee4\u4f1a\u751f\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728\u76ee\u6807\u7cfb\u7edf\u4e0a\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u60a8\u5e94\u8be5\u5df2\u7ecf\u638c\u63e1\u4e86\u4f7f\u7528PyQt5\u521b\u5efaPython\u5e94\u7528\u7a0b\u5e8f\u7684\u57fa\u672c\u6b65\u9aa4\u548c\u6280\u5de7\u3002<strong>\u5b89\u88c5PyQt5\u3001\u521b\u5efa\u57fa\u672c\u7684PyQt5\u5e94\u7528\u7a0b\u5e8f\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u3001\u8fde\u63a5\u4fe1\u53f7\u548c\u69fd\u3001\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f<\/strong>\u662f\u57fa\u672c\u6d41\u7a0b\uff0c\u800c\u4f7f\u7528<strong>\u5e38\u7528\u63a7\u4ef6\u548c\u5e03\u5c40\u3001\u8fdb\u9636\u529f\u80fd\u5982QTimer\u3001QFileDialog\u3001QThread\u3001\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4ee5\u53ca\u6253\u5305\u90e8\u7f72<\/strong>\u5219\u53ef\u4ee5\u63d0\u5347\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u529f\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u3002PyQt5\u662f\u4e00\u4e2a\u5f3a\u5927\u4e14\u7075\u6d3b\u7684GUI\u5e93\uff0c\u638c\u63e1\u4e86\u8fd9\u4e9b\u57fa\u672c\u77e5\u8bc6\u540e\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u4e00\u6b65\u6269\u5c55\u548c\u5b8c\u5584\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5b89\u88c5PyQt5\u4ee5\u4fbf\u5728Python\u4e2d\u4f7f\u7528\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u4f7f\u7528PyQt5\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u4f60\u7684\u5f00\u53d1\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86PyQt5\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u6765\u5b89\u88c5\u3002\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a  <\/p>\n<pre><code>pip install PyQt5\n<\/code><\/pre>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165PyQt5\u6a21\u5757\u6765\u5f00\u59cb\u4f7f\u7528\u3002<\/p>\n<p><strong>PyQt5\u9002\u5408\u7528\u4e8e\u54ea\u4e9b\u7c7b\u578b\u7684\u5e94\u7528\u5f00\u53d1\uff1f<\/strong><br \/>PyQt5\u662f\u4e00\u4e2a\u5f3a\u5927\u7684GUI\uff08\u56fe\u5f62\u7528\u6237\u754c\u9762\uff09\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u5f00\u53d1\u5404\u79cd\u7c7b\u578b\u7684\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002\u65e0\u8bba\u662f\u6570\u636e\u53ef\u89c6\u5316\u5de5\u5177\u3001\u56fe\u5f62\u7f16\u8f91\u8f6f\u4ef6\uff0c\u8fd8\u662f\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf\uff0cPyQt5\u90fd\u80fd\u591f\u63d0\u4f9b\u4e30\u5bcc\u7684\u7ec4\u4ef6\u548c\u7075\u6d3b\u7684\u5e03\u5c40\u9009\u9879\u3002\u6b64\u5916\uff0cPyQt5\u8fd8\u652f\u6301\u8de8\u5e73\u53f0\u5f00\u53d1\uff0c\u53ef\u4ee5\u5728Windows\u3001macOS\u548cLinux\u4e0a\u8fd0\u884c\u3002<\/p>\n<p><strong>\u5728PyQt5\u4e2d\u5982\u4f55\u521b\u5efa\u7b80\u5355\u7684\u7a97\u53e3\u5e94\u7528\u7a0b\u5e8f\uff1f<\/strong><br \/>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7a97\u53e3\u5e94\u7528\u7a0b\u5e8f\u975e\u5e38\u5bb9\u6613\u3002\u4f60\u53ea\u9700\u5bfc\u5165PyQt5\u7684\u6838\u5fc3\u6a21\u5757\u548cQtWidgets\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5b9e\u4f8b\u548c\u4e00\u4e2a\u7a97\u53e3\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import sys\nfrom PyQt5.QtWidgets import QApplication, QWidget\n\napp = QApplication(sys.argv)\nwindow = QWidget()\nwindow.setWindowTitle(&#39;Hello PyQt5&#39;)\nwindow.setGeometry(100, 100, 600, 400)\nwindow.show()\nsys.exit(app.exec_())\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4f1a\u521b\u5efa\u4e00\u4e2a\u6807\u9898\u4e3a\u201cHello PyQt5\u201d\u7684\u7a97\u53e3\uff0c\u5e76\u8bbe\u7f6e\u5176\u5927\u5c0f\u3002\u8fd0\u884c\u540e\uff0c\u4f60\u5c06\u770b\u5230\u4e00\u4e2a\u7b80\u5355\u7684\u5e94\u7528\u7a97\u53e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u4f7f\u7528 PyQt5 \u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5PyQt5\u3001\u521b\u5efa\u57fa\u672c\u7684PyQt5\u5e94\u7528\u7a0b\u5e8f\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u3001\u8fde\u63a5\u4fe1 [&hellip;]","protected":false},"author":3,"featured_media":1178639,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178627"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1178627"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178627\/revisions"}],"predecessor-version":[{"id":1178640,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178627\/revisions\/1178640"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1178639"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1178627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1178627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1178627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}