我有一个使用PyQt5作为其GUI的Python应用程序。我有一个标签小部件,我想在窗口类之外添加和删除标签。就像是:
Tabs.addTab("name")
我怎么做?
这是我的代码:
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTabWidget ,QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Test'
self.left = 0
self.top = 0
self.width = 500
self.height = 500
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.table_widget = MyTableWidget(self)
self.setCentralWidget(self.table_widget)
self.show()
class MyTableWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tabs.resize(300,200)
self.tabs.addTab(self.tab1, "Tab 1")
self.tabs.addTab(self.tab2, "Tab 2")
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
谢谢您的帮助!
最佳答案
不管是要在类中还是在其外部删除选项卡都没有关系,但必须使用QTabWidget对象,例如,如果要从“ App”类中添加选项卡,则必须执行此操作通过属性为“ tabs”的对象“ table_widget”(即QTabWidget):
class App(QMainWindow):
def __init__(self):
super().__init__()
# ...
self.table_widget.tabs.addTab(QWidget(), "name") # <--- add tab
self.table_widget.tabs.removeTab(0) # <--- remove tab
关于python - PyQt5在类外添加和删除选项卡,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59977806/