简体   繁体   中英

Properly use .ui with pyQt5

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_())

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 ...

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

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_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM