pyqt5中添加tab页,且每个tab页都有一个按钮
时间: 2024-01-21 16:17:59 浏览: 147
可以使用QTabWidget来添加tab页,然后在每个tab页中添加QPushButton按钮。
以下是一个简单的示例代码,可以添加两个tab页,每个tab页中都有一个按钮:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTabWidget, QPushButton
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 tabs - pythonspot.com'
self.left = 0
self.top = 0
self.width = 300
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
# Create a tab widget
tabs = QTabWidget()
tab1 = QWidget()
tab2 = QWidget()
# Add tabs to the widget
tabs.addTab(tab1, "Tab 1")
tabs.addTab(tab2, "Tab 2")
# Create a vertical layout for each tab
layout1 = QVBoxLayout(tab1)
layout2 = QVBoxLayout(tab2)
# Add a button to each tab
button1 = QPushButton("Button 1", self)
button2 = QPushButton("Button 2", self)
layout1.addWidget(button1)
layout2.addWidget(button2)
# Set the layout of the main window
main_layout = QVBoxLayout()
main_layout.addWidget(tabs)
self.setLayout(main_layout)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
```
运行代码后,会显示一个带有两个tab页的窗口,每个tab页中都有一个按钮。
阅读全文