Search code examples
pythonqtpysideqwidget

Using a file converted by QtCreator in Python


I've got a file converted by the command: pyside-uic -o ui_name.py name.ui and I try to use it in my project. First file:

class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        self.ui_window = ui_window.Ui_MainWindow()

        self.btn5 = QPushButton(QIcon(),"Open", self)
        self.btn5.move(0, 20)
        self.btn5.resize(70, 20)
        self.btn5.clicked.connect(self.doAction5)

    def doAction5(self):
        self.ui_window.Show()

Second (ui_window.py):

class Ui_MainWindow(object):
.......
    def Show(self):
    self.show()

When I execute the project and press the Button, there is an error:

AttributeError: 'Ui_MainWindow' object has no attribute 'show'

I have no idea what to replace it with.

EDIT: First file code edited.


Solution

  • After you've run pyside-uic -o ui_name.py name.ui, you'll end up with a python file for the interface, ui_name.py. Don't change anything in this file. This file is meant to be imported in your main code, like this:

    from ui_name import Ui_MainWindow
    
    class MainWindow(QMainWindow, Ui_MainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.setupUi(self)
    
            self.btn5 = QPushButton(QIcon(),"Open", self)
            self.btn5.move(0, 20)
            self.btn5.resize(70, 20)
            self.btn5.clicked.connect(self.doAction5)
    
            self.show()