Rows of widgets that already exist hidden, show them one by one by button click (make code shorter)

36 Views Asked by At

I have 15 rows of widgets that pre exist .hidden(). I have a + button and when i connect the function below, the rows appear one by one on every click, the code works, i am very new to python and i was wondering if there is a way to make this code shorter, i have searched a lot and probably because of my lack of experience i cant seem to find something understandable


    def add_ingredient_row(self):
        row_1 = [self.no1, self.percent_1, self.last_date_1, self.category_1, self.ingredient_1, self.brand_1, self.value_1, self.gr_1, self.new_value_1, self.cost_per_kilo_1]
        row_2 = [self.no2, self.percent_2, self.last_date_2, self.category_2, self.ingredient_2, self.brand_2, self.value_2, self.gr_2, self.new_value_2, self.cost_per_kilo_2]
        row_3 = [self.no3, self.percent_3, self.last_date_3, self.category_3, self.ingredient_3, self.brand_3, self.value_3, self.gr_3, self.new_value_3, self.cost_per_kilo_3]
        row_4 = [self.no4, self.percent_4, self.last_date_4, self.category_4, self.ingredient_4, self.brand_4, self.value_4, self.gr_4, self.new_value_4, self.cost_per_kilo_4]
        row_5 = [self.no5, self.percent_5, self.last_date_5, self.category_5, self.ingredient_5, self.brand_5, self.value_5, self.gr_5, self.new_value_5, self.cost_per_kilo_5]
        row_6 = [self.no6, self.percent_6, self.last_date_6, self.category_6, self.ingredient_6, self.brand_6, self.value_6, self.gr_6, self.new_value_6, self.cost_per_kilo_6]
        row_7 = [self.no7, self.percent_7, self.last_date_7, self.category_7, self.ingredient_7, self.brand_7, self.value_7, self.gr_7, self.new_value_7, self.cost_per_kilo_7]
        row_8 = [self.no8, self.percent_8, self.last_date_8, self.category_8, self.ingredient_8, self.brand_8, self.value_8, self.gr_8, self.new_value_8, self.cost_per_kilo_8]
        row_9 = [self.no9, self.percent_9, self.last_date_9, self.category_9, self.ingredient_9, self.brand_9, self.value_9, self.gr_9, self.new_value_9, self.cost_per_kilo_9]
        row_10 = [self.no10, self.percent_10, self.last_date_10, self.category_10, self.ingredient_10, self.brand_10, self.value_10, self.gr_10, self.new_value_10, self.cost_per_kilo_10]
        row_11 = [self.no11, self.percent_11, self.last_date_11, self.category_11, self.ingredient_11, self.brand_11, self.value_11, self.gr_11, self.new_value_11, self.cost_per_kilo_11]
        row_12 = [self.no12, self.percent_12, self.last_date_12, self.category_12, self.ingredient_12, self.brand_12, self.value_12, self.gr_12, self.new_value_12, self.cost_per_kilo_12]
        row_13 = [self.no13, self.percent_13, self.last_date_13, self.category_13, self.ingredient_13, self.brand_13, self.value_13, self.gr_13, self.new_value_13, self.cost_per_kilo_13]
        row_14 = [self.no14, self.percent_14, self.last_date_14, self.category_14, self.ingredient_14, self.brand_14, self.value_14, self.gr_14, self.new_value_14, self.cost_per_kilo_14]
        row_15 = [self.no15, self.percent_15, self.last_date_15, self.category_15, self.ingredient_15, self.brand_15, self.value_15, self.gr_15, self.new_value_15, self.cost_per_kilo_15]
        if self.no1.isHidden():
            for parts in row_1:
                parts.show()
        elif self.no2.isHidden():
            for parts in row_2:
                parts.show()
        elif self.no3.isHidden():
            for parts in row_3:
                parts.show()
        elif self.no4.isHidden():
            for parts in row_4:
                parts.show()
        elif self.no5.isHidden():
            for parts in row_5:
                parts.show()
        elif self.no6.isHidden():
            for parts in row_6:
                parts.show()
        elif self.no7.isHidden():
            for parts in row_7:
                parts.show()
        elif self.no8.isHidden():
            for parts in row_8:
                parts.show()
        elif self.no9.isHidden():
            for parts in row_9:
                parts.show()
        elif self.no10.isHidden():
            for parts in row_10:
                parts.show()
        elif self.no11.isHidden():
            for parts in row_11:
                parts.show()
        elif self.no12.isHidden():
            for parts in row_12:
                parts.show()
        elif self.no13.isHidden():
            for parts in row_13:
                parts.show()
        elif self.no14.isHidden():
            for parts in row_14:
                parts.show()
        elif self.no15.isHidden():
            for parts in row_15:
                parts.show()
        else:
            pass

