简体   繁体   中英

KIVY: How to print list of data onto second screen but not on first screen

My screen manager is working and I am able to display the information I want on both of my screens but on my FirstWindow , I would just like to show the name of the place ( in this example, A ). Then when a user clicks on one of the MDCard it takes them to the SecondWindow where it will display the rest of the information for the place they specifically picked (in this example A AA AAA ).

I've tried to create a list but when you get to the second screen it always just returns what the last set of elements are from the testz list.

I've minimised my code as much as I could for the example, I've been trying this for days. Thanks for the help!

*.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"))
        testz = [["A","AA","AAA"],["B","BB","BBB"],["C","CC","CCC"]]
        for x in testz:
            name = x[0]
            address = x[1]
            street = x[2]
            card = MDCard(orientation='vertical', size_hint=(1, None), height=200 ,padding=(20,0),ripple_behavior= True,radius=[5, ], elevation = 18)
            card.add_widget(MDLabel(text=name, halign= "center"))
            card.add_widget(MDLabel(text=address, halign= "left"))
            card.add_widget(MDLabel(text=street, halign= "left"))
            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)
        self.ids.house.text = "MAP"
    
    def clicked(self, card, touch):
        self.create_scrollview
        if card.collide_point(*touch.pos):
            FirstWindow.clicked.place_name = card.children[2].text
            FirstWindow.clicked.place_address = card.children[1].text
            FirstWindow.clicked.place_postcode = card.children[0].text
            self.manager.current = 'SecondWindow'
            
class SecondWindow(Screen):
    def on_pre_enter(self):
        self.ids.place_name.text = FirstWindow.clicked.place_name
        self.ids.place_address.text = FirstWindow.clicked.place_address
        self.ids.place_postcode.text = FirstWindow.clicked.place_postcode
    
    def test(self):
        self.manager.current = 'FirstWindow'

class ScreenSwitcher(ScreenManager):
    pass

class MainScreen(GridLayout):
    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"
        return MainScreen()

if __name__ == '__main__':
    NearMeApp().run()

*.kv

#:import hex kivy.utils.get_color_from_hex
#:kivy 1.10.1

<ToolBar@BoxLayout>:
    size_hint_y:None
    height: 50
    pos_hint: {'center_x': 0.5, 'top':1.0}
    canvas:
        Color:
            rgba: hex('#171717')
        Rectangle:
            pos: self.pos
            size: self.size
    Label:
        text:"NearMeApplication"

<FirstWindow>:
    name:"FirstWindow"
    view: view
   # NavBar:
    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:
                    id:house
                    halign: "center"
                    canvas.before:
                        Color:
                            rgba: 1, 1, 1, 1

            ScrollView:
                id: view 
                canvas.before:
                    Color:
                        rgba: 1, 1, 0, 1
                    #Line:
                        #width: 2
                        #rectangle: self.x, self.y, self.width, self.height
<SecondWindow>:
    name:"SecondWindow"
    AnchorLayout:
        size:1,1
        anchor_x: "center"
        anchor_y: "center"
        canvas.before:
            Color:
                rgba: .2, .2, .2, 1
            Rectangle:
                pos: self.pos
                size: self.size

        MDCard:
            orientation: "vertical"
            padding: "8dp"
            size_hint: 1,0.5
            pos_hint:{'top': 0.1,'right':1}
            radius:[5, ]
            elevation: 18

            MDLabel:
                id:place_name
                halign: "center"

            MDSeparator:
                height: "1dp"
            
            MDLabel:
                id:place_address
                halign: "center"
            MDLabel:
                id:place_postcode
                halign: "center"

    Button:
        text:"<--"
        size: 75, 50
        size_hint: None, None # <---
        on_press:
            root.manager.transition.direction = "left"
            root.test()
            #app.root.current = "FirstWindow"

# To keep the nav bar on all screens
<MainScreen>:
    rows: 2
    size_hint: 1, 1
    ToolBar:
    ScreenSwitcher:
        id: SS
        FirstWindow:
            name:'FirstWindow'
        SecondWindow:
            name:'SecondWindow'

You can just store the information in the MDCard rather than in MDLabels , like this:

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"))
    testz = [["A", "AA", "AAA"], ["B", "BB", "BBB"], ["C", "CC", "CCC"]]
    for x in testz:
        name = x[0]
        address = x[1]
        street = x[2]
        card = MDCard(orientation='vertical', size_hint=(1, None), height=200, padding=(20, 0),
                      ripple_behavior=True, radius=[5, ], elevation=18)

        # store the needed info
        card.name = name
        card.address = address
        card.street = street

        card.add_widget(MDLabel(text=name, halign="center"))

        # don't add unwanted MDLabels
        # card.add_widget(MDLabel(text=address, halign="left"))
        # card.add_widget(MDLabel(text=street, halign="left"))

        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)
    self.ids.house.text = "MAP"

Then in the clicked() method, you can use that stored information:

def clicked(self, card, touch):
    self.create_scrollview
    if card.collide_point(*touch.pos):
        FirstWindow.clicked.place_name = card.name
        FirstWindow.clicked.place_address = card.address
        FirstWindow.clicked.place_postcode = card.street
        self.manager.current = 'SecondWindow'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM