使用 PyInstaller 创建的 .exe 文件不显示其 GUI
.exe file created with PyInstaller does not show its GUI
我使用 PyQt5 设计器编写了一个密码生成器并ui为其创建了一个 GUI。该脚本在其启动器中调用 .ui 并且两者都在同一文件夹中。
def __init__(self, parent=None):
super().__init__(parent)
self.ui = uic.loadUi('Generator.ui', self)
但是,在通过 PyInstaller 将两者转换为 .exe 文件后,从其 dist 文件夹中提取 .exe 文件并执行后,控制台会立即弹出并关闭,而不会显示 GUI。
如何在不手动将 .ui 代码添加到 Generator.py 脚本的情况下解决此问题?
谢谢
我把这个作为答案展示我可以举个例子。使用 pyuic 工具将 .ui 文件转换为 .py 文件。此创建的 .py 文件中将有一个 class,它是 Qt Designer 中的小部件 built 的名称。将此 class 导入并子class 到您正在创建的 GUI class 中。
from designer_file import Ui_Gui # Designer file is the converted .ui file and Ui_Gui is the ui class it created
class GUIWindow(QtWidgets.QWidget, Ui_Gui):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self) # This is necessary to setup the ui when using this method
# Code here...
我使用 PyQt5 设计器编写了一个密码生成器并ui为其创建了一个 GUI。该脚本在其启动器中调用 .ui 并且两者都在同一文件夹中。
def __init__(self, parent=None):
super().__init__(parent)
self.ui = uic.loadUi('Generator.ui', self)
但是,在通过 PyInstaller 将两者转换为 .exe 文件后,从其 dist 文件夹中提取 .exe 文件并执行后,控制台会立即弹出并关闭,而不会显示 GUI。
如何在不手动将 .ui 代码添加到 Generator.py 脚本的情况下解决此问题?
谢谢
我把这个作为答案展示我可以举个例子。使用 pyuic 工具将 .ui 文件转换为 .py 文件。此创建的 .py 文件中将有一个 class,它是 Qt Designer 中的小部件 built 的名称。将此 class 导入并子class 到您正在创建的 GUI class 中。
from designer_file import Ui_Gui # Designer file is the converted .ui file and Ui_Gui is the ui class it created
class GUIWindow(QtWidgets.QWidget, Ui_Gui):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self) # This is necessary to setup the ui when using this method
# Code here...