Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 108fcb6

Browse files
✨ feat(tabBar): 隐藏指定Tab关闭按钮
1 parent c1c1b70 commit 108fcb6

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

‎QTabWidget/HideCloseButton.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Created on 2025年08月12日
6+
@author: Irony
7+
@site: https://pyqt.site | https://github.com/PyQt5
8+
@email: 892768447@qq.com
9+
@file: HideCloseButton.py
10+
@description:
11+
"""
12+
13+
from PyQt5.QtWidgets import QApplication, QLabel, QTabBar, QTabWidget
14+
15+
16+
class HideCloseButton(QTabWidget):
17+
def __init__(self, *args, **kwargs):
18+
super().__init__(*args, **kwargs)
19+
self.resize(800, 600)
20+
self.setTabsClosable(True)
21+
self.tabCloseRequested.connect(self.onCloseTab)
22+
23+
# add tabs
24+
self.addTab(QLabel("Home", self), "Home")
25+
for i in range(10):
26+
title = "Tab {}".format(i)
27+
self.addTab(QLabel(title, self), title)
28+
29+
# hide first tab's close button
30+
btn = self.tabBar().tabButton(0, QTabBar.RightSide)
31+
btn.close()
32+
self.tabBar().setTabButton(0, QTabBar.RightSide, None)
33+
34+
def onCloseTab(self, index):
35+
w = self.widget(index)
36+
if w:
37+
w.close()
38+
self.removeTab(index)
39+
40+
41+
if __name__ == "__main__":
42+
import cgitb
43+
import sys
44+
45+
cgitb.enable(format="text")
46+
app = QApplication(sys.argv)
47+
w = HideCloseButton()
48+
w.show()
49+
sys.exit(app.exec_())

‎QTabWidget/README.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# QTabWidget
2+
3+
- 目录
4+
- [隐藏指定Tab关闭按钮](#1隐藏指定Tab关闭按钮)
5+
6+
## 1、隐藏指定Tab关闭按钮
7+
8+
[运行 HideCloseButton.py](HideCloseButton.py)
9+
10+
```python
11+
btn = self.tabBar().tabButton(0, QTabBar.RightSide)
12+
btn.close()
13+
self.tabBar().setTabButton(0, QTabBar.RightSide, None)
14+
```
15+
16+
![HideCloseButton](ScreenShot/HideCloseButton.png)
6.86 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /