{"id":1696,"date":"2023-06-26T12:19:24","date_gmt":"2023-06-26T04:19:24","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=1696"},"modified":"2023-06-26T12:19:24","modified_gmt":"2023-06-26T04:19:24","slug":"gui%e6%80%8e%e4%b9%88%e5%88%b6%e4%bd%9cexe%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2023\/1696\/","title":{"rendered":"gui\u600e\u4e48\u5236\u4f5cexe\uff1f"},"content":{"rendered":"

\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5c06\u4e3a\u4f60qt \u505a\u597d\u7684\u9879\u76ee\u5982\u4f55\u751f\u6210exe<\/a>\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u548c\u5176\u76f8\u5173\u5e93\u6765\u5236\u4f5c\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u5c06\u5176\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff08.exe\uff09\u3002<\/p>\n

\u6211\u4eec\u5c06\u4f7f\u7528Python\u7684Tkinter\u5e93\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u7a97\u4f53\u5e94\u7528\uff0c\u5e76\u4f7f\u7528PyInstaller\u5de5\u5177\u5c06\u7a0b\u5e8f\u6253\u5305\u4e3aEXE\u6587\u4ef6\u3002<\/p>\n

### 1. \u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u548c\u5de5\u5177<\/p>\n

\u6211\u4eec\u9996\u5148\u9700\u8981\u5b89\u88c5Python\u3002\u4f60\u53ef\u4ee5\u4ecePython\u5b98\u65b9\u7f51\u7ad9(https:\/\/www.python.org\/downloads\/)\u83b7\u53d6\u6700\u65b0\u7248\u672c\u7684Python\u3002<\/p>\n

\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Tkinter\u5e93\uff0c\u5b83\u5c06\u7528\u4e8e\u521b\u5efaGUI\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5b83\u5df2\u7ecf\u968fPython\u4e00\u8d77\u5b89\u88c5\u4e86\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u4f7f\u7528PyInstaller\u5c06Python\u811a\u672c\u6253\u5305\u6210EXE\u6587\u4ef6\u3002<\/p>\n

\u5b89\u88c5PyInstaller\u7684\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n

“`bash<\/p>\n

pip install pyinstaller<\/p>\n

“`<\/p>\n

### 2. \u7f16\u5199GUI\u5e94\u7528\u7a0b\u5e8f<\/p>\n

\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u4f7f\u7528Python\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684GUI\u7a0b\u5e8f\u3002\u65b0\u5efa\u4e00\u4e2a\u540d\u4e3a`app.py`\u7684\u6587\u4ef6\uff0c\u7136\u540e\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n

“`python<\/p>\n

import tkinter as tk<\/p>\n

def on_click():<\/p>\n

label.config(text=”Hello, ” + entry.get())<\/p>\n

app = tk.Tk()<\/p>\n

app.title(“My GUI App”)<\/p>\n

label = tk.Label(app, text=”Enter your name:”)<\/p>\n

label.pack()<\/p>\n

entry = tk.Entry(app)<\/p>\n

entry.pack()<\/p>\n

button = tk.Button(app, text=”Submit”, command=on_click)<\/p>\n

button.pack()<\/p>\n

app.mainloop()<\/p>\n

“`<\/p>\n

\u8fd9\u4e2a\u7b80\u5355\u7684Python\u7a0b\u5e8f\u521b\u5efa\u4e86\u4e00\u4e2a\u7a97\u53e3\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u5355\u884c\u6587\u672c\u8f93\u5165\u6846\u3001\u4e00\u4e2a\u6807\u7b7e\u548c\u4e00\u4e2a\u6309\u94ae\u3002\u5f53\u7528\u6237\u5728\u6587\u672c\u6846\u4e2d\u8f93\u5165\u4fe1\u606f\u5e76\u70b9\u51fb”\u63d0\u4ea4”\u6309\u94ae\u65f6\uff0c\u7a0b\u5e8f\u5c06\u5728\u6807\u7b7e\u4e0a\u663e\u793a\u76f8\u5e94\u7684\u95ee\u5019\u8bed\u3002<\/p>\n

### 3. \u5c06Python\u811a\u672c\u8f6c\u6362\u4e3aEXE\u6587\u4ef6<\/p>\n

\u73b0\u5728\u6211\u4eec\u51c6\u5907\u5c06`ap\u7f51\u9875\u6253\u5305exe\u7a0b\u5e8f<\/a>p.py`\u6587\u4ef6\u8f6c\u6362\u4e3aEXE\u6587\u4ef6\u3002\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\uff0c\u7136\u540e\u8f93\u5165\u4ee5\u4e0b\u547d<\/p>\n

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

\u4ee4\uff1a<\/p>\n

“`bash<\/p>\n

pyinstaller –onefile –noconsole app.py<\/p>\n

“`<\/p>\n

\u8fd9\u4e2a\u547d\u4ee4\u544a\u8bc9PyInstaller\u4ee5\u5355\u6587\u4ef6\u6a21\u5f0f\uff08\u4e0d\u4f9d\u8d56\u5176\u4ed6\u5e93\u6587\u4ef6\uff09\u5c06Python\u811a\u672c\u6253\u5305\uff0c\u5e76\u4e14\u4e0d\u8981\u663e\u793a\u63a7\u5236\u53f0\u7a97\u53e3\u3002<\/p>\n

\u7a0d\u7b49\u7247\u523b\uff0cPyInstaller\u5c06\u751f\u6210\u4e00\u4e2a\u540d\u4e3a`app.exe`\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u5728`dist`\u76ee\u5f55\u4e0b\u627e\u5230\u5b83\u3002<\/p>\n

### 4. \u6d4b\u8bd5EXE\u6587\u4ef6<\/p>\n

\u53cc\u51fb`dist`\u76ee\u5f55\u4e0b\u7684`app.exe`\u6587\u4ef6\uff0c\u4f60\u5c06\u770b\u5230\u4e0e\u6211\u4eec\u7684Python\u7a0b\u5e8f\u76f8\u540c\u7684GUI\u7a97\u53e3\u3002\u73b0\u5728\uff0c\u4f60\u5df2\u7ecf\u6210\u529f\u5c06Python GUI\u7a0b\u5e8f\u6253\u5305\u6210EXE\u6587\u4ef6\u3002<\/p>\n

\u6ce8\u610f\uff1a\u751f\u6210\u7684EXE\u6587\u4ef6\u53ef\u80fd\u4f1a\u88ab\u4e00\u4e9b\u6740\u6bd2\u8f6f\u4ef6\u8bef\u62a5\u4e3a\u75c5\u6bd2\u3002\u8fd9\u662f\u4e00\u4e2a\u4f17\u6240\u5468\u77e5\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u5c06EXE\u6587\u4ef6\u6dfb\u52a0\u5230\u6740\u6bd2\u8f6f\u4ef6\u7684\u767d\u540d\u5355\u4e2d\uff0c\u4ee5\u907f\u514d\u8bef\u62a5\u3002<\/p>\n

\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u4f60\u66f4\u597d\u5730\u4e86\u89e3\u5982\u4f55\u5236\u4f5cGUI\u5e94\u7528\u7a0b\u5e8f\u5e76\u5c06\u5176\u6253\u5305\u6210EXE\u6587\u4ef6\u3002\u7ee7\u7eed\u6df1\u5165\u7814\u7a76Python\u548cGUI\u7f16\u7a0b\uff0c\u4f60\u5c06\u80fd\u591f\u521b\u5efa\u66f4\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5c06\u4e3a\u4f60\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u548c\u5176\u76f8\u5173\u5e93\u6765\u5236\u4f5c\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u5c06\u5176\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff08.exe\uff09\u3002\u6211\u4eec\u5c06\u4f7f\u7528Python\u7684Tkinter\u5e93\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[3629,3628,14,171,398],"class_list":["post-1696","post","type-post","status-publish","format-standard","hentry","category-zuoexe","tag-pythonexe","tag-app","tag-14","tag-171","tag-398"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/1696","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\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=1696"}],"version-history":[{"count":0,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/1696\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=1696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=1696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=1696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}