我正在QTabWidget中创建一个功能:
我能够使'重置参数'在大多数情况下工作,它可以取消两个单选按钮和&组合框之间的依赖关系是独立的。但我不确定如何定义可以将选定的组合框添加到先前定义的网格布局的功能。另外,不确定选项卡名称来自尴尬下拉菜单的位置/原因。
使用Python 2.7将代码输入Anaconda的iPython笔记本中。
#7.5.2016 making toggle switches
import sys
import webbrowser
from PyQt4 import QtGui, QtCore
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Plastique'))
class BeginExplorerWidget(QtGui.QTabWidget):
def __init__(self, parent=None):
super(BeginExplorerWidget, self).__init__(parent)
self.setGeometry(50,50,800,600)
self.setWindowTitle("Explorer")
#Defining items inside the tabs
#tab 1: ProQinase Cancer Cell Line Explorer
button = QtGui.QPushButton("Reset parameters")
button.clicked.connect(self.my_method)
self.rad1 = QtGui.QRadioButton("Explore Individual Cell Lines")
self.rad1.toggled.connect(lambda: self.btnstate(self.rad1))
self.rad2 = QtGui.QRadioButton("Cell Line Correlation")
self.rad2.toggled.connect(lambda: self.btnstate(self.rad2))
#opens if rad1 on
self.combo1 = QtGui.QComboBox(self)
self.list1 = ['x', 'y', 'z']
self.combo1.addItems(QtCore.QStringList(self.list1))
self.combo1.currentIndexChanged.connect(self.changeindexcombo1)
self.combo2 = QtGui.QComboBox(self)
self.list2 =[['x1', 'x2', 'x3'], ['y1', 'y2', 'y3'], ['z1', 'z2', 'z3']]
self.combo2.addItems(QtCore.QStringList(self.list2[0]))
#opens if rad2 on
self.comboA = QtGui.QComboBox(self)
self.listA = ['CPQ #', 'CCLE #']
self.comboA.currentIndexChanged.connect(self.changeindexcomboA)
self.comboB = QtGui.QComboBox(self)
self.listB = [['Q1', 'Q2', 'Q3'], ['C1', 'C2', 'C3']]
self.comboC = QtGui.QComboBox(self)
self.listC = ['CPQ #', 'CCLE #']
self.comboC.currentIndexChanged.connect(self.changeindexcomboC)
self.comboD = QtGui.QComboBox(self)
self.listD = [['Q1', 'Q2', 'Q3'], ['C1', 'C2', 'C3']]
tab1 = QtGui.QWidget()
#layouts:
#tab 1: ProQinase Cancer Cell Line Explorer
layout = QtGui.QGridLayout()
layout.addWidget(button, 0,0)
layout.addWidget(self.rad1, 0,1)
layout.addWidget(self.rad2, 0,2)
tab1.setLayout(layout)
self.group = QtGui.QButtonGroup()
self.group.addButton(self.rad1)
self.group.addButton(self.rad2)
#adding tabs to the QtGui.QTabWidget
self.addTab(tab1,"ProQinase Cancer Cell Lines")
self.show()
def my_method(self):
self.group.setExclusive(False)
self.rad1.setChecked(False)
self.rad2.setChecked(False)
self.group.setExclusive(True)
def btnstate(self, rad):
if rad.text() == "Explore Individual Cell Lines":
self.tab1.addWidget(self.combo1, 1, 1)
self.tab1.addWidget(self.combo2, 1, 2)
if rad.text() == "Cell Line Correlation":
#layout = QtGui.QGridLayout()
self.tab1.addWidget(self.comboA, 1, 1)
self.tab1.addWidget(self.comboB, 1, 2)
self.tab1.addWidget(self.comboC, 2, 1)
self.tab1.addWidget(self.comboD, 2, 2)
#tab1.setlayout(layout)
def changeindexcombo1(self, ind):
self.combo2.clear()
self.combo2.addItems(QtCore.QStringList(self.list2[ind]))
def changeindexcomboA(self, ind):
self.comboB.clear()
self.comboB.addItems(QtCore.QStringList(self.list2[ind]))
def changeindexcomboC(self, ind):
self.comboD.clear()
self.comboD.addItems(QtCore.QStringList(self.list2[ind]))
if __name__ == '__main__':
app = QtGui.QApplication([])
window = BeginExplorerWidget()
window.show()
app.exec_()