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
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
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
## \u6253\u5305\u5305\u542b\u5176\u4ed6Python\u6a21\u5757\u7684Tkinter\u7a0b\u5e8f<\/p>\n
pyinstaller –onefile –hidden-import=requests hello.py<\/p>\n