使用 KivyMDList 时打印列表的名称
Print name of the list while using KivyMDList
我正在开发一个显示电影信息的应用程序。但是出现了一个问题;如何找出mdlist中的哪一项被点击了?
例如:如果我单击列表中的电影名称:小丑,它应该打开一个名为 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()
您只需为每个 OneLineListItem
添加一个 on_press
或 on_release
。这是一个修改后的 hlo()
方法:
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)
我正在开发一个显示电影信息的应用程序。但是出现了一个问题;如何找出mdlist中的哪一项被点击了?
例如:如果我单击列表中的电影名称:小丑,它应该打开一个名为 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()
您只需为每个 OneLineListItem
添加一个 on_press
或 on_release
。这是一个修改后的 hlo()
方法:
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)