1 回答
TA贡献1802条经验 获得超4个赞
当您使用 Qt Designer 时,您必须使用基于 QTabWidget 的设计,您必须在Widgets部分选择它:
生成的 .ui 如下:
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>
然后您必须将 .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, 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_())
然后将其导入到 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_())
最后,该文件夹将包含以下文件:
├── main.py
├── tabwidget.ui
└── tabwidget_ui.py
添加回答
举报