after a lot of tries i came to something like this, but it shows all the rows together.

i made the lists callable because i discovered i am going to use them a lot in other functions too


    def no_list(self):
        no_list = [self.no1, self.no2, self.no3, self.no4, self.no5, self.no6, self.no7, self.no8, self.no9, self.no10, self.no11, self.no12, self.no13, self.no14, self.no15]
        return no_list

    def percent_list(self):
        percent_list = [self.percent_1, self.percent_2, self.percent_3, self.percent_4, self.percent_5, self.percent_6, self.percent_7, self.percent_8, self.percent_9, self.percent_10, self.percent_11, self.percent_12, self.percent_13, self.percent_14, self.percent_15]
        return percent_list

    def last_date_list(self):
        last_date_list = [self.last_date_1, self.last_date_2, self.last_date_3, self.last_date_4, self.last_date_5, self.last_date_6, self.last_date_7, self.last_date_8, self.last_date_9, self.last_date_10, self.last_date_11, self.last_date_12, self.last_date_13, self.last_date_14, self.last_date_15]
        return last_date_list

    def category_list(self):
        category_list = [self.category_1, self.category_2, self.category_3, self.category_4, self.category_5, self.category_6, self.category_7, self.category_8, self.category_9, self.category_10, self.category_11, self.category_12, self.category_13, self.category_14, self.category_15]
        return category_list

    def ingredient_list(self):
        ingredient_list = [self.ingredient_1, self.ingredient_2, self.ingredient_3, self.ingredient_4, self.ingredient_5, self.ingredient_6, self.ingredient_7, self.ingredient_8, self.ingredient_9, self.ingredient_10, self.ingredient_11, self.ingredient_12, self.ingredient_13, self.ingredient_14, self.ingredient_15]
        return ingredient_list

    def value_list(self):
        value_list = [self.value_1, self.value_2, self.value_3, self.value_4, self.value_5, self.value_6, self.value_7, self.value_8, self.value_9, self.value_10, self.value_11, self.value_12, self.value_13, self.value_14, self.value_15]
        return value_list

    def cost_per_kilo_list(self):
        cost_per_kilo_list = [self.cost_per_kilo_1, self.cost_per_kilo_2, self.cost_per_kilo_3, self.cost_per_kilo_4, self.cost_per_kilo_5, self.cost_per_kilo_6, self.cost_per_kilo_7, self.cost_per_kilo_8, self.cost_per_kilo_9, self.cost_per_kilo_10, self.cost_per_kilo_11, self.cost_per_kilo_12, self.cost_per_kilo_13, self.cost_per_kilo_14, self.cost_per_kilo_15]
        return cost_per_kilo_list

    def brand_list(self):
        brand_list = [self.brand_1, self.brand_2, self.brand_3, self.brand_4, self.brand_5, self.brand_6, self.brand_7, self.brand_8, self.brand_9, self.brand_10, self.brand_11, self.brand_12, self.brand_13, self.brand_14, self.brand_15]
        return brand_list

    def gr_list(self):
        gr_list = [self.gr_1, self.gr_2, self.gr_3, self.gr_4, self.gr_5, self.gr_6, self.gr_7, self.gr_8, self.gr_9, self.gr_10, self.gr_11, self.gr_12, self.gr_13, self.gr_14, self.gr_15]
        return gr_list

    def new_value_list(self):
        new_value_list = [self.new_value_1, self.new_value_2, self.new_value_3, self.new_value_4, self.new_value_5, self.new_value_6, self.new_value_7, self.new_value_8, self.new_value_9, self.new_value_10, self.new_value_11, self.new_value_12, self.new_value_13, self.new_value_14, self.new_value_15]
        return new_value_list


    def add_ingredient_row(self):
        no_list = self.no_list()
        percent_list = self.percent_list()
        last_date_list = self.last_date_list()
        category_list = self.category_list()
        ingredient_list = self.ingredient_list()
        value_list = self.value_list()
        cost_per_kilo_list = self.cost_per_kilo_list()
        brand_list = self.brand_list()
        gr_list = self.gr_list()
        new_value_list = self.new_value_list()
        for no, percent, last_date, category, ingredient, value, cost, brand, gr, new_value in zip(no_list, percent_list, last_date_list, category_list, ingredient_list, value_list, cost_per_kilo_list, brand_list, gr_list, new_value_list):

            if no.isHidden():
                no.show()
                percent.show()
                last_date.show()
                category.show()
                ingredient.show()
                value.show()
                cost.show()
                brand.show()
                gr.show()
                new_value.show()
            else:
                pass

what am i missing?

thanks in advance

0

There are 0 best solutions below