正确使用 .ui 和 pyQt5 [英] Properly use .ui with pyQt5

查看:71
本文介绍了正确使用 .ui 和 pyQt5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在 Python 程序中引入 .ui 界面.它几乎不起作用,但我想改进它,因为我无法完全获得我想要的东西.

my goal is to bring a .ui interface in a python program. It barely works but I would like to improve it since I can't obtain exactly what I want.

class MainWindow(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.ui = uic.loadUi('sss.ui')
        mainLayout = QVBoxLayout()
        mainLayout.addWidget(self.ui)
        self.setLayout(mainLayout)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec_())

现在我必须创建一个布局来粘贴我的 sss.ui.QVboxLayout 不尊重我在 Qt Designer 中设置的样式表.如何直接使用我的 sss.ui ?我需要转换它吗?我在 Qt 安装中找不到 pyuic5 ...

For now I have to create a layout to paste my sss.ui to. The QVboxLayout dosen't respect the stylesheet I setup in Qt Designer. How can I use my sss.ui directly ? Do I need to convert it ? I unable to find the pyuic5 in my Qt install ...

推荐答案

首先,获取pyuic5 可执行文件.尝试重新安装 PyQt,在 bin 目录(系统范围、virtualenv 等)中搜索它.

First, obtain the pyuic5 executable. Try to reinstall PyQt, search for it in bin directories (system-wide, virtualenv, etc.).

一旦你有了它,你应该使用它编译你的 .ui 文件:

Once you have it, you should compile your .ui file using it:

pyuic5 sss.ui > ui_sss.py

然后在您的代码中使用它:

Then use it in your code:

import sys

from PyQt5.QtWidgets import QWidget, QApplication

from ui_sss import Ui_sss  # if toplevel widget in .ui file is also called "sss"


class MainWindow(QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(self, parent)
        self.ui = Ui_sss()
        self.ui.setupUi(self)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

这篇关于正确使用 .ui 和 pyQt5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