gpt4 book ai didi

python - 将图标添加到选项卡(QTabWidget)?

转载 作者:行者123 更新时间:2023-12-01 07:29:01 31 4
gpt4 key购买 nike

我正在使用带有三个选项卡QTabWidget
其中一个选项卡是关于信息/帮助的。所以我想使用问号图片 (.png) 而不是文本“帮助”。

这可能吗?我正在使用 Python/PyQt,但只能找到 C++

的解决方案

最佳答案

int QTabWidget::addTab(QWidget *page, const QIcon &icon, const QString &label)

Adds a tab with the given page, icon, and label to the tab widget, and returns the index of the tab in the tab bar. Ownership of page is passed on to the QTabWidget.

<小时/>

void QTabWidget::setTabIcon(int index, const QIcon &icon)

Sets the icon for the tab at position index.

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Window(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)

self.qtabwidget = QtWidgets.QTabWidget(self)

widget = QtWidgets.QPlainTextEdit("QPlainTextEdit 1")
label = 'Tab &1'
widget2 = QtWidgets.QPlainTextEdit("QPlainTextEdit 2")

tab_index1 = self.qtabwidget.addTab(widget, label)

tab_index2 = self.qtabwidget.addTab(widget2, 'Tab &2')
self.qtabwidget.setTabIcon(tab_index2, QtGui.QIcon('im.png')) # <---
self.qtabwidget.setIconSize(QtCore.QSize(32, 32))

self.qtabwidget.addTab(
QtWidgets.QLabel("QLabel Tab &3", alignment=QtCore.Qt.AlignCenter),
QtGui.QIcon('Ok.png'), # < ---
'Tab &3')

self.qtabwidget.addTab(None, "No Widget")

self.qtabwidget.setTabsClosable(True)
self.qtabwidget.tabCloseRequested.connect(self.qtabwidget_tabcloserequested)
self.qtabwidget.setTabShape(QtWidgets.QTabWidget.Triangular)
self.qtabwidget.setTabPosition(QtWidgets.QTabWidget.East)
self.qtabwidget.setTabEnabled(0, False) # disable tab
self.qtabwidget.setTabEnabled(1, True) # enable tab
self.qtabwidget.currentChanged.connect(self.qtabwidget_currentchanged)

self.setCentralWidget(self.qtabwidget)

@QtCore.pyqtSlot(int)
def qtabwidget_tabcloserequested(self, index):
# gets the widget
widget = self.qtabwidget.widget(index)
# if the widget exists
if widget:
widget.deleteLater()
# removes the tab of the QTabWidget
self.qtabwidget.removeTab(index)

@QtCore.pyqtSlot(int)
def qtabwidget_currentchanged(self, index):
print(f"\n New index of current page: {index}")


if __name__ == '__main__':
application = QtWidgets.QApplication(sys.argv)
window = Window()
window.setWindowTitle('QTabWidget')
window.resize(400, 400)
window.show()
sys.exit(application.exec_())

enter image description here

关于python - 将图标添加到选项卡(QTabWidget)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57305452/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com