猿问

在代码中运行由设计器制作的 QTabWidget

我创建了带有选项卡的 GUI,每个选项卡(如 Chrome)的不同视图是我的应用程序与设计器所需的。现在我在用我的 Python 代码运行它时遇到了问题。在我使用 MainWindow 之前,它工作正常,但在使用新的 .ui 数据后,它不再工作了。


我尝试了与 MainWindow 相同的方法,但似乎没有 Ui_tabWidget。我可能弄错了 ui 的设置是如何工作的,所以我尝试了一些与 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_())

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


慕尼黑的夜晚无繁华
浏览 176回答 1
1回答

慕虎7371278

当您使用 Qt Designer 时,您必须使用基于 QTabWidget 的设计,您必须在Widgets部分选择它:生成的 .ui 如下:tabwidget.ui<?xml version="1.0" encoding="UTF-8"?><ui version="4.0">&nbsp;<class>TabWidget</class>&nbsp;<widget class="QTabWidget" name="TabWidget">&nbsp; <property name="geometry">&nbsp; &nbsp;<rect>&nbsp; &nbsp; <x>0</x>&nbsp; &nbsp; <y>0</y>&nbsp; &nbsp; <width>400</width>&nbsp; &nbsp; <height>300</height>&nbsp; &nbsp;</rect>&nbsp; </property>&nbsp; <property name="windowTitle">&nbsp; &nbsp;<string>TabWidget</string>&nbsp; </property>&nbsp;</widget>&nbsp;<resources/>&nbsp;<connections/></ui>然后您必须将 .ui 转换为 .py:pyuic5 tabwidget.ui -o tabwidget_ui.py -x生成以下 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, QtWidgetsclass Ui_TabWidget(object):&nbsp; &nbsp; def setupUi(self, TabWidget):&nbsp; &nbsp; &nbsp; &nbsp; TabWidget.setObjectName("TabWidget")&nbsp; &nbsp; &nbsp; &nbsp; TabWidget.resize(400, 300)&nbsp; &nbsp; &nbsp; &nbsp; self.retranslateUi(TabWidget)&nbsp; &nbsp; &nbsp; &nbsp; QtCore.QMetaObject.connectSlotsByName(TabWidget)&nbsp; &nbsp; def retranslateUi(self, TabWidget):&nbsp; &nbsp; &nbsp; &nbsp; _translate = QtCore.QCoreApplication.translate&nbsp; &nbsp; &nbsp; &nbsp; TabWidget.setWindowTitle(_translate("TabWidget", "TabWidget"))if __name__ == "__main__":&nbsp; &nbsp; import sys&nbsp; &nbsp; app = QtWidgets.QApplication(sys.argv)&nbsp; &nbsp; TabWidget = QtWidgets.QTabWidget()&nbsp; &nbsp; ui = Ui_TabWidget()&nbsp; &nbsp; ui.setupUi(TabWidget)&nbsp; &nbsp; TabWidget.show()&nbsp; &nbsp; sys.exit(app.exec_())然后将其导入到 main.py 中:主文件from PyQt5 import QtWidgetsfrom tabwidget_ui import Ui_TabWidgetclass TabWidget(QtWidgets.QTabWidget):&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__()&nbsp; &nbsp; &nbsp; &nbsp; self.ui = Ui_TabWidget()&nbsp; &nbsp; &nbsp; &nbsp; self.ui.setupUi(self)if __name__ == "__main__":&nbsp; &nbsp; import sys&nbsp; &nbsp; app = QtWidgets.QApplication(sys.argv)&nbsp; &nbsp; w = TabWidget()&nbsp; &nbsp; w.show()&nbsp; &nbsp; sys.exit(app.exec_())最后,该文件夹将包含以下文件:├── main.py├── tabwidget.ui└── tabwidget_ui.py
随时随地看视频慕课网APP

相关分类

Python
我要回答