Search code examples
pythonpyqtpyqt5qtabwidget

Run QTabWidget made with designer in code


I created the GUI with tabs for different views per tab (like Chrome) that I need for my application with designer. Now I have issues running it with my Python code. Before I worked with MainWindow and that works fine, but after using the new .ui data it doesnt work anymore.

I tried the same method as with MainWindow, but there seems to be no Ui_tabWidget. I am probably mistaken how the setup of the ui works so I tried something that worked with MainWindow.

for MainWindow:

class MyForm(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.show()

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

For TabWidget:

class MyForm(QTabWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow() #or Ui_TabWidget which doesnt exist
        self.ui.setupUi(self)
        self.show()
if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyForm()
    w.show()
    sys.exit(app.exec_())

Solution

  • You have to use the design based on a QTabWidget for it when you use Qt Designer you must select it in the Widgets section:

    enter image description here

    The generated .ui is the following:

    tabwidget.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>TabWidget</class>
     <widget class="QTabWidget" name="TabWidget">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>400</width>
        <height>300</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>TabWidget</string>
      </property>
     </widget>
     <resources/>
     <connections/>
    </ui>
    

    Then you must convert the .ui to .py:

    pyuic5 tabwidget.ui -o tabwidget_ui.py -x
    

    Generating the following tabwidget_ui.py:

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'tabwidget.ui'
    #
    # Created by: PyQt5 UI code generator 5.12.1
    #
    # WARNING! All changes made in this file will be lost!
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class Ui_TabWidget(object):
        def setupUi(self, TabWidget):
            TabWidget.setObjectName("TabWidget")
            TabWidget.resize(400, 300)
    
            self.retranslateUi(TabWidget)
            QtCore.QMetaObject.connectSlotsByName(TabWidget)
    
        def retranslateUi(self, TabWidget):
            _translate = QtCore.QCoreApplication.translate
            TabWidget.setWindowTitle(_translate("TabWidget", "TabWidget"))
    
    
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        TabWidget = QtWidgets.QTabWidget()
        ui = Ui_TabWidget()
        ui.setupUi(TabWidget)
        TabWidget.show()
        sys.exit(app.exec_())
    

    Then you import it into the main.py:

    main.py

    from PyQt5 import QtWidgets
    
    from tabwidget_ui import Ui_TabWidget
    
    
    class TabWidget(QtWidgets.QTabWidget):
        def __init__(self):
            super().__init__()
            self.ui = Ui_TabWidget()
            self.ui.setupUi(self)
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        w = TabWidget()
        w.show()
        sys.exit(app.exec_())
    

    At the end the folder will have the following files:

    ├── main.py
    ├── tabwidget.ui
    └── tabwidget_ui.py