lundi 23 mars 2020

Python PyQt5 - Checkbox to add and remove itens from a list

I'm building a user interface with PyQt5 (and python 3.7) which I'm using a check box to add elements in a general list (input_columns_general) when it's checked and remove/delete then when unchecked. Every time when I check a check box the added elements must follow a default position order in the specific list. With this task I'm having two problems:

  1. For each one of the check boxes that I have I need to build up a function which call the separately the "isChecked()" class and the correspond list that I want to add to "input_columns_general". In the case below I showed 2 lists: input_columns_mixer (with one element) and input_columns_microreactor (with five elements). How can I build up just one function (def) able to receive both situations?

  2. When I check a check box, the idea is the correlated list receive the correspondent list always at the correct postion of the general list (input_columns_general). As example: should "input_columns_microreactor" be always the first elements and "input_columns_mixer" be after "input_columns_micrreactor" without considering which one was checked first. I'm dealing with 4 check boxes and each one of them has a list which must be added regarding the correct position independently if other check box was pressed in order or not.

In the code showed below, regard a try that consider how can I add and remove the elements of the general list (but it doesn't work properly due it's not deleting all the desired elements in the list), and I didn't figure a way how can I always place in the correct position new list elements.

Do someone has a hint about this case?

I hope that I described enough code to the code be understandable about the task I'm seeking.

""'

```
    self.mixer_checkbox = QtWidgets.QCheckBox(self.tab_2)
    self.mixer_checkbox.setObjectName("mixer_checkbox")
    self.mixer_checkbox.stateChanged.connect(lambda: self.state_changed_mixer(self.mixer_checkbox)) 
    self.microreactor_checkbox = QtWidgets.QCheckBox(self.tab_2)

    self.mixer_checkbox = QtWidgets.QCheckBox(self.tab_2)
    self.microreactor_checkbox.setObjectName("microreactor_checkbox")
    self.microreactor_checkbox.stateChanged.connect(lambda:self.state_changed_microreactor(self.microreactor_checkbox))

def state_changed_mixer(self, int):
    if self.mixer_checkbox.isChecked():
        input_columns_general[4:5] = input_columns_mixer
    else:
        for element in input_columns_mixer:
            input_columns_general.remove(element)


def state_changed_microreactor(self, int):
    if self.microreactor_checkbox.isChecked():
        input_columns_general[0:4] = input_columns_microreactor
    else:
        for element in input_columns_microreactor:
            input_columns_general.remove(element)


```



Aucun commentaire:

Enregistrer un commentaire