Search code examples
pythonkivy

Python Kivy: 2 sets of buttons and connection with each other


Maybe someone would give me a hint what direction have I go in, couz I"ve stucked on my problem. I will be very grateful. So, the thing is I am working on my Kivy project. The task is to evoke a line with 2 buttons, one of them have to rename another in the line. Actually it works, but only with 1 row. If we have set of lines, 5 for instance, whatever line you would choose, only first button in first line will be renamed, not specified one. I am trying to explore indexing of button set and lists of buttons but unsuccessfully

py code is here:

class Radiators(Screen):
    btn_lst = []
    btn_cn_lst = []
    i = 0
    cn = 0

    def add_button_radiator_setting(self):
        self.i += 1
        self.btn_rad = Button(text="room")
        self.btn_lst.append(self.btn_rad)

        self.ids.button_grid_radiators.add_widget(self.btn_rad)

        self.cn = self.cn + 1
        self.btn_context = Button(text="...")
        self.btn_cn_lst.append(self.btn_context)
        self.btn_cn_lst[self.cn-1].bind(on_press = self.rename_btn)
        self.ids.button_context_menu.add_widget(self.btn_context)

    def reject_button_radiator_setting(self):

        self.ids.button_grid_radiators.remove_widget(self.btn_lst[self.i-1])
        self.i -= 1
        self.btn_lst.pop(-1)
        self.ids.button_context_menu.remove_widget(self.btn_cn_lst[self.cn-1])
        self.cn -= 1
        self.btn_cn_lst.pop(-1)

    def rename_btn(self, ind):
        self.ids.button_grid_radiators.children[self.cn-1].text = str(input("enter: "))

class radiatorsApp(App):
  pass


radiatorsApp().run()

kivy code is here:

Radiators:
<Radiators>:
    name: "Radiators"
    BoxLayout:
        orientation: "horizontal"
        size_hint: 1, 0.1
        pos_hint: {"top": 1}
        Label:

            text: "Radiators"
            font_size: 32

    BoxLayout:
        orientation: "horizontal"
        size_hint: 1, 0.8

        BoxLayout:
            size_hint: 0.15, 0.8
        BoxLayout:
            size_hint: 0.6, 0.8
            GridLayout:
                orientation: "tb-lr"
                id: button_grid_radiators
                row_force_default: True
                row_default_height: 40
                cols: 1
        BoxLayout:
            size_hint: 0.05, 0.8
            GridLayout:
                orientation: "tb-lr"
                id: button_context_menu
                row_force_default: True
                row_default_height: 40
                cols: 1
        BoxLayout:
            size_hint: 0.15, 0.8

    BoxLayout:
        size_hint: 1, 0.1
        Button:
            text: "<-- back to previous Window"
            on_press: root.manager.current = "Heat System"
        Button:
            text: "Reset"
        Button:
            text: "-"
            on_press: root.reject_button_radiator_setting()
        Button:
            text: "+"
            on_press: root.add_button_radiator_setting()

Solution

  • The on_press method gets the Button that was pressed as an argument. You can use that to find the other Button:

    def rename_btn(self, pressed_button):
        index = self.btn_cn_lst.index(pressed_button)  # get index in list of Buttons
        butt = self.btn_lst[index]  # get the Button from the other list at the same index
        butt.text = str(input("enter: "))  # change the text of the Button