在 PySide 中加载 QtDesigner 的 .ui 文件
我正在寻找一个简单的示例,说明如何将 QtDesigner 生成的 .ui 文件直接加载到 Python 应用程序中。
我只是想避免使用 pyuic4。
I am looking for a simple example of how to directly load a QtDesigner generated .ui file into a Python application.
I simply would like to avoid using pyuic4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于 PySide 和 .ui 文件的完整菜鸟,这里是一个完整的示例:
For the complete noobs at PySide and .ui files, here is a complete example:
PySide 与 PyQt 不同,它实现了 QUiLoader 类来直接读取 .ui 文件。
从链接的文档中,
PySide, unlike PyQt, has implemented the QUiLoader class to directly read in .ui files.
From the linked documentation,
另一种变体,基于较短的加载指令,可在 https://askubuntu.com/questions/140740/should-i-use-pyqt-or-pyside-for-a-new-qt-project#comment248297_141641。 (基本上,您可以避免所有文件打开和关闭。)
注意:
filename.ui
应与您的 .py 文件位于同一文件夹中。if __name__ == "__main__":
如 BarryPye 的回答中所述Another variant, based on a shorter load directive, found on https://askubuntu.com/questions/140740/should-i-use-pyqt-or-pyside-for-a-new-qt-project#comment248297_141641. (Basically, you can avoid all that file opening and closing.)
Notes:
filename.ui
should be in the same folder as your .py file.if __name__ == "__main__":
as outlined in BarryPye's answer对于来自 PyQt5/6 的人来说,他们对此感到非常困惑:
PySide 没有我们习惯的相同功能,即在窗口/小部件子类顶部加载 ui 文件,如下所示:
没有任何东西与 PySide 中的非常相似。
相反,最好的办法是接受您已经避免的 ui 文件编译,因为在 PyQt 中加载 ui 文件非常容易。这有几个优点 有
缺点是你必须使用 pyside6-uic每次编辑 *.ui 文件时都会对其进行编译,但是通过使用脚本来自动化该过程可以减轻痛苦 - 在 VSCode、批处理文件或 powershell 脚本中进行设置。完成此操作后,代码就很好了:
For people coming from PyQt5/6 who are thoroughly confused by this:
PySide does not have the same functionality that we're used to, which is to load the ui file at the top of the window/widget subclass like so:
There is nothing very similar to this in PySide.
Instead, the best thing to do is embrace the ui-file compilation that you've avoided because loading the ui file is so easy in PyQt. This has a couple of advantages
The disadvantage is that you have to use pyside6-uic to compile the *.ui files each time you edit them, but this can be made less painful by using scripts to automate the process - either setting it up in VSCode, a batch file or a powershell script. After you've done this, the code is nice:
这是 PySide6 和 Windows 的一些示例。 (对于 Linux,您需要使用 /,而不是 \\)
Here is some example for PySide6 and Windows. (For linux you need use /, not \\)