我有3个问题如下:
(1)在tab1中,我希望第一行中的文本标签自动匹配它下面的复选框/单选按钮。然而,当我放大窗户时,它开始脱离网格。
(2)对于滚动区域中的是/否文本:当我放大窗口时,标签本身变得比滚动区域内的其他小部件(即复选框)太大。不确定是什么导致了这种情况发生?
(3)那些单选按钮代码有点冗长。我想要有一个for循环来使它更简单,但现在还不知道如何做到这一点。
如果你知道如何解决这个问题,请告诉我。理解!
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setLayout(QGridLayout())
yesLabel = QLabel('Yes')
self.layout().addWidget(yesLabel, 0, 3)
noLabel = QLabel('No')
self.layout().addWidget(noLabel,0, 4)
checkBox1 = QCheckBox('1', self)
checkBox2 = QCheckBox('2', self)
checkBox3 = QCheckBox('3', self)
checkBox4 = QCheckBox('4', self)
checkBox5 = QCheckBox('5', self)
checkBox6 = QCheckBox('6', self)
checkBox7 = QCheckBox('7', self)
checkBox8 = QCheckBox('8', self)
checkBox9 = QCheckBox('9')
checkBox10 = QCheckBox('10', self)
checkBox11 = QCheckBox('11', self)
checkBox12 = QCheckBox('12', self)
checkBox13 = QCheckBox('13', self)
checkBox14 = QCheckBox('14', self)
checkBox15 = QCheckBox('15', self)
checkBox16 = QCheckBox ('16', self)
self.layout().addWidget(checkBox1, 1, 1, 1, 15)
self.layout().addWidget(checkBox2, 2, 1)
self.layout().addWidget(checkBox3, 3, 1)
self.layout().addWidget(checkBox4, 4, 1)
self.layout().addWidget(checkBox5, 5, 1)
self.layout().addWidget(checkBox6, 6, 1)
self.layout().addWidget(checkBox7, 7, 1)
self.layout().addWidget(checkBox8, 8, 1)
self.layout().addWidget(checkBox9, 9, 1)
self.layout().addWidget(checkBox10, 10, 1)
self.layout().addWidget(checkBox11, 11, 1)
self.layout().addWidget(checkBox12, 12, 1)
self.layout().addWidget(checkBox13, 13, 1)
self.layout().addWidget(checkBox14, 14, 1)
self.layout().addWidget(checkBox15, 15, 1)
self.layout().addWidget(checkBox16, 16, 1)
radioButton1a = QRadioButton(self)
radioButton1b = QRadioButton(self)
self.layout().addWidget(radioButton1a, 1, 3)
self.layout().addWidget(radioButton1b, 1, 4)
qgroup1 = QButtonGroup(self)
qgroup1.addButton(radioButton1a)
qgroup1.addButton(radioButton1b)
radioButton2a = QRadioButton(self)
radioButton2b = QRadioButton(self)
self.layout().addWidget(radioButton2a, 2, 3)
self.layout().addWidget(radioButton2b, 2, 4)
qgroup2 = QButtonGroup(self)
qgroup2.addButton(radioButton2a)
qgroup2.addButton(radioButton2b)
radioButton3a = QRadioButton(self)
radioButton3b = QRadioButton(self)
self.layout().addWidget(radioButton3a, 3, 3)
self.layout().addWidget(radioButton3b, 3, 4)
qgroup3 = QButtonGroup(self)
qgroup3.addButton(radioButton3a)
qgroup3.addButton(radioButton3b)
radioButton4a = QRadioButton(self)
radioButton4b = QRadioButton(self)
self.layout().addWidget(radioButton4a, 4, 3)
self.layout().addWidget(radioButton4b, 4, 4)
qgroup4 = QButtonGroup(self)
qgroup4.addButton(radioButton4a)
qgroup4.addButton(radioButton4b)
radioButton5a = QRadioButton(self)
radioButton5b = QRadioButton(self)
self.layout().addWidget(radioButton5a, 5, 3)
self.layout().addWidget(radioButton5b, 5, 4)
qgroup5= QButtonGroup(self)
qgroup5.addButton(radioButton5a)
qgroup5.addButton(radioButton5b)
radioButton6a = QRadioButton(self)
radioButton6b = QRadioButton(self)
self.layout().addWidget(radioButton6a, 6, 3)
self.layout().addWidget(radioButton6b, 6, 4)
qgroup6 = QButtonGroup(self)
qgroup6.addButton(radioButton6a)
qgroup6.addButton(radioButton6b)
radioButton7a = QRadioButton(self)
radioButton7b = QRadioButton(self)
self.layout().addWidget(radioButton7a, 7, 3)
self.layout().addWidget(radioButton7b, 7, 4)
qgroup7 = QButtonGroup(self)
qgroup7.addButton(radioButton7a)
qgroup7.addButton(radioButton7b)
radioButton8a = QRadioButton(self)
radioButton8b = QRadioButton(self)
self.layout().addWidget(radioButton8a, 8, 3)
self.layout().addWidget(radioButton8b, 8, 4)
qgroup8 = QButtonGroup(self)
qgroup8.addButton(radioButton8a)
qgroup8.addButton(radioButton8b)
radioButton9a = QRadioButton(self)
radioButton9b = QRadioButton(self)
self.layout().addWidget(radioButton9a, 9, 3)
self.layout().addWidget(radioButton9b, 9, 4)
qgroup9 = QButtonGroup(self)
qgroup9.addButton(radioButton9a)
qgroup9.addButton(radioButton9b)
radioButton10a = QRadioButton(self)
radioButton10b = QRadioButton(self)
self.layout().addWidget(radioButton10a, 10, 3)
self.layout().addWidget(radioButton10b, 10, 4)
qgroup10 = QButtonGroup(self)
qgroup10.addButton(radioButton10a)
qgroup10.addButton(radioButton10b)
radioButton11a = QRadioButton(self)
radioButton11b = QRadioButton(self)
self.layout().addWidget(radioButton11a, 11, 3)
self.layout().addWidget(radioButton11b, 11, 4)
qgroup11 = QButtonGroup(self)
qgroup11.addButton(radioButton11a)
qgroup11.addButton(radioButton11b)
radioButton12a = QRadioButton(self)
radioButton12b = QRadioButton(self)
self.layout().addWidget(radioButton12a, 12, 3)
self.layout().addWidget(radioButton12b, 12, 4)
qgroup12 = QButtonGroup(self)
qgroup12.addButton(radioButton12a)
qgroup12.addButton(radioButton12b)
radioButton13a = QRadioButton(self)
radioButton13b = QRadioButton(self)
self.layout().addWidget(radioButton13a, 13, 3)
self.layout().addWidget(radioButton13b, 13, 4)
qgroup13 = QButtonGroup(self)
qgroup13.addButton(radioButton13a)
qgroup13.addButton(radioButton13b)
radioButton14a = QRadioButton(self)
radioButton14b = QRadioButton(self)
self.layout().addWidget(radioButton14a, 14, 3)
self.layout().addWidget(radioButton14b, 14, 4)
qgroup14 = QButtonGroup(self)
qgroup14.addButton(radioButton14a)
qgroup14.addButton(radioButton14b)
radioButton15a = QRadioButton(self)
radioButton15b = QRadioButton(self)
self.layout().addWidget(radioButton15a, 15, 3)
self.layout().addWidget(radioButton15b, 15, 4)
qgroup15 = QButtonGroup(self)
qgroup15.addButton(radioButton15a)
qgroup15.addButton(radioButton15b)
radioButton16a = QRadioButton(self)
radioButton16b = QRadioButton(self)
self.layout().addWidget(radioButton16a, 16, 3)
self.layout().addWidget(radioButton16b, 16, 4)
qgroup16 = QButtonGroup(self)
qgroup16.addButton(radioButton16a)
qgroup16.addButton(radioButton16b)
class Tab1(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setLayout(QGridLayout())
benchmarkLabel = QLabel('someText1')
self.layout().addWidget(benchmarkLabel, 0,1)
tatLabel = QLabel('someText2')
self.layout().addWidget(tatLabel, 0, 3)
self.group = Widget(self)
scroll = QScrollArea(self)
scroll.setWidget(self.group)
scroll.setWidgetResizable(True)
self.layout().addWidget(scroll, 1, 1, 1, 50)
checkBoxT1a = QCheckBox('abc', self)
self.layout().addWidget(checkBoxT1a, 2, 1)
leT1a = QLineEdit()
self.layout().addWidget(leT1a, 2, 2)
runBtn = QPushButton("Run", self)
self.layout().addWidget(runBtn, 4, 1)
class Page1(QTabWidget):
def __init__(self, parent=None):
super(Page1, self).__init__(parent)
self.tab1 = Tab1()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.addTab(self.tab1, "Tab1")
self.addTab(self.tab2, "Tab2")
self.addTab(self.tab3, "Tab3")
self.tab2_initUI()
self.tab3_initUI()
def tab2_initUI(self):
grid = QGridLayout()
self.tab2.setLayout(grid)
def tab3_initUI(self):
grid = QGridLayout()
self.tab3.setLayout(grid)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setGeometry(300, 200, 600, 370)
self.startPage1()
def startPage1(self):
x = Page1(self)
self.setWindowTitle("1")
self.setCentralWidget(x)
self.show()
def main():
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()