我尝试使用pyinstaller模块将py文件转换为exe文件。 当我从pycharm或命令行执行py文件时,效果很好。但是,当我转换为exe文件后,它不再起作用。
我的命令是
“ pyinstaller -F -w practise.py”
然后,我在dist文件夹中找到一个exe文件。实际上,我可以看到我的计算机正在读取该程序,但是读取后什么也没显示。
即使我试图将用户输入命令放入我的代码中,但它也不起作用。
有人可以帮我解决这个问题吗?
我想制作一个无需安装python的独立exe GUI程序。
谢谢。
PS。我上传的代码是测试版本,因此非常简单。
import tkinter as tk
from tkinter import ttk, scrolledtext, Menu
from tkinter import messagebox as mBox
import PyInstaller as pInstall
COLOR = ['Blue', 'Gold', 'Red']
class OOP():
def __init__(self):
self.win = tk.Tk()
self.win.title("Python GUI")
self.win.geometry('500x300')
self.creatwidgets()
self.win.resizable(0,0)
def _click(self):
self.action.configure(text='Hello ' + self.name.get() + ' ' + self.number.get())
def _rad(self):
if self.radVal.get() == 0: self.monty.config(text=COLOR[0])
elif self.radVal.get() == 1: self.monty.config(text=COLOR[1])
elif self.radVal.get() == 2: self.monty.config(text=COLOR[2])
def _quit(self):
self.win.quit()
self.win.destroy()
exit()
def _msgbox(self):
mBox.showinfo('About', 'This is from mBox')
def _contents(self):
#######################################
# === === === Tab1 === === ===
# === ROW 0 ===
# labels
ttk.Label(self.monty, text='Enter a name: ').grid(column=0, row=0, sticky=tk.E)
ttk.Label(self.monty, text='Number').grid(column=1, row=0, sticky=tk.E)
# === ROW 1 ===
# Entry
self.name = tk.StringVar()
nameEntered = ttk.Entry(self.monty, width=24, textvariable=self.name)
nameEntered.grid(column=0, row=1, padx=8, pady=4)
nameEntered.focus()
nameEntered.delete(0, tk.END)
nameEntered.insert(0, '<Default Name>')
# ComboBox
self.number = tk.StringVar()
numberBox = ttk.Combobox(self.monty, width=20, state='readonly', textvariable=self.number)
numberBox.grid(column=1, row=1, padx=8, pady=4)
numberBox['values'] = [1, 3, 5, 6]
numberBox.current(0)
# Button
self.action = ttk.Button(self.monty, text='Click!', command=self._click)
self.action.grid(column=2, row=1, padx=8, pady=4)
# === ROW 2 ===
# CheckBox
check1 = tk.Checkbutton(self.monty, text='Disabled', state='disabled')
check1.select()
check1.grid(column=0, row=2)
check2 = tk.Checkbutton(self.monty, text='UnChecked')
check2.deselect()
check2.grid(column=1, row=2)
check3 = tk.Checkbutton(self.monty, text='Enabled')
check3.select()
check3.grid(column=2, row=2)
# === ROW 3 ===
self.radVal = tk.IntVar()
for idx in range(3):
rad = 'Rad' + str(idx)
rad = ttk.Radiobutton(self.monty, text=COLOR[idx], command=self._rad,
variable=self.radVal, value=idx)
rad.grid(column=idx, row=3)
# === ROW 4 ===
scr = scrolledtext.ScrolledText(self.monty, width=50, height=5)
scr.grid(column=0, row=4, columnspan=3, pady=10, padx=8)
#######################################
# === === === Tab2 === === ===
# === ROW 0 ===
scr = scrolledtext.ScrolledText(self.monty2, width=50, height=5)
scr.grid(column=0, row=0, columnspan=3, pady=10, padx=8)
# === ROW 1 ===
labelframes = ttk.LabelFrame(self.monty2, text='Labels in a Frame')
labelframes.grid(column=0, row=1, padx=8, pady=10)
for idx in range(3):
label = 'label' + str(idx + 1)
label = ttk.Label(labelframes, text=label)
label.grid(column=idx, row=0)
for child in labelframes.winfo_children():
child.grid_configure(padx=8, pady=4)
def _menu(self):
self.menubar = Menu(self.win)
self.win.config(menu=self.menubar)
filemenu = Menu(self.menubar, tearoff=0)
filemenu.add_command(label='New')
filemenu.add_separator()
filemenu.add_command(label='Exit', command=self._quit)
self.menubar.add_cascade(label='File', menu=filemenu)
helpmenu = Menu(self.menubar, tearoff=0)
helpmenu.add_command(label='About', command=self._msgbox)
self.menubar.add_cascade(label='Help', menu=helpmenu)
def _tab(self):
tabcontrol = ttk.Notebook(self.win)
self.tab1 = ttk.Frame(tabcontrol)
tabcontrol.add(self.tab1, text='Tab 1')
self.tab2 = ttk.Frame(tabcontrol)
tabcontrol.add(self.tab2, text='Tab 2')
tabcontrol.pack(expand=1, fill='both')
def _frames(self):
self.monty = ttk.LabelFrame(self.tab1, text='Monty Python')
self.monty.grid(column=0, row=0, padx=8, pady=4)
self.monty2 = ttk.LabelFrame(self.tab2, text='Monty2 Python')
self.monty2.grid(column=0, row=0, padx=8, pady=4)
def creatwidgets(self):
# === Tab ===
self._tab()
# === Monty Frame ===
self._frames()
# === Contents ===
self._contents()
# === Menu ===
self._menu()
oop = OOP()
oop.win.mainloop()
#k = input("press close to exit")