{"id":13411,"date":"2023-12-25T15:43:59","date_gmt":"2023-12-25T07:43:59","guid":{"rendered":"https:\/\/www.yimenyun.cn\/emen\/?p=13411"},"modified":"2023-12-25T15:43:59","modified_gmt":"2023-12-25T07:43:59","slug":"tkinter%e6%89%93%e5%8c%85%e6%88%90exe%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%e7%9a%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.yimenyun.cn\/emen\/13411\/","title":{"rendered":"tkinter\u6253\u5305\u6210exe\u600e\u4e48\u5b9e\u73b0\u7684\uff1f"},"content":{"rendered":"

Tkinter\u662fPython\u5185\u7f6e\u7684GUI\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5404\u79cdGUI\u5e94\u7528\u7a0b\u5e8f\u3002\u5f53\u6211\u4eec\u4f7f\u7528Tkinter\u521b\u5efa\u4e86\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u540e\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u5c06\u5176\u6253\u5305\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u7684exe\u6587\u4ef6\uff0c\u4ee5\u4fbf\u5728\u6ca1\u6709Python\u73af\u5883\u7684\u673a\u5668\u4e0a\u8fd0\u884c\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528pyinstaller\u5c06Tkinter\u7a0b\u5e8f\u6253\u5305\u6210exe\u6587\u4ef6\u3002<\/p>\n

## PyInstaller\u7b80\u4ecb<\/p>\n

PyInstaller\u662f\u4e00\u4e2a\u7528\u4e8e\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5de5\u5177\u3002\u5b83\u53ef\u4ee5\u5c06Python\u7a0b\u5e8f\u6253\u5305\u6210Windows\u3001Linux\u548cMac OS X\u4e0b\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5e76\u4e14\u53ef\u4ee5\u5c06\u6240\u6709\u4f9d\u8d56\u7684\u5e93\u548c\u8d44\u6e90\u6587\u4ef6\u4e00\u5e76\u6253\u5305\u8fdb\u53bb\uff0c\u4e0d\u9700\u8981\u5b89\u88c5Python\u73af\u5883\u5c31\u53ef\u4ee5\u8fd0\u884c\u3002<\/p>\n

## \u5b89\u88c5PyInstaller<\/p>\n

\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5PyInstaller\u3002\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n

“`<\/p>\n

pip install pyinstaller<\/p>\n

“`<\/p>\n

\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5PyInstaller\u662f\u5426\u5b89\u88c5\u6210\u529f\uff1a<\/p>\n

“`<\/p>\n

pyinstaller –version<\/p>\n

“`<\/p>\n

\u5982\u679c\u8f93\u51fa\u4e86\u7248\u672c\u53f7\uff0c\u5219\u8bf4\u660ePyInstaller\u5df2\u7ecf\u5b89\u88c5\u6210\u529f\u3002<\/p>\n

## \u6253\u5305Tkinter\u7a0b\u5e8f<\/p>\n

\u6211\u4eec\u5047\u8bbe\u6709\u4e00\u4e2aTkinter\u7a0b\u5e8f\uff0c\u5b83\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

“`<\/p>\n

import tkinter as tk<\/p>\n

class Application(tk.Frame):<\/p>\n

def __init__(self, mawindows\u6587\u4ef6\u6253\u5305\u5de5\u5177<\/a>ster=None):<\/p>\n

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

self.master = master<\/p>\n

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

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

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

self.hi_there = tk.Button(self)<\/p>\n

self.hi_there[“text”] = “Hello World\\n(click me)”<\/p>\n

\n

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

self.hi_there[“command”] = self.say_hi<\/p>\n

self.hi_there.pack(side=”top”)<\/p>\n

self.quit = tk.Button(self, text=”QUIT”, fg=”red”,<\/p>\n

command=self.master.destroy)<\/p>\n

self.quit.pack(side=”bottom”)<\/p>\n

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

print(“hi there, everyone!”)<\/p>\n

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

app = Application(master=root)<\/p>\n

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

“`<\/p>\n

\u6211\u4eec\u5c06\u8fd9\u4e2a\u7a0b\u5e8f\u4fdd\u5b58\u4e3ahello.py\u6587\u4ef6\uff0c\u7136\u540e\u5728\u547d\u4ee4\u884c\u4e2d\u5207\u6362\u5230\u8be5\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\u4e0b\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u6253\u5305\uff1a<\/p>\n

“`<\/p>\n

pyinstaller –onefile hello.py<\/p>\n

“`<\/p>\n

\u8be5\u547d\u4ee4\u5c06\u4f1a\u751f\u6210\u4e00\u4e2adist\u76ee\u5f55\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u6211\u4eec\u7684\u53ef\u6267\u884c\u6587\u4ef6hello.exe\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u8be5\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u770b\u770b\u7a0b\u5e8f\u662f\u5426\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n

## \u6253\u5305\u5305\u542b\u56fe\u7247\u548c\u5176\u4ed6\u8d44\u6e90\u7684Tkinter\u7a0b\u5e8f<\/p>\n

\u5982\u679c\u6211\u4eec\u7684Tkinter\u7a0b\u5e8f\u5305\u542b\u4e86\u56fe\u7247\u548c\u5176\u4ed6\u8d44\u6e90\u6587\u4ef6\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8fd9\u4e9b\u6587\u4ef6\u4e00\u5e76\u6253\u5305\u8fdb\u53bb\u3002\u5047\u8bbe\u6211\u4eec\u7684\u7a0b\u5e8f\u9700\u8981\u52a0\u8f7d\u4e00\u5f20\u540d\u4e3alogo.gif\u7684\u56fe\u7247\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u52a0\u8f7d\u56fe\u7247\uff1a<\/p>\n

“`<\/p>\n

logo = tk.PhotoImage(file=”logo.gif”)<\/p>\n

“`<\/p>\n

\u5728\u6253\u5305\u7a0b\u5e8f\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5c06logo.gif\u6587\u4ef6\u4e00\u5e76\u6253\u5305\u8fdb\u53bb\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u6253\u5305\u547d\u4ee4\u4e2d\u6dfb\u52a0–add-data\u9009\u9879\u6765\u6307\u5b9a\u9700\u8981\u6253\u5305\u7684\u6587\u4ef6\uff1a<\/p>\n

“`<\/p>\n

pyinstaller –onefile –add-data=”logo.gif;.”php\u6253\u5305\u6210exe<\/a> hello.py<\/p>\n

“`<\/p>\n

\u8be5\u547d\u4ee4\u5c06\u4f1a\u5c06logo.gif\u6587\u4ef6\u6253\u5305\u8fdb\u53bb\uff0c\u5e76\u4e14\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u5c06\u4f1a\u4ece\u5f53\u524d\u76ee\u5f55\u4e0b\u52a0\u8f7d\u8be5\u6587\u4ef6\u3002<\/p>\n

## \u6253\u5305\u5305\u542b\u5176\u4ed6Python\u6a21\u5757\u7684Tkinter\u7a0b\u5e8f<\/p>\n

\u5982\u679c\u6211\u4eec\u7684Tkinter\u7a0b\u5e8f\u4f9d\u8d56\u4e86\u5176\u4ed6Python\u6a21\u5757\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8fd9\u4e9b\u6a21\u5757\u4e00\u5e76\u6253\u5305\u8fdb\u53bb\u3002\u5047\u8bbe\u6211\u4eec\u7684\u7a0b\u5e8f\u4f9d\u8d56\u4e86requests\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u5bfc\u5165\u8be5\u6a21\u5757\uff1a<\/p>\n

“`<\/p>\n

import requests<\/p>\n

“`<\/p>\n

\u5728\u6253\u5305\u7a0b\u5e8f\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5c06requests\u6a21\u5757\u4e00\u5e76\u6253\u5305\u8fdb\u53bb\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u6253\u5305\u547d\u4ee4\u4e2d\u6dfb\u52a0–hidden-import\u9009\u9879\u6765\u6307\u5b9a\u9700\u8981\u6253\u5305\u7684\u6a21\u5757\uff1a<\/p>\n

“`<\/p>\n

pyinstaller –onefile –hidden-import=requests hello.py<\/p>\n

“`<\/p>\n

\u8be5\u547d\u4ee4\u5c06\u4f1a\u5c06requests\u6a21\u5757\u6253\u5305\u8fdb\u53bb\uff0c\u5e76\u4e14\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u5c06\u4f1a\u4ece\u6253\u5305\u6587\u4ef6\u4e2d\u52a0\u8f7d\u8be5\u6a21\u5757\u3002<\/p>\n

## \u603b\u7ed3<\/p>\n

\u4f7f\u7528PyInstaller\u5c06Tkinter\u7a0b\u5e8f\u6253\u5305\u6210exe\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002\u6211\u4eec\u53ea\u9700\u8981\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4e00\u4e2a\u547d\u4ee4\uff0c\u5c31\u53ef\u4ee5\u5c06\u7a0b\u5e8f\u6253\u5305\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5e76\u4e14\u5c06\u6240\u6709\u4f9d\u8d56\u7684\u5e93\u548c\u8d44\u6e90\u6587\u4ef6\u4e00\u5e76\u6253\u5305\u8fdb\u53bb\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u7a0b\u5e8f\u65b9\u4fbf\u5730\u90e8\u7f72\u5230\u6ca1\u6709Python\u73af\u5883\u7684\u673a\u5668\u4e0a\u8fd0\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Tkinter\u662fPython\u5185\u7f6e\u7684GUI\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5404\u79cdGUI\u5e94\u7528\u7a0b\u5e8f\u3002\u5f53\u6211\u4eec\u4f7f\u7528Tkinter\u521b\u5efa\u4e86\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u540e\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u5c06\u5176\u6253\u5305\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u7684exe\u6587\u4ef6\uff0c\u4ee5\u4fbf\u5728\u6ca1\u6709Python\u73af\u5883\u7684\u673a<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[18093,16486,16,662,89],"topic":[],"class_list":["post-13411","post","type-post","status-publish","format-standard","hentry","category-exekf","tag-htmlexe","tag-pc","tag-16","tag-662","tag-89"],"_links":{"self":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/13411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/comments?post=13411"}],"version-history":[{"count":1,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/13411\/revisions"}],"predecessor-version":[{"id":13494,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/13411\/revisions\/13494"}],"wp:attachment":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/media?parent=13411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/categories?post=13411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/tags?post=13411"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/topic?post=13411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}