KIVY:如何将数据列表打印到第二个屏幕而不是第一个屏幕
KIVY: How to print list of data onto second screen but not on first screen
我的屏幕管理器正在工作,我可以在我的两个屏幕上显示我想要的信息,但在我的 FirstWindow
上,我只想显示地点的名称(在这个例子中, A
)。然后,当用户单击其中一个 MDCard
时,它会将他们带到 SecondWindow
,在那里它将显示他们专门选择的地点的其余信息(在本例中为 A AA AAA
)。
我试图创建一个列表,但是当您进入第二个屏幕时,它总是 returns 最后一组元素来自 testz
列表。
为了这个例子,我已经尽可能地减少了我的代码,我已经尝试了好几天了。感谢您的帮助!
*.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'
您可以只将信息存储在 MDCard
而不是 MDLabels
中,如下所示:
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"
然后在clicked()
方法中,您可以使用存储的信息:
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'
我的屏幕管理器正在工作,我可以在我的两个屏幕上显示我想要的信息,但在我的 FirstWindow
上,我只想显示地点的名称(在这个例子中, A
)。然后,当用户单击其中一个 MDCard
时,它会将他们带到 SecondWindow
,在那里它将显示他们专门选择的地点的其余信息(在本例中为 A AA AAA
)。
我试图创建一个列表,但是当您进入第二个屏幕时,它总是 returns 最后一组元素来自 testz
列表。
为了这个例子,我已经尽可能地减少了我的代码,我已经尝试了好几天了。感谢您的帮助!
*.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'
您可以只将信息存储在 MDCard
而不是 MDLabels
中,如下所示:
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"
然后在clicked()
方法中,您可以使用存储的信息:
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'