{"id":1970,"date":"2023-06-29T12:19:19","date_gmt":"2023-06-29T04:19:19","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=1970"},"modified":"2023-06-29T12:19:19","modified_gmt":"2023-06-29T04:19:19","slug":"gui%e7%bc%96%e8%af%91%e7%94%9f%e6%88%90exe%e5%90%8e%e4%bd%9c%e5%9b%be%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2023\/1970\/","title":{"rendered":"gui\u7f16\u8bd1\u751f\u6210exe\u540e\u4f5c\u56fe\u6ce8\u610f\u4e8b\u9879"},"content":{"rendered":"

\u5982\u4f55\u4f7f\u7528GUI\u7f16\u8bd1\u5668\u751f\u6210.exe\u6587\u4ef6\u4ee5\u53ca\u4f5c\u56fe\u539f\u7406\u8be6\u7ec6\u4ecb\u7ecd<\/p>\n

\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08Graphical User Interface\uff0c\u7b80\u79f0GUI\uff09\u662f\u8ba1\u7b97\u673a\u5e94\u7528\u8f6f\u4ef6\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u65e8\u5728\u8ba9\u7528\u6237\u901a\u8fc7\u76f4\u89c2\u7684\u56fe\u5f62\u754c\u9762\u66f4\u65b9\u4fbf\u5730\u4e0e\u8ba1\u7b97\u673a\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\u3002\u5728\u8bb8\u591a\u7f16\u7a0b\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528GUI\u5e93\u6765\u521b\u5efa\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\uff0c\u7f16\u8bd1\u751f\u6210.exe\u6587\u4ef6\uff0c\u7136\u540e\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u8fd0\u884c\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u7684PyQt\u5e93\uff08\u4f5c\u4e3a\u793a\u4f8b\uff09\u751f\u6210.exe\u6587\u4ef6\uff0c\u5e76\u89e3\u91ca\u4e00\u4e9b\u57fa\u672c\u7684\u4f5c\u56fe\u539f\u7406\u3002<\/p>\n

**\u73af\u5883\u914d\u7f6e**<\/p>\n

\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u6b63\u786e\u914d\u7f6e\u4e86Python\u73af\u5883\u4ee5\u53ca\u5b89\u88c5\u4e86\u76f8\u5e94\u7684\u5e93\u3002\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n

1. \u5b89\u88c5Python\uff1a\u8bbf\u95eehttps:\/\/www.python.org\/downloads\/ \u4e0b\u8f7d\u5e76\u5b89\u88c5\u6700\u65b0\u7248\u672c\u7684Python\u3002<\/p>\n

2. \u5b89\u88c5PyQt\u4ee5\u53ca\u5176\u4ed6\u5fc5\u8981\u5e93\uff1a\u4f7f\u7528\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u5b89\u88c5\u6240\u9700\u5e93\uff1a<\/p>\n

“`shell<\/p>\n

pip install PyQt5 PyQt5-tools pyinstaller<\/p>\n

“`<\/p>\n

**\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684GUI\u5e94\u7528\u7a0b\u5e8f**<\/p>\n

1. \u65b0\u5efa\u4e00\u4e2aPython\u6587\u4ef6\uff0c\u4f8b\u5982\uff1a`main.py`\u3002<\/p>\n

2. \u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\uff0c\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684PyQt\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n

“`python<\/p>\n

import sys<\/p>\n

from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton<\/p>\n

class MainWindow(QMainWindow):<\/p>\n

def __init__(self):<\/p>\n

super().__init__()<\/p>\n

self.setWindowTitle(“\u4f5c\u56fe\u793a\u4f8b”)<\/p>\n

# \u6dfb\u52a0\u4e00\u4e2a\u7528\u4e8e\u4f5c\u56fe\u7684\u6309\u94ae<\/p>\n

self.draw_button = QPushButton(“\u7ed8\u5236”, self)<\/p>\n

ideajava\u5982\u4f55\u6253\u5305\u4e3aexe<\/a> self.draw_button.setGeometry(50, 50, 120, 30)<\/p>\n

self.draw_button.clicked.connect(self.draw)<\/p>\n

def draw(self):<\/p>\n

print(“\u7ed8\u5236\u56fe\u5f62”)<\/p>\n

if __name__ == “__mainxamarin\u751f\u6210exe<\/a>__”:<\/p>\n

app = QApplication(sys.argv)<\/p>\n

mainWindow = MainWindow()<\/p>\n

mainWindow.show()<\/p>\n

sys.exit(app.exec_())<\/p>\n

“`<\/p>\n

\u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u8fd0\u884c\u8fd9\u4e2a\u7b80\u5355\u7684\u56fe\u5f62\u754c\u9762\u7a0b\u5e8f\u5e76\u70b9\u51fb\u201c\u7ed8\u5236\u201d\u6309\u94ae\u3002\u6bcf\u6b21\u70b9\u51fb\u6309\u94ae\u65f6\uff0c\u63a7\u5236\u53f0\u6253\u5370\u201c\u7ed8\u5236\u56fe\u5f62\u201d\u5b57\u6837\u3002<\/p>\n

**\u7f16\u8bd1\u751f\u6210.exe\u6587\u4ef6**<\/p>\n

\u8981\u5c06Python\u6587\u4ef6\u7f16\u8bd1\u4e3a.exe\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528PyInstaller\u3002\u5728\u547d\u4ee4\u884c\u754c\u9762\uff0c\u5bfc\u822a\u5230\u5305\u542b`main.py`\u6587\u4ef6\u7684\u76ee\u5f55\uff0c\u7136\u540e\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n

“`shell<\/p>\n

pyinstaller –onefile –windowed main.py<\/p>\n

“`<\/p>\n

\u7f16\u8bd1\u6210\u529f\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u65b0\u521b\u5efa\u7684`dist`\u76ee\u5f55\u4e2d\u627e\u5230`main.exe`\uff08\u6216\u4f60\u8bbe\u7f6e\u7684\u5176\u4ed6\u6587\u4ef6\u540d\uff09\u3002<\/p>\n

**\u4f5c\u56fe\u539f\u7406**<\/p>\n

\u4f7f\u7528PyQt\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u7ed8\u56fe\u7a97\u53e3\uff0c\u7136\u540e\u5728\u7a97\u53e3\u4e0a\u7ed8\u5236\u5404\u79cd\u5f62\u72b6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7ed8\u56fe\u539f\u7406\u3002<\/p>\n

1. \u9996\u5148\uff0c\u5bfc\u5165\u6240\u9700\u7684\u5e93\uff1a<\/p>\n

“`python<\/p>\n

from PyQt5.QtCore import Qt<\/p>\n

from PyQt5.QtGui import QPainter, QPen<\/p>\n

“`<\/p>\n

2. \u5728`MainWindow`\u7c7b\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u51fd\u6570`paintEvent()`\uff0c\u6b64\u51fd\u6570\u5c06\u5728\u7a97\u53e3\u9700\u8981\u91cd\u7ed8\u65f6\u88ab\u8c03\u7528\u3002\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528`QPainter`\u548c`QPen`\u6765\u5b9a\u4e49\u4f5c\u56fe\u76f8\u5173\u7684\u7ed8\u56fe\u8bbe\u7f6e\u3002<\/p>\n

“`python<\/p>\n

def paintEvent(self, event):<\/p>\n

painter = QPainter(self)<\/p>\n

painter.setPen(QPen(Qt.black, 3))<\/p>\n

# \u5728\u6307\u5b9a\u7684\u5750\u6807\u4f4d\u7f6e\u7ed8\u5236\u4e00\u4e2a\u77e9\u5f62<\/p>\n

painter.drawRect(100, 100, 200, 200)<\/p>\n

# \u5728\u6307\u5b9a\u7684\u5750\u6807\u4f4d\u7f6e\u7ed8\u5236\u4e00\u4e2a\u692d\u5706\u5f62<\/p>\n

painter.drawEllipse(400, 100, 200, 200)<\/p>\n

“`<\/p>\n

3. \u786e\u4fdd\u5728`draw()`\u51fd\u6570\u4e2d\u8c03\u7528`update()`\u4ee5\u4fbf\u7a97\u53e3\u5728\u70b9\u51fb\u201c\u7ed8\u5236\u201d\u6309\u94ae\u540e\u7acb\u5373\u66f4\u65b0\u3002<\/p>\n

“`python<\/p>\n

def draw(self):<\/p>\n

<\/figure>\n<\/p>\n

self.update()<\/p>\n

“`<\/p>\n

\u73b0\u5728\uff0c\u8fd0\u884c\u7a0b\u5e8f\u5e76\u70b9\u51fb\u201c\u7ed8\u5236\u201d\u6309\u94ae\uff0c\u4f60\u5c06\u5728`MainWindow`\u7684\u7a97\u53e3\u4e0a\u770b\u5230\u4e00\u4e2a\u77e9\u5f62\u548c\u4e00\u4e2a\u692d\u5706\u5f62\u3002<\/p>\n

\u901a\u8fc7\u672c\u6559\u7a0b\uff0c\u4f60\u5e94\u8be5\u5b66\u4f1a\u4e86\u5982\u4f55\u4f7f\u7528GUI\u5e93\uff08\u5982PyQt\uff09\u6765\u521b\u5efa\u57fa\u672c\u7684\u5e94\u7528\u7a0b\u5e8f\u3001\u4f5c\u56fe\uff0c\u4ee5\u53ca\u5982\u4f55\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u6210.exe\u6587\u4ef6\u3002\u8fd9\u53ea\u662f\u4e00\u4e2a\u5165\u95e8\u7ea7\u6559\u7a0b\uff0cGUI\u7f16\u7a0b\u6709\u5f88\u591a\u9ad8\u7ea7\u529f\u80fd\u548c\u9009\u9879\u7b49\u7740\u4f60\u53bb\u63a2\u7d22\u3002\u795d\u4f60\u5b66\u4e60\u6109\u5feb\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5982\u4f55\u4f7f\u7528GUI\u7f16\u8bd1\u5668\u751f\u6210.exe\u6587\u4ef6\u4ee5\u53ca\u4f5c\u56fe\u539f\u7406\u8be6\u7ec6\u4ecb\u7ecd\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08Graphical User Interface\uff0c\u7b80\u79f0GUI\uff09\u662f\u8ba1\u7b97\u673a\u5e94\u7528\u8f6f\u4ef6\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u65e8\u5728\u8ba9\u7528\u6237\u901a\u8fc7\u76f4\u89c2\u7684\u56fe\u5f62\u754c\u9762\u66f4\u65b9\u4fbf\u5730\u4e0e<\/p>\n","protected":false},"author":24,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[4182,4181,65,3379,171],"class_list":{"0":"post-1970","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-zuoexe","7":"tag-vsexe","9":"tag-65","10":"tag-3379","11":"tag-171"},"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/1970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/24"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=1970"}],"version-history":[{"count":0,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/1970\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=1970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=1970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=1970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}