python - 隐藏后显示关闭按钮(x)(QTabBar)
问题描述
我正在创建一种方法来隐藏和显示选项卡的关闭按钮。我找到了隐藏它的方法。但是,我不知道如何反过来。
这是我现有的隐藏关闭按钮的代码。使用相同的代码行,如何显示选项卡的关闭按钮?
def disable_close_button(self):
self.ui.tab_widget.tabBar().setTabButton(self.current_index(), QTabBar.RightSide, None)
def enable_close_button(self):
pass
提前致谢!
解决方案
您不是在隐藏按钮,而是在消除它。因此,在我的解决方案中,我得到了按钮,然后根据需要隐藏或显示它。
import sys
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
show_button = QtWidgets.QPushButton(
text="show",
clicked=self.enable_close_button
)
hide_button = QtWidgets.QPushButton(
text="hide",
clicked=self.disable_close_button
)
self.tab_widget = QtWidgets.QTabWidget(tabsClosable=True)
for i in range(4):
label = QtWidgets.QLabel(
text="label {}".format(i),
alignment=QtCore.Qt.AlignCenter
)
self.tab_widget.addTab(label , "tab-{}".format(i))
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(show_button)
lay.addWidget(hide_button)
lay.addWidget(self.tab_widget)
@QtCore.pyqtSlot()
def enable_close_button(self):
ix = self.tab_widget.currentIndex()
button = self.tab_widget.tabBar().tabButton(ix, QtWidgets.QTabBar.RightSide)
if button is not None:
button.show()
@QtCore.pyqtSlot()
def disable_close_button(self):
ix = self.tab_widget.currentIndex()
button = self.tab_widget.tabBar().tabButton(ix, QtWidgets.QTabBar.RightSide)
if button is not None:
button.hide()
if __name__ == '__main__':
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
app.setStyle("fusion")
w = Widget()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
推荐阅读
- javascript - 用于 YAML 前端的 Javascript 正则表达式
- python - 启动器中的致命错误:无法使用“”创建进程
- html - 根据该图像坐标将图像放在 VueJS/Vuetify 中的另一个上
- php - 将 id 值提交到购物车页面 php
- c++ - 通过它的模板类重载 C++ 模板类方法
- node.js - Electron - 未捕获的错误:EROFS:只读文件系统
- c# - 我怎样才能钳制旋转?
- ios - 在 Swift / SwiftUI 中一次更新列表中的每个按钮项,而不是一次更新所有按钮
- robotframework - 在 Robot Framework 中使用 For 循环进行分页
- python - Not recognizing commands in discord with Python