Created
March 21, 2024 14:51
-
-
Save ShenTengTu/5d14b4b6e44385853357c423d6575edf to your computer and use it in GitHub Desktop.
Function that generates a stylesheet for styling the QTabWidget, supporting four tab positions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from qtpy.QtWidgets import ( | |
QApplication, | |
QMainWindow, | |
QTabWidget, | |
QFrame, | |
QVBoxLayout, | |
) | |
def style_QTabWidget( | |
border_color: str = "#b0b0b0", | |
pane_bg_color: str = "#e0e0e0", | |
tab_bg_color: str = "#d0d0d0", | |
tab_fg_color: str = "#707070", | |
active_fg_color: str = "#404040", | |
border_width: str = "1px", | |
tab_radius: str = "4px", | |
tab_vertical_padding: str = "2px", | |
tab_horizontal_padding: str = "4px", | |
): | |
""" | |
`border_color`: color of the border of the tab widget. | |
`pane_bg_color`: color of the background of the tabpane & the selected tab. | |
`tab_bg_color`: color of the background of the unselected tab. | |
`tab_fg_color`: color of the text of the unselected tab. | |
`active_fg_color`: color of the text of the selected tab. | |
`border_width`: width of the border of the tab widget. | |
`tab_radius`: radius of the border of the tabs. | |
`tab_vertical_padding`: vertical padding of the North & South tabs, | |
or horizontal padding of the West & East tabs. | |
`tab_horizontal_padding`: horizontal padding of the North & South tabs, | |
or vertical padding of the West & East tabs. | |
""" | |
qss = f""" | |
QTabWidget::pane {{ | |
border: {border_width} solid {border_color}; | |
background-color: {pane_bg_color}; | |
}} | |
QTabBar::tab {{ | |
border-radius: {tab_radius}; | |
border: {border_width} solid {border_color}; | |
background-color: {tab_bg_color}; | |
color: {tab_fg_color}; | |
}} | |
QTabBar::tab:selected {{ | |
background-color: {pane_bg_color}; | |
color: {active_fg_color}; | |
}} | |
QTabWidget[tabPosition="North"] > QTabBar::tab, QTabWidget[tabPosition="South"] > QTabBar::tab {{ | |
padding: {tab_vertical_padding} {tab_horizontal_padding}; | |
}} | |
QTabWidget[tabPosition="West"] > QTabBar::tab, QTabWidget[tabPosition="East"] > QTabBar::tab {{ | |
padding: {tab_horizontal_padding} {tab_vertical_padding}; | |
}} | |
QTabWidget[tabPosition="North"]:tab-bar {{ | |
top: {border_width}; | |
}} | |
QTabWidget[tabPosition="North"] > QTabBar::tab {{ | |
border-bottom-left-radius: 0px; | |
border-bottom-right-radius: 0px; | |
}} | |
QTabWidget[tabPosition="North"] > QTabBar::tab:selected {{ | |
border-bottom: none; | |
}} | |
QTabWidget[tabPosition="South"]:tab-bar {{ | |
bottom: {border_width}; | |
}} | |
QTabWidget[tabPosition="South"] > QTabBar::tab {{ | |
border-top-left-radius: 0px; | |
border-top-right-radius: 0px; | |
}} | |
QTabWidget[tabPosition="South"] > QTabBar::tab:selected {{ | |
border-top: none; | |
}} | |
QTabWidget[tabPosition="West"]:tab-bar {{ | |
left: {border_width}; | |
}} | |
QTabWidget[tabPosition="West"] > QTabBar::tab {{ | |
border-top-right-radius: 0px; | |
border-bottom-right-radius: 0px; | |
}} | |
QTabWidget[tabPosition="West"] > QTabBar::tab:selected {{ | |
border-right: none; | |
}} | |
QTabWidget[tabPosition="East"]:tab-bar {{ | |
right: {border_width}; | |
}} | |
QTabWidget[tabPosition="East"] > QTabBar::tab {{ | |
border-top-left-radius: 0px; | |
border-bottom-left-radius: 0px; | |
}} | |
QTabWidget[tabPosition="East"] > QTabBar::tab:selected {{ | |
border-left: none; | |
}} | |
""" | |
return qss | |
class Frame(QFrame): | |
def __init__(self, parent=None): | |
super().__init__(parent) | |
layout = QVBoxLayout() | |
self.tabs = {} | |
for e in QTabWidget.TabPosition: | |
w = QTabWidget() | |
w.setTabPosition(e) | |
for i in range(3): | |
w.addTab(QFrame(w), f"{e.name} {i}") | |
layout.addWidget(w) | |
self.tabs[e] = w | |
self.setLayout(layout) | |
def main(): | |
app = QApplication(sys.argv) | |
app.setStyleSheet(style_QTabWidget()) | |
w = QMainWindow() | |
w.setMinimumSize(800, 600) | |
w.setCentralWidget(Frame(w)) | |
w.show() | |
sys.exit(app.exec()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment