我写了一个 Python 应用程序,我想分发它。我已经在它之上构建了一个 GUI,并且到目前为止工作正常。我使用以下内容来设置 GUI:
qtCreatorFile = "gui.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
# Initialize parent PyQt classes
QtWidgets.QMainWindow.__init__(self)
self.setupUi(self)
(....)
该应用程序从以下内容开始:
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
如果我理解正确,对于使用 fbs 打包的“gui.ui”文件,我应该将其作为资源加载。所以我用这些作为修改:
from fbs_runtime.application_context import ApplicationContext, cached_property #added to imports)
class AppContext(ApplicationContext): # Subclass ApplicationContext
def run(self):
qtCreatorFile=self.get_design() # to get the .ui file
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
window = MyApp()
version = self.build_settings['version']
window.setWindowTitle("EPANET parser v" + version)
window.show()
return self.app.exec_()
def get_design(self):
qtCreatorFile=self.get_resource("gui.ui") # It is in the correct src\main\resources path
return qtCreatorFile
@cached_property # all tutorials suggest this , but don't understand why. ???
def main_window(self):
return MainWindow(self)
fbs 应用程序应从以下替换开始if __name__ == '__main__:'
:
if __name__ == '__main__':
appctxt = AppContext()
exit_code = appctxt.app.exec_()
sys.exit(exit_code)
但是,我收到以下错误:
Traceback (most recent call last):
File "C:\Users\....src\main\python\main.py", line 61, in <module>
class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
NameError: name 'Ui_MainWindow' is not defined
我知道 MyApp 是从现在类Ui_MainWindow
中定义的继承而来的AppContext
,而 MyApp 无法引用它。任何帮助将不胜感激!