Print name of the list while using KivyMDList

156 Views Asked by At

I am developing an application that shows movie info. But a problem has arisen; how to find out which item of the mdlist has been clicked? E.g: if I clicked a movie name in the list: joker, it should open a new screen with a name joker.

from kivymd.app import MDApp
from kivymd.uix.button import MDIconButton,MDFloatingActionButton
from kivymd.uix.screen import Screen
from kivy.lang import Builder
from kivymd.uix.list import OneLineListItem,OneLineIconListItem,IconLeftWidget,OneLineRightIconListItem
from kivy.uix.screenmanager import ScreenManager
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivymd.uix.textfield import MDTextField
import imdb
from kivymd.uix.boxlayout import BoxLayout
from kivy.uix.image import AsyncImage
from kivymd.uix.label import MDLabel


Window.size=(300,500)

Builds ="""
ScreenManager:
    Search_screen:
    Show_screen:
    Movie_screen:



<Search_screen>:
    name:'searchs'
    MDIconButton:
        icon:'movie'
       pos_hint:{'center_x':.5,'center_y':.5}
       on_press:app.hlo()
    MDTextField:
        id:wow
        hint_text:'search movie'
        pos_hint:{'center_x':.5,'center_y':.9}
        color_mode: 'accent'
        width:300
    
    

<Show_screen>:
    name:'show'
    ScrollView:
        MDList:
            id:container
    MDFloatingActionButton:
        icon:'keyboard-return'
        pos_hint:{'center_x':.9,'center_y':.1}
        on_press:app.move()

   

<Movie_screen>:
    name:'movie'
    ScrollView:
        BoxLayout:
            MDCard:
    MDIconButton:
        icon:'android'
        on_press:root.manager.current='searchs'

            

        
"""


class Search_screen(Screen):
    pass



class Show_screen(Screen):
    pass

class Movie_screen(Screen):
    pass


sm = ScreenManager()
sm.add_widget(Search_screen(name='search'))
sm.add_widget(Show_screen(name='show'))
sm.add_widget(Movie_screen(name='movie'))



class apps(MDApp):
    def build(self):
        screens = Screen()
        self.theme_cls.primary_pallete = "Orange"
        self.theme_cls.theme_style = 'Dark'
        self.helper = Builder.load_string(Builds)
        screens.add_widget(self.helper)
        return screens

def hlo(self):
    self.helper.current='show'
    ia = imdb.IMDb()
    name = self.helper.get_screen('searchs').ids.wow.text
    search = ia.search_movie(name)
    self.no=0
    self.j=0
    for self.i in search:                                                                  
                                                                                    
       self.helper.get_screen('show').ids.container.add_widget(OneLineListItem(text=str(self.i),#here on_presss it should print its name)


def wown(self,o):
    self.helper.current='movie'
    self.helper.get_screen('show').ids.container.clear_widgets()
def move(self):
    self.helper.current='searchs'
    self.helper.get_screen('show').ids.container.clear_widgets()
1

There are 1 best solutions below

1
On BEST ANSWER

You just need to add an on_press or on_release to each OneLineListItem. Here is a modified hlo() method that does that:

def hlo(self):
    self.helper.current='show'
    ia = imdb.IMDb()
    name = self.helper.get_screen('searchs').ids.wow.text
    search = ia.search_movie(name)
    self.no=0
    self.j=0
    for self.i in search:
       self.helper.get_screen('show').ids.container.add_widget(OneLineListItem(text=str(self.i), on_release=self.item_pressed))

def item_pressed(self, item):
    print('item_pressed:', item.text)