使用python读取qt的ui文件时出现路径错误。

问题描述 投票:0回答:1

UI文件和脚本在同一个目录下,但当我直接用UI的名字加载时,显示错误,当我用abs路径加载成功,我需要把它打包成exe文件,这样就不能用abs路径了。enter image description here

enter image description here

python pyside2
1个回答
2
投票

相对路径是关于脚本执行的地方,在你的例子中,从图像中可以说你的IDE从 "D:\Anaconda3/python.exe "启动,所以不建议使用这种类型的路径,但最好是建立绝对路径,以你的例子为例,你可以使用.py文件的路径。

CURRENT_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
filename = os.path.join(CURRENT_DIRECTORY, "uni.ui")
self.ui = QUiLoader().load(filename)
© www.soinside.com 2019 - 2024. All rights reserved.