以下代码提供了一个可以动态添加标签的标签界面
import sys, random
from PyQt4 import QtCore, QtGui
class TabContainer(QtGui.QWidget):
def __init__(self):
super(TabContainer, self).__init__()
self.next_item_is_table = False
self.initUI()
def initUI(self):
self.setGeometry( 150, 150, 650, 350)
self.tabwidget = QtGui.QTabWidget(self)
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.tabwidget)
self.setLayout(vbox)
self.pages = []
self.add_page()
self.show()
def create_page(self, *contents):
page = QtGui.QWidget()
vbox = QtGui.QVBoxLayout()
for c in contents:
vbox.addWidget(c)
page.setLayout(vbox)
return page
def create_table(self):
rows, columns = random.randint(2,5), random.randint(1,5)
table = QtGui.QTableWidget( rows, columns )
for r in xrange(rows):
for c in xrange(columns):
table.setItem( r, c, QtGui.QTableWidgetItem( str( random.randint(0,10) ) ) )
return table
def create_list(self):
list = QtGui.QListWidget()
columns = random.randint(2,5)
for c in xrange(columns):
QtGui.QListWidgetItem( str( random.randint(0,10) ), list )
return list
def create_new_page_button(self):
btn = QtGui.QPushButton('Create a new page!')
btn.clicked.connect(self.add_page)
return btn
def add_page(self):
if self.next_item_is_table:
self.pages.append( self.create_page( self.create_table(), self.create_new_page_button() ) )
self.next_item_is_table = False
else:
self.pages.append( self.create_page( self.create_list(), self.create_new_page_button() ) )
self.next_item_is_table = True
self.tabwidget.addTab( self.pages[-1] , 'Page %s' % len(self.pages) )
self.tabwidget.setCurrentIndex( len(self.pages)-1 )
app = QtGui.QApplication(sys.argv)
ex = TabContainer()
sys.exit(app.exec_())
但我想在标签栏旁边添加一个类似于任何浏览器的新标签按钮... 我搜索了它,但我找不到添加加号按钮的方法...... TabWidget中是否有任何属性,或者我需要在那里添加一个新按钮..?
上述代码的来源是:Dynamically change view of tabs in a QTabWidget (PyQt)
答案 0 :(得分:3)
您可以在标签栏上设置corner-widget:
self.tabButton = QtGui.QToolButton(self)
self.tabButton.setText('+')
font = self.tabButton.font()
font.setBold(True)
self.tabButton.setFont(font)
self.tabwidget.setCornerWidget(self.tabButton)
self.tabButton.clicked.connect(self.add_page)