So far I have simple scroll view and a for loop in range of 10 to represent some data, that ill add in later, the data is within a MDCard
. I want the user to be able to tap on any of the 10 cards which will then take them to my second screen, which I've created in Kivy and show the number that the user has tapped on.
I've used on_touch_down
to register the user tapping on the card, but Im unsure how to then direct the user to the second screen showing the number they tapped on.
How can I create a function in Python to execute this?
*.py
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.core.window import Window
from kivymd.app import MDApp
from kivymd.uix.card import MDCard
from kivymd.uix.label import MDLabel
class FirstWindow(Screen):
def __init__(self, **kwargs):
super(FirstWindow, self).__init__(**kwargs)
Clock.schedule_once(self.create_scrollview)
def create_scrollview(self, dt):
layout = BoxLayout( orientation='vertical', spacing=25, size_hint_y=None, size_hint=(1, None), padding=[170, 10])
layout.bind(minimum_height=layout.setter("height"))
for self.x in range(10):
card = MDCard(orientation='vertical', size_hint=(1, None), height=200 ,padding=(20,0),ripple_behavior= True,radius=[5, ])
card.add_widget(MDLabel(text=str(self.x), halign= "center"))
layout.add_widget(card)
card.bind(on_touch_down=self.clicked)
scrollview = ScrollView(size_hint=(1, 1))
self.view.add_widget(scrollview)
scrollview.add_widget(layout)
def clicked(self, card, touch):
self.create_scrollview
if card.collide_point(*touch.pos):
print(self.x)
class SecondWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
class NearMeApp(MDApp):
def build(self):
self.theme_cls.theme_style ="Dark"
self.theme_cls.accent_palette = "Red"
self.theme_cls.primary_palette = "Blue"
self.root = Builder.load_file('NearMe.kv')
if __name__ == '__main__':
NearMeApp().run()
*.kv
WindowManager:
FirstWindow:
SecondWindow:
<FirstWindow>:
name:"FirstWindow"
view: view
BoxLayout:
orientation: 'vertical'
canvas.before:
Color:
rgba: .2, .2, .2, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'vertical'
BoxLayout:
size: (50, 50)
size_hint: (1, None)
Label:
text: "xxx"
canvas.before:
Color:
rgba: 0, 1, 1, 1
BoxLayout:
size: (50, 50)
size_hint: (1, None)
Label:
text:"xxx"
canvas.before:
Color:
rgba: 1, 1, 1, 1
ScrollView:
id: view
canvas.before:
Color:
rgba: 1, 1, 0, 1
<SecondWindow>:
name:"SecondWindow"
BoxLayout:
orientation: 'vertical'
canvas.before:
Color:
rgba: .2, .2, .2, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'vertical'
Label:
text: "Screen2"
Button:
text:"click"
on_release:
app.root.current = "FirstWindow"
CodePudding user response:
I think you just need to modify your clicked()
method:
def clicked(self, card, touch):
if card.collide_point(*touch.pos):
print(card.children[0].text)
self.manager.current = 'SecondWindow'