0

I am creating a gallery in one of the app screens. The app fetch a list of tuples from the database in this format: [('id#','title','filepath','download date','link','url image'),('id#','title','filepath','download date','link','url image')]

Then in the gallery I am using on_pre_enter() to generate a scroll view with buttons as soon as the user enters the downloads screen. So far for the image link and the button name are good 100%

The only issue is with on_press for some reason all the buttons have the same file path which is the final one in the database.

Any ideas on how to approach this?

Code:

    def on_pre_enter(self,*args):
        self.Scroll_content = self.ids.scroll_view
        self.layout = GridLayout(cols=2,spacing=10,
                                size_hint_y=15)
        self.layout.bind(minimum_height=self.layout.setter('height'))
        self.Scroll_content.remove_widget(self.layout)
        for self.item in self.list_from_database:
            self.current_file_path = self.item[2]
            self.title = self.item=[1]
            self.btn = Button(text=(self.title
                            + "\n"
                            + self.item[3]
                            + "\n"
                            + 'Click me to play'
                            ),
                            # border=(5,5,5,5),
                            size_hint=(1, 1),
                            width= 20,
                            font_size=8,
                            background_color=(1.0, 0.0, 0.0, 1.0),
                            on_press=(lambda x: startfile(self.current_file_path))
                            )
            self.image = AsyncImage(source =self.item[5],
                                    size_hint= (1,1)
                                    )
            # print(self.item[2])
            self.layout.add_widget(self.image)
            self.layout.add_widget(self.btn)
        self.Scroll_content.add_widget(self.layout)

This worked for me passing the button as an instance to the call_back function then getting the text on the button after searching for the path of the required file based on the text on the button:

def on_pre_enter(self,*args):
        self.Scroll_content = self.ids.scroll_view
        self.layout = GridLayout(cols=2,spacing=10,
                                size_hint_y=15)
        self.layout.bind(minimum_height=self.layout.setter('height'))
        for self.item in self.list_from_database:
            self.current_file_path = self.item[2]
            self.title = self.item[1]
            self.btn = Button(
                            text=self.title,
                            # border=(5,5,5,5),
                            size_hint=(1, 1),
                            width= 20,
                            font_size=8,
                            background_color=(1.0, 0.0, 0.0, 1.0)
                            )
            self.image = AsyncImage(source =self.item[5],
                                    size_hint= (1,1)
                                    )
            self.btn.bind(on_press=(self.call_back))
            # print(self.item[2])
            self.layout.add_widget(self.image)
            self.layout.add_widget(self.btn)
        self.Scroll_content.clear_widgets()
        self.Scroll_content.add_widget(self.layout)


    def call_back(self, instance):
        self.title = instance.text
        print(self.title)
        for self.item in self.list_from_database:
            if self.title == self.item[1]:
                startfile(self.item[2])
                print(self.item[2])
                break
araldhafeeri
  • 179
  • 9
  • change to `on_press=(lambda x, path=self.current_file_path: startfile(path))` – eyllanesc Apr 02 '20 at 05:37
  • Thank you so much! I have left the code and worked on something else. Then came after a break and a cup of coffee wrote a solution which work for me but I will try yours as well! so much less code with your method! @eyllanesc – araldhafeeri Apr 02 '20 at 10:06

0 Answers0