{"id":34,"date":"2024-09-05T23:48:00","date_gmt":"2024-09-05T23:48:00","guid":{"rendered":"https:\/\/blog.tacoin.site\/?p=18"},"modified":"2024-09-05T23:48:00","modified_gmt":"2024-09-05T23:48:00","slug":"18","status":"publish","type":"post","link":"https:\/\/blog.tacoin.site\/index.php\/2024\/09\/05\/18\/","title":{"rendered":"\u4f7f\u7528PyQt\u5236\u4f5cGUI"},"content":{"rendered":"<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/blog.tacoin.site\/usr\/wp-content\/uploads\/2024\/09\/3550169507.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/blog.tacoin.site\/usr\/wp-content\/uploads\/2024\/09\/3550169507.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"GUI\u6548\u679c\" title=\"GUI\u6548\u679c\"><\/div><br \/>EHS\u5b9e\u9a8c\u5ba4\u9879\u76ee\u3002 \u4f7f\u7528PyQT\u5236\u4f5cGUI\u4ea4\u4e92\uff0c\u5176\u4ed6\u4f7f\u7528\u5d4c\u5165\u5f0f\u6a21\u5757\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5148\u914d\u7f6e\u73af\u5883\uff0c\u4e2a\u4eba\u559c\u6b22\u7528conda\u73af\u5883\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3apy310\u7684python3.10 \u3002<\/p>\n<pre><code>conda create python=3.10 -n py310 \n<\/code><\/pre>\n<p>\u8bb0\u5f97\u6fc0\u6d3b<\/p>\n<pre><code>conda activate py310\n<\/code><\/pre>\n<p>\u5b89\u88c5PyQT<\/p>\n<pre><code>pip3 install PyQt5 \n#\u76f8\u5173\u5de5\u5177\uff08\u53ef\u9009\uff09\npip install PyQt5-tools\n<\/code><\/pre>\n<p>\u6253\u5f00\u8bbe\u8ba1\uff1a<\/p>\n<pre><code>pyqt5-tools designer\n<\/code><\/pre>\n<p>&#8212;&#8211;\u7701\u7565&#8212;&#8212;-<br \/>\u4fdd\u5b58\u4e3a.ui\u6587\u4ef6\uff0c\u7136\u540e\u751f\u6210python\u6587\u4ef6<\/p>\n<p>A. \u52a8\u6001\u52a0\u8f7d .ui \u6587\u4ef6\uff08\u65e0\u9700\u8f6c\u6362\uff09<\/p>\n<pre><code>from PyQt5 import uic\nfrom PyQt5.QtWidgets import QApplication, QMainWindow\n\nclass MyApp(QMainWindow):\n    def __init__(self):\n        super().__init__()\n        uic.loadUi('yourfile.ui', self)  # \u76f4\u63a5\u52a0\u8f7d .ui \u6587\u4ef6\nif __name__ == '__main__':\n    app = QApplication([])\n    window = MyApp()\n    window.show()\n    app.exec_()\n\n<\/code><\/pre>\n<p>B. \u8f6c\u6362\u4e3apy\u6587\u4ef6<\/p>\n<pre><code>pyuic5 -o output.py input.ui\n<\/code><\/pre>\n<p>\u7136\u540e\u8fd0\u884c.<br \/><img decoding=\"async\" src=\"https:\/\/blog.tacoin.site\/usr\/wp-content\/uploads\/2024\/09\/3550169507.png\" alt=\"GUI\" title=\"GUI\"><\/p>\n<p>\u6253\u5305exe:<br \/>\u9996\u5148\u5b89\u88c5\u5e93<\/p>\n<pre><code>pip install pyinstaller\n<\/code><\/pre>\n<p>\u6253\u5305\uff0c\u5982\u679c\u6709py\u4ee5\u5916\u7684\u8d44\u6e90\u6587\u4ef6\u8981\u624b\u52a8\u6dfb\u52a0\u3002<\/p>\n<pre><code>#\u5982\u679c\u6ca1\u6709\u8d44\u6e90\u6587\u4ef6\u7684\u8bdd\npyinstaller --onefile --windowed main.py\n#\u6dfb\u52a0\u8d44\u6e90\u6587\u4ef6\u7248\u672c\npyinstaller --onefile --add-data &quot;image.png;.&quot; main.py\n<\/code><\/pre>\n<p>\u7136\u540e\u5c31\u53ef\u4ee5\u5728dist\u4e0b\u9762\u627e\u5230main.exe\u4e86\uff0c\u73b0\u5728\u662f\u9ed8\u8ba4\u56fe\u6807\uff0c\u8981\u6dfb\u52a0\u56fe\u6807\u7684\u8bdd <em>&#8211;icon xx.ico<\/em><\/p>\n<p>\u540e\u8bb0\uff1a<br \/>\u6253\u5305\u6587\u4ef6\u7684\u8bdd\u8def\u5f84\u4f1a\u6709\u95ee\u9898\uff0c\u8d34\u4e00\u4e2aGPT\u7684\u65b9\u6848\u4f9b\u53c2\u8003\uff1a<\/p>\n<pre><code># \u83b7\u53d6\u5f53\u524d\u8fd0\u884c\u6587\u4ef6\u7684\u76ee\u5f55\nbasedir = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))\npixmap_path = os.path.join(basedir, 'image.png')  # \u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>EHS\u5b9e\u9a8c\u5ba4\u9879\u76ee\u3002 \u4f7f\u7528PyQT\u5236\u4f5cGUI\u4ea4\u4e92\uff0c\u5176\u4ed6\u4f7f\u7528\u5d4c\u5165\u5f0f\u6a21\u5757\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[13,11,12],"class_list":["post-34","post","type-post","status-publish","format-standard","hentry","category-cs","tag-gui","tag-python","tag-qt"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/blog.tacoin.site\/index.php\/wp-json\/wp\/v2\/posts\/34","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.tacoin.site\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.tacoin.site\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.tacoin.site\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.tacoin.site\/index.php\/wp-json\/wp\/v2\/comments?post=34"}],"version-history":[{"count":0,"href":"https:\/\/blog.tacoin.site\/index.php\/wp-json\/wp\/v2\/posts\/34\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.tacoin.site\/index.php\/wp-json\/wp\/v2\/media?parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.tacoin.site\/index.php\/wp-json\/wp\/v2\/categories?post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.tacoin.site\/index.php\/wp-json\/wp\/v2\/tags?post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}