我开始使用PyQt4
. 这是我第一次使用 GUI(而且 oo 编程对我来说有点新)。该 GUI 的一部分将像 4 到 5 个QComboBox
. 由于要做出许多选择,我希望用户能够锁定一个选择,这样以后就不会无意中改变它。对于一个 QComboBox,我可以用我写的这段代码来解决这个问题:
import sys
from PyQt4 import QtGui, QtCore
class MyGui(QtGui.QWidget):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.resize(250, 50)
# vertical layout for widgets
self.vbox = QtGui.QVBoxLayout()
self.setLayout(self.vbox)
# Create a combo box with some choices
self.combo_color = QtGui.QComboBox()
self.vbox.addWidget(self.combo_color)
items = 'Red Yellow Purple'.split()
self.combo_color.addItems(items)
self.connect(self.combo_color, QtCore.SIGNAL('activated(QString)'), self.use_choice)
# add a checkbox next to the combobox which (un-)locks the the combo-choice
self.checkbox_color = QtGui.QCheckBox('Lock Choice', self)
self.vbox.addWidget(self.checkbox_color)
self.connect(self.checkbox_color, QtCore.SIGNAL('stateChanged(int)'), self.lock_choice)
def use_choice(self, text):
# do something very useful with the choice
print 'The current choice is: {choice}'.format(choice=text)
def lock_choice(self):
if self.checkbox_color.isChecked():
self.combo_color.setEnabled(False)
print 'Choice {choice} locked'.format(choice=self.combo_color.currentText())
else:
self.combo_color.setEnabled(True)
print 'Choice unlocked'
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mygui = MyGui()
mygui.show()
app.exec_()
这段代码做了它应该做的,但我对它的设计很不满意,因为这个方法lock_choice
是硬编码的,只能锁定 QComboBox 的选择combo_color
。如果我现在想对另一个 QComboBox(比如combo_name
)和第二个 QCheckBox(比如checkbox_name
)做同样的事情怎么办,这可以通过将以下代码附加到类__init__(self)
代码块来实现:
# create second combo box with some other choices
self.combo_name = QtGui.QComboBox()
self.vbox.addWidget(self.combo_name)
items = 'Bob Peter'.split()
self.combo_name.addItems(items)
self.connect(self.combo_name, QtCore.SIGNAL('activated(QString)'), self.use_choice)
# add a checkbox next to the combobox which (un-)locks the the combo-choice
self.checkbox_name = QtGui.QCheckBox('Lock Choice', self)
self.vbox.addWidget(self.checkbox_name)
self.connect(self.checkbox_name, QtCore.SIGNAL('stateChanged(int)'), self.lock_choice) # <-- obviously wrong, as it (un-)locks color choice at the moment
两个 QComboBoxesuse_choice()
现在可以共享方法,但它们不能共享方法lock_choice()
,因为两个复选框都锁定了颜色选择。我希望复选框checkbox_name
锁定名称选择,而不复制和粘贴当前 lock_choice()
方法并切换硬编码组合框。我确信有一种简单的方法,比如将目标组合框传递给我还不知道的方法。帮助将不胜感激!