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 14:03:37 浏览: 63
这是一个比较复杂的问题,需要用到PyQt5库来实现。以下是一个可供参考的实现代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QPushButton, QTableWidget, QTableWidgetItem, QVBoxLayout, QHBoxLayout, QCheckBox, QComboBox
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(100, 100, 800, 600)
self.setWindowTitle("Pushbutton Demo")
self.gridLayout = QGridLayout()
self.setLayout(self.gridLayout)
self.pushButton = QPushButton('Show/Hide Widget')
self.pushButton.clicked.connect(self.showHideWidget)
self.gridLayout.addWidget(self.pushButton, 0, 0)
self.widget2 = QWidget()
self.widget2Layout = QVBoxLayout()
self.widget2.setLayout(self.widget2Layout)
self.tableWidget1 = QTableWidget()
self.tableWidget1.setColumnCount(2)
self.tableWidget1.setRowCount(3)
self.tableWidget1.setHorizontalHeaderLabels(['Checkbox', 'Combobox'])
for i in range(3):
checkbox = QCheckBox('Checkbox1{}'.format(i+1))
checkbox.stateChanged.connect(self.updateTable1)
self.tableWidget1.setCellWidget(i, 0, checkbox)
combobox = QComboBox()
combobox.addItems(['Option1', 'Option2'])
self.tableWidget1.setCellWidget(i, 1, combobox)
self.widget2Layout.addWidget(self.tableWidget1)
self.tableWidget2 = QTableWidget()
self.tableWidget2.setColumnCount(2)
self.tableWidget2.setRowCount(3)
self.tableWidget2.setHorizontalHeaderLabels(['Checkbox', 'Combobox'])
for i in range(3):
checkbox = QCheckBox('CheckboxA{}'.format(i+1))
checkbox.stateChanged.connect(self.updateTable2)
self.tableWidget2.setCellWidget(i, 0, checkbox)
combobox = QComboBox()
combobox.addItems(['Option1', 'Option2'])
self.tableWidget2.setCellWidget(i, 1, combobox)
self.widget2Layout.addWidget(self.tableWidget2)
self.tableWidget3 = QTableWidget()
self.tableWidget3.setColumnCount(2)
self.tableWidget3.setRowCount(3)
self.tableWidget3.setHorizontalHeaderLabels(['Checkbox', 'Combobox'])
for i in range(3):
checkbox = QCheckBox('Checkbox30{}'.format(i+1))
self.tableWidget3.setCellWidget(i, 0, checkbox)
combobox = QComboBox()
combobox.addItems(['Option1', 'Option2'])
self.tableWidget3.setCellWidget(i, 1, combobox)
self.widget2Layout.addWidget(self.tableWidget3)
self.widget2Button = QPushButton('Print')
self.widget2Button.clicked.connect(self.printValues)
self.widget2Layout.addWidget(self.widget2Button)
self.widget2.hide()
self.gridLayout.addWidget(self.widget2, 1, 0)
def showHideWidget(self):
if self.widget2.isHidden():
self.widget2.show()
else:
self.widget2.hide()
def updateTable1(self):
for i in range(3):
checkbox = self.tableWidget1.cellWidget(i, 0)
combobox = self.tableWidget1.cellWidget(i, 1)
if checkbox.isChecked():
combobox.show()
else:
combobox.hide()
def updateTable2(self):
for i in range(3):
checkbox = self.tableWidget2.cellWidget(i, 0)
combobox = self.tableWidget2.cellWidget(i, 1)
if checkbox.isChecked():
combobox.show()
else:
combobox.hide()
def printValues(self):
values = []
for i in range(3):
checkbox = self.tableWidget1.cellWidget(i, 0)
combobox = self.tableWidget1.cellWidget(i, 1)
if checkbox.isChecked():
values.append('{}:{}'.format(checkbox.text(), combobox.currentText()))
for i in range(3):
checkbox = self.tableWidget2.cellWidget(i, 0)
combobox = self.tableWidget2.cellWidget(i, 1)
if checkbox.isChecked():
values.append('{}:{}'.format(checkbox.text(), combobox.currentText()))
for i in range(3):
checkbox = self.tableWidget3.cellWidget(i, 0)
combobox = self.tableWidget3.cellWidget(i, 1)
if checkbox.isChecked():
values.append('{}:{}'.format(checkbox.text(), combobox.currentText()))
print(values)
if __name__ == '__main__':
app = QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())
```
该代码实现了一个窗口,包含一个按钮和一个隐藏的Widget。点击按钮可以显示或隐藏该Widget。Widget中包含三个TableWidget和一个PushButton。每个TableWidget都包含两列,第一列为CheckBox,第二列为ComboBox。根据CheckBox的勾选情况,ComboBox可以动态显示或隐藏。点击PushButton会打印所有CheckBox和ComboBox的值。
阅读全文