Python PyQt5 多Tab demo

本文介绍在PyQt5 中如何实现多个tab 的UI,每个TabUI中独立实现某些业务逻辑功能,用到了PyQt5.QtWidgets中的如下模块:

QVBoxLayout,QWidget,QFormLayout,QHBoxLayout,QLineEdit,QRadioButton,QCheckBox,QLabel,QGroupBox,QApplication,QTabWidget

importsys

fromPyQt5.QtWidgetsimportQVBoxLayout,QWidget,QFormLayout,QHBoxLayout,QLineEdit,QRadioButton,QCheckBox,QLabel,QGroupBox,QApplication,QTabWidget

classTabDemo(QTabWidget):

def__init__(self,parent=None):

super(TabDemo,self).__init__(parent)

self.resize(500,300)

self.tab1 = QWidget()

self.tab2 = QWidget()

self.tab3 = QWidget()

self.tab4 = QWidget()

self.tab5 = QWidget()

self.addTab(self.tab1,"Tab 1")

self.addTab(self.tab2,"Tab 2")

self.addTab(self.tab3,"Tab 3")

self.addTab(self.tab4,"Tab 4")

self.addTab(self.tab5,"Tab 5")

self.tab1UI()

self.tab2UI()

self.tab3UI()

self.tab4UI()

self.tab5UI()

self.setWindowTitle("Tab Demo")

deftab1UI(self):

layout = QFormLayout()

layout.addRow("姓名",QLineEdit())

layout.addRow("地址",QLineEdit())

self.setTabText(,"联系方式")#也可以在addTab时进行修改

self.tab1.setLayout(layout)

deftab2UI(self):

layout = QFormLayout()

sex = QHBoxLayout()

sex.addWidget(QRadioButton("男"))

sex.addWidget(QRadioButton("女"))

lb = QLabel()

lb.setText("性别")

layout.addRow(lb,sex)

layout.addRow("生日",QLineEdit())

self.setTabText(1,"个人详细信息")

self.tab2.setLayout(layout)

deftab3UI(self):

layout = QHBoxLayout()

lb = QLabel()

lb.setText("科目")

layout.addWidget(lb)

layout.addWidget(QCheckBox("物理"))

layout.addWidget(QCheckBox("高数"))

layout.addWidget(QCheckBox("体育"))

layout.addWidget(QCheckBox("英语"))

self.setTabText(2,"教育程度")

self.tab3.setLayout(layout)

deftab4UI(self):

layout = QHBoxLayout()

mainBox = QGroupBox("汇总")

self.setTabText(3,"信息汇总")

layout1 = QVBoxLayout()

lb = QLabel()

lb.setText("科目")

layout1.addWidget(lb)

layout1.addWidget(QCheckBox("计算机"))

layout1.addWidget(QCheckBox("python"))

layout1.addWidget(QCheckBox("C"))

layout1.addWidget(QCheckBox("Java"))

layout1.addWidget(QCheckBox("ruby"))

mainBox.setLayout(layout1)

layout.addWidget(mainBox)

self.tab4.setLayout(layout)

deftab5UI(self):

layout = QHBoxLayout()

mainBox = QGroupBox("汇总")

self.setTabText(4,"信息汇总1")

layout1 = QVBoxLayout()

lb = QLabel()

lb.setText("科目")

layout1.addWidget(lb)

layout1.addWidget(QCheckBox("计算机"))

layout1.addWidget(QCheckBox("python"))

layout1.addWidget(QCheckBox("C"))

layout1.addWidget(QCheckBox("Java"))

layout1.addWidget(QCheckBox("ruby"))

mainBox.setLayout(layout1)

layout.addWidget(mainBox)

self.tab5.setLayout(layout)

if__name__ =='__main__':

app = QApplication(sys.argv)

demo = TabDemo()

demo.show()

sys.exit(app.exec_())

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190123G1D22A00?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励