python - Lock a choice in PyQt4 QComboBox using a QCheckBox -
I'm starting to write a GUI using PyQt4
. This is my first experience with GUI (and oo-programming is something new to me). The part of that GUI will be as 4 to 5 examples of QComboBox
, as many options are being prepared, I want the user to be able to lock an option, such as later that is not changed inadvertently is being done.
PyQt4 import QtGui, import QtCore class MyGui (QtGui.QWidget) sys: def __init __ (self): QtGui.QMainWindow for a QComboBox I code that I wrote Can solve the problem with .__ init __ (self) self.resize (250, 50) widgets # vertical layout for self.vbox = QtGui.QVBoxLayout () self.setLayout (self.vbox) # with some options Create a combo box self.combo_color = QtGui.QComboBox () self.vbox.addWidget (self.combo_color) item = 'red yellow Purple'.split () self.combo_color.addItems (items) self.connect (self.combo_color, QtCore.SIGNAL ('active (QString)', self.use_choice) # that was shown (concurrently The nation) combo like self.checkbox_color = QtGui.QCheckBox ( 'lock' option, self) self.vbox.addWidget (self.checkbox_color) add a checkbox next to the self-locks Kknekt (self.checkbox_color, QtCore.SIGNAL ( ' Change the state (int) ', self.lock_choice) def use_choice (self, text): # Preferably do some useful work with print' The current option is: {likes} '. Format (option = text) def lock_choice (self): if self.checkbox_color.isChecked (): self.combo_color setEnabled (false) print 'Choice {like} locked'.format (base = Selfkcombo_colorkcurrentText ()) and : self.combo_color.setEnabled (true) is unlocked print 'choice' that __name__ == "__main__": app = QtGui.QApplication (sys.argv) mygui = myGui () mygui.show () app.exec_ ()
What this code should do, but I am very unhappy with its design, because the option of the code lock_choice
to QCombobox combo_color
is locked only To be hard work She is If I want to do the same thing for another QCebobox (eg combo_name
) and a second quote box (say checkbox_name
) which may be realized by adding the following code __ init __ (self)
code block:
# create second combo box with the second option self.combo_name = QtGui.QComboBox () self.vbox.addWidget ( Self .combo_name) item = 'bob peter' split () self.combo_name.addItems (items) self.connect (self.combo_name, QtCore.SIGNAL ('active'), self.use_choice) # add a checkbox next to Which was told ( UN) like locks combo self.checkbox_name = QtGui.QCheckBox ( 'lock' option, self) self.vbox.addWidget (self.checkbox_name) self.connect (self.checkbox_name, QtCore.SIGNAL the (State changed (int) '), Self.lock_choice) # & lt; - Clearly incorrect, because this (UN) locks the color choice at this time
Both QComboBoxes can share the method use_choice ()
rig now , But they can not share the method lock_choice ()
, because both checkboxes lock the color-options I currently do not copy and paste lock_choice ()
-method, Checkbox checkbox_name
wants to lock the name-option and switch to the hardcode combobox. Ta said. I'm sure there is an easy way, the way the goal-conmbox is to be passed by method, which I do not know yet. Help will be appreciated!
Try partial functions -
Along with partial imports, enter the QWidget name: self.connect (self.checkbox_color, QtCore.SIGNAL ('stateChanged (int))', partial (self.lock_choice, self.combo_color ))
And in your method, you can add an argument.
We will add an argument to handle the QWidget (QComboBox) method below, which we will pass through the partial function. Def lock_choice (self, connective): if combos.isEnabled (True): combos .etc enabled (false) print 'Choice (lock)' lock. Format (like = combos.currentText ())
Comments
Post a Comment