Enable and disable list of checkboxes using pyqt loop
-
I have about 16 checkboxes on my gui. I also have a list of values and based on those values I only want to enable certain checkboxes. my checkboxes are all named
cb_text_(this then varies from rf1a, rf2a, rf3a, rf4a, rf1b, rf2b...).
I have a list of all of the names (rf1a, rf2a, rf3a,...) and am trying to say
plist = ['rf1a', 'rf2a', rf3a'...] for i in plist: enable_str = 'cb_text_' + str(i) self.ui.enable_str.setEnabled(True)
but python is telling me
AttributeError: 'PySide2.QtWidgets.QWidget' object has no attribute 'enable_str'
-
@poordev123 said in Enable and disable list of checkboxes using pyqt loop:
I have a list of all of the names (rf1a, rf2a, rf3a,...) and am trying to say
The available approaches depend on what you mean by "names" here:
- You are talking about the name given to the checkbox widget in Designer, so that there are variables like
self.ui.cb_text_rf1a
. - You gave each checkbox an object name in its properties in Designer.
Even Python does not allow to directly access a variable via a string of its name!
You could go down @mrjj's way of accessing
self.ui
's attributes to find a variable by a string of its name if you want, but it's a bit ugly (at least IMHO).However, in the case of a group of checkboxes (or radiobuttons) there is an alternative approach supplied by Qt: QButtonGroup Class. This allows you to group all your checkboxes and access each/all of them via e.g.
QButtonGroup.buttons()
. This has existed for years. A post like https://forum.qt.io/topic/24027/qtdesigner-how-radio-button-group-together mentions how to use this from Designer:You can also create a button group explicitly for your radio buttons. Select the ones you want to group, and right click on one of them. Then, you'll find an option to "group" the buttons. If you click that, you will notice that a new QButtonGroup has appeared in your object browser in designer. You can select that button group to give it a name, make it exclusive, etc.
Going back, if you do want to stick with your "array/list* for the checkboxes, it's much easier if you create a list of references to the checkboxes than strings of their variable names:
cblist = [self.ui.cb_text_rf1a, self.ui.cb_text_rf2a, self.ui.cb_text_rf3a, ...] for cb in cblist: cb.setEnabled(True)
It's a few more characters to type for each one, but avoids having to try to convert strings to variable names.
Finally, since I'm here, there is another way of accessing a bunch of widgets without knowing anything about their variable (or object) names. Provided they all share some parent you can find all checkboxes at runtime from that parent downward. So let's say
self
is aQMainWindow
and you want to visit every checkbox on the main window. Then:cblist = self.findChildren(QCheckBox) for cb in cblist: cb.setEnabled(True)
- You are talking about the name given to the checkbox widget in Designer, so that there are variables like
-
Hi
Im not sure you can do it that way as the "compiler" just sees the actual expression
" self.ui.enable_str."
and not what's inside enable_strhowever, did you try with
getattrhttps://www.programiz.com/python-programming/methods/built-in/getattr
-
@poordev123 said in Enable and disable list of checkboxes using pyqt loop:
I have a list of all of the names (rf1a, rf2a, rf3a,...) and am trying to say
The available approaches depend on what you mean by "names" here:
- You are talking about the name given to the checkbox widget in Designer, so that there are variables like
self.ui.cb_text_rf1a
. - You gave each checkbox an object name in its properties in Designer.
Even Python does not allow to directly access a variable via a string of its name!
You could go down @mrjj's way of accessing
self.ui
's attributes to find a variable by a string of its name if you want, but it's a bit ugly (at least IMHO).However, in the case of a group of checkboxes (or radiobuttons) there is an alternative approach supplied by Qt: QButtonGroup Class. This allows you to group all your checkboxes and access each/all of them via e.g.
QButtonGroup.buttons()
. This has existed for years. A post like https://forum.qt.io/topic/24027/qtdesigner-how-radio-button-group-together mentions how to use this from Designer:You can also create a button group explicitly for your radio buttons. Select the ones you want to group, and right click on one of them. Then, you'll find an option to "group" the buttons. If you click that, you will notice that a new QButtonGroup has appeared in your object browser in designer. You can select that button group to give it a name, make it exclusive, etc.
Going back, if you do want to stick with your "array/list* for the checkboxes, it's much easier if you create a list of references to the checkboxes than strings of their variable names:
cblist = [self.ui.cb_text_rf1a, self.ui.cb_text_rf2a, self.ui.cb_text_rf3a, ...] for cb in cblist: cb.setEnabled(True)
It's a few more characters to type for each one, but avoids having to try to convert strings to variable names.
Finally, since I'm here, there is another way of accessing a bunch of widgets without knowing anything about their variable (or object) names. Provided they all share some parent you can find all checkboxes at runtime from that parent downward. So let's say
self
is aQMainWindow
and you want to visit every checkbox on the main window. Then:cblist = self.findChildren(QCheckBox) for cb in cblist: cb.setEnabled(True)
- You are talking about the name given to the checkbox widget in Designer, so that there are variables like
-
@JonB Hi, thank you for your answer. I am going to go with the final way that you mentioned. That is
cblist = self.findChildren(QCheckBox) for cb in cblist: cb.setEnabled(True)
That worked for me. Thank you very much, I appreciate it.