对pyqt5中QTabWidget的相关操作详解
首先,下面贴上designer处理的界面文件(转换成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_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 600) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.tabWidget = QtWidgets.QTabWidget(self.centralwidget) self.tabWidget.setGeometry(QtCore.QRect(110, 60, 521, 411)) self.tabWidget.setObjectName("tabWidget") self.tab = QtWidgets.QWidget() self.tab.setObjectName("tab") self.tabWidget.addTab(self.tab, "") self.tab_2 = QtWidgets.QWidget() self.tab_2.setObjectName("tab_2") self.tabWidget.addTab(self.tab_2, "") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) self.tabWidget.setCurrentIndex(0) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1")) self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
然后,开始处理这个控件:
1、如何将控件最上面的小tab变成透明的?
# -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets import QApplication, QMainWindow from TabWidget import Ui_MainWindow class ChangeTabColor(Ui_MainWindow, QMainWindow): def __init__(self): super(ChangeTabColor, self).__init__() self.setupUi(self) self.setFixedSize(self.width(), self.height()) self.tabWidget.setTabsClosable(True) str = "QTabBar::tab{background-color:rbg(255,255,255,0);}" self.tabWidget.setStyleSheet(str) def ui_main(): app = QApplication(sys.argv) w = ChangeTabColor() w.show() sys.exit(app.exec_()) if __name__ == '__main__': ui_main()
其中,下面这两句话是将控件的上面的小tab变成透明的关键。
str = "QTabBar::tab{background-color:rbg(255,255,255,0);}" self.tabWidget.setStyleSheet(str)
2、如何在选中小tab的时候突出选中的部分(改变颜色)?
还是上面的代码不变,其中str部分加上点内容,改成如下语句:
str = "QTabBar::tab{background-color:rbg(255,255,255,0);}" + \ "QTabBar::tab:selected{color:red;background-color:rbg(255,200,255);} "
3、如何处理小tab的点击事件?
在上面的代码中加入一些语句即可实现。
def __init__(self): super(ChangeTabColor, self).__init__() self.setupUi(self) self.setFixedSize(self.width(), self.height()) self.tabWidget.setTabsClosable(True) str = "QTabBar::tab{background-color:rbg(255,255,255,0);}" + \ "QTabBar::tab:selected{color:red;background-color:rbg(255,200,255);} " self.tabWidget.setStyleSheet(str) self.tabWidget.currentChanged.connect(self.slot_small_tab) def slot_small_tab(self): if self.tabWidget.currentIndex() == 0: print('666') elif self.tabWidget.currentIndex() == 1: print('222') else: pass
未完待续。。。
相关推荐
OldBigBrother 2020-11-09
追逐阳光的风 2020-07-04
王磊的程序员之路 2020-05-15
JimyFengqi 2020-02-17
hustlei 2020-02-01
hustlei 2020-01-29
lianback 2020-01-11
追逐阳光的风 2020-01-05
shengge0 2019-12-31
lianback 2019-12-30
Airuio 2019-12-05
hustlei 2011-03-03
OldBigBrother 2019-06-19
Eppley 2019-06-19