采用pyside6库布置一个MainWindow。MainWindow含有两个widget。第一个Widget共四行,第一行依次为textlabel1,,comboBox1;第二行依次为textlabel2,CheckBox1,CheckBox2;第三行依次为textlabel3,CheckBoxA,CheckBoxB;第四行为一个Pushbutton,点击Pushbutton后显示或隐藏第二个Widget。第二个Widget默认隐藏,第二个Widget包含三个TableWidget,每个tablewidget都是两列,其中TableWidget1与CheckBox1和CheckBox2对应,对应关系如下:CheckBox1对应TableWidget1中的CheckBox101,102,103,CheckBox2对应TableWidget1中的CheckBox201,202,203,根据CheckBox1和CheckBox2的勾选情况在table中第一列动态显示对应的CheckBox;第二列设置为comboBox,每个comboBox有两个选项;TableWidget2与CheckBoxA和CheckBoxB对应,对应关系如下:CheckBoxA对应TableWidget2中的CheckBoxA1,A2,A3,CheckBoxB对应TableWidget2中的CheckBoxB1,B2,B3,根据CheckBoxA和CheckBoxB的勾选情况在table中第一列动态显示CheckBox;第二列设置为comboBox,每个comboBox有两个选项;TableWidget3第一列为CheckBox:301,302,303和;第二列设置为comboBox,每个comboBox有两个选项。三个TableWidget下方设置一个Pushbutton,点击Pushbutton后依次print所有CheckBox和comboBox的值。
时间: 2024-01-24 17:03:37 浏览: 84
好的,以下是一个示例代码,可以实现你所描述的功能:
```python
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QComboBox, \
QCheckBox, QPushButton, QTableWidget, QTableWidgetItem, QGridLayout
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建第一个Widget
widget1 = QWidget(self)
layout1 = QVBoxLayout(widget1)
# 第一行
hbox1 = QHBoxLayout()
textlabel1 = QLabel("Label 1")
comboBox1 = QComboBox()
comboBox1.addItems(["Option 1", "Option 2", "Option 3"])
hbox1.addWidget(textlabel1)
hbox1.addWidget(comboBox1)
layout1.addLayout(hbox1)
# 第二行
hbox2 = QHBoxLayout()
textlabel2 = QLabel("Label 2")
checkBox1 = QCheckBox("Checkbox 1")
checkBox2 = QCheckBox("Checkbox 2")
hbox2.addWidget(textlabel2)
hbox2.addWidget(checkBox1)
hbox2.addWidget(checkBox2)
layout1.addLayout(hbox2)
# 第三行
hbox3 = QHBoxLayout()
textlabel3 = QLabel("Label 3")
checkBoxA = QCheckBox("Checkbox A")
checkBoxB = QCheckBox("Checkbox B")
hbox3.addWidget(textlabel3)
hbox3.addWidget(checkBoxA)
hbox3.addWidget(checkBoxB)
layout1.addLayout(hbox3)
# 第四行
pushButton = QPushButton("Toggle Widget 2")
pushButton.clicked.connect(self.toggle_widget2)
layout1.addWidget(pushButton)
# 创建第二个Widget
widget2 = QWidget(self)
widget2.hide()
layout2 = QVBoxLayout(widget2)
# TableWidget 1
tableWidget1 = QTableWidget(self)
tableWidget1.setColumnCount(2)
tableWidget1.setRowCount(3)
tableWidget1.setHorizontalHeaderLabels(["Checkbox", "ComboBox"])
for i in range(3):
checkBox = QCheckBox("Checkbox 101" + str(i + 1))
comboBox = QComboBox()
comboBox.addItems(["Option 1", "Option 2"])
tableWidget1.setCellWidget(i, 0, checkBox)
tableWidget1.setCellWidget(i, 1, comboBox)
layout2.addWidget(tableWidget1)
# TableWidget 2
tableWidget2 = QTableWidget(self)
tableWidget2.setColumnCount(2)
tableWidget2.setRowCount(3)
tableWidget2.setHorizontalHeaderLabels(["Checkbox", "ComboBox"])
for i in range(3):
checkBox = QCheckBox("Checkbox A" + str(i + 1))
comboBox = QComboBox()
comboBox.addItems(["Option 1", "Option 2"])
tableWidget2.setCellWidget(i, 0, checkBox)
tableWidget2.setCellWidget(i, 1, comboBox)
layout2.addWidget(tableWidget2)
# TableWidget 3
tableWidget3 = QTableWidget(self)
tableWidget3.setColumnCount(2)
tableWidget3.setRowCount(3)
tableWidget3.setHorizontalHeaderLabels(["Checkbox", "ComboBox"])
for i in range(3):
checkBox = QCheckBox("Checkbox 301" + str(i + 1))
comboBox = QComboBox()
comboBox.addItems(["Option 1", "Option 2"])
tableWidget3.setCellWidget(i, 0, checkBox)
tableWidget3.setCellWidget(i, 1, comboBox)
layout2.addWidget(tableWidget3)
# Button
button = QPushButton("Print Values")
button.clicked.connect(self.print_values)
layout2.addWidget(button)
# 设置MainWindow布局
centralWidget = QWidget(self)
layout = QGridLayout(centralWidget)
layout.addWidget(widget1, 0, 0)
layout.addWidget(widget2, 1, 0)
self.setCentralWidget(centralWidget)
def toggle_widget2(self):
widget2 = self.centralWidget().itemAtPosition(1, 0).widget()
if widget2.isHidden():
widget2.show()
else:
widget2.hide()
def print_values(self):
print("Values:")
widget1 = self.centralWidget().itemAtPosition(0, 0).widget()
comboBox1 = widget1.findChild(QComboBox, "")
checkBox1 = widget1.findChild(QCheckBox, "Checkbox 1")
checkBox2 = widget1.findChild(QCheckBox, "Checkbox 2")
checkBoxA = widget1.findChild(QCheckBox, "Checkbox A")
checkBoxB = widget1.findChild(QCheckBox, "Checkbox B")
tableWidget1 = self.centralWidget().itemAtPosition(1, 0).widget().findChild(QTableWidget, "")
tableWidget2 = self.centralWidget().itemAtPosition(1, 0).widget().findChild(QTableWidget, "")
tableWidget3 = self.centralWidget().itemAtPosition(1, 0).widget().findChild(QTableWidget, "")
for checkBox in [checkBox1, checkBox2, checkBoxA, checkBoxB]:
if checkBox.isChecked():
print(checkBox.text() + ":")
if checkBox == checkBox1:
tableWidget = tableWidget1
elif checkBox == checkBox2:
tableWidget = tableWidget1
elif checkBox == checkBoxA:
tableWidget = tableWidget2
else:
tableWidget = tableWidget2
for i in range(tableWidget.rowCount()):
checkBox = tableWidget.cellWidget(i, 0)
comboBox = tableWidget.cellWidget(i, 1)
if checkBox.isChecked():
print(checkBox.text() + ": " + comboBox.currentText())
print()
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec())
```
这段代码创建了一个MainWindow,包含两个Widget,第一个Widget中包含四行,第二个Widget中包含三个TableWidget,以及一个Print Values按钮。点击Toggle Widget 2按钮可以显示或隐藏第二个Widget。点击Print Values按钮可以打印所有CheckBox和ComboBox的值。
阅读全文