gpt4 book ai didi

python-2.7 - 向TabWidget添加加号按钮pyqt4

转载 作者:行者123 更新时间:2023-12-03 00:27:29 25 4
gpt4 key购买 nike

下面的代码给出了一个tab界面,可以动态添加tabs

 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)

最佳答案

您可以设置 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)

关于python-2.7 - 向TabWidget添加加号按钮pyqt4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22616446/

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