首先,我的代码:
main.py
from kivy.app import App
from kivy.graphics import Color
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.listview import ListItemButton
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import StringProperty, NumericProperty, ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
class ShowStartScreen(Screen):
pass
class CurrLocation(Screen):
pass
class SearchHeight(Screen):
pass
class SearchMountain(Screen):
pass
class SearchLocation(Screen):
pass
class SearchSummit(Screen):
pass
class SearchSummary(Screen):
pass
class SearchScreenManager(ScreenManager):
pass
class MountainClimberApp(App):
def build(self):
return SearchScreenManager()
def get_search_height(self):
scr_manager = self.root
hScr = scr_manager.get_screen('height')
hInput = StringProperty(hScr.ids['searchHigherThan'])
print "height: {}".format(hInput)
if __name__ == "__main__":
MountainClimberApp().run()
我的kv档案:
#: import main main
MountainClimberRoot:
<MountainClimberRoot>:
ShowStartScreen
<ShowStartScreen@BoxLayout>:
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
Image:
source: 'background.png'
Image:
source: 'logo.png'
center_x: root.width / 2
height: 1000
width: 400
size_hint_y: None
size_hint_x: None
GridLayout:
cols: 1
center_x: root.width / 2
height: 140
size_hint_y: None
Button:
text: "SUCHE"
height: 60
width: 130
size_hint_x: None
size_hint_y: None
font_size: 20
background_color: 0.2, 0.8, 0.4, 0.9
on_press: app.root.showSearchHeight()
BoxLayout:
size_hint_y: 10
Button:
text: "STANDORT"
height: 60
width: 130
size_hint_x: None
size_hint_y: None
font_size: 20
background_color: 0.2, 0.8, 0.4, 0.9
on_press: app.root.showCurrLocation()
BoxLayout:
size_hint_y: 10
<SearchHeight@BoxLayout>:
on_right_to_left_line: app.root.showSearchMountain()
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
Image:
source: 'background.png'
BoxLayout:
pos: 0, 550
height: 60
width: root.width
size_hint_x: None
size_hint_y: None
Button:
text: "START"
font_size: 20
background_color: 0.2, 0.8, 0.4, 0.1
on_press: app.root.showStartScreen()
Label:
text: "SUCHE"
font_size: 20
background_color: 0.2, 0.8, 0.4, 0.1
BoxLayout:
center_x: (root.width / 2) - 60
Image:
source: 'MC_icons_01.png'
height: 700
width: 400
size_hint_y: None
size_hint_x: None
BoxLayout:
pos: 0, 200
height: 40
center_x: (root.width / 2) + 15
width: root.width
size_hint_x: None
size_hint_y: None
Label:
width: 40
size_hint_x: None
text: "höher als "
color: 0, 0, 1, 1
font_size: 20
TextInput:
width: 60
size_hint_x: None
id: searchHigherThan
focus: True
multiline: False
Label:
width: 30
size_hint_x: None
text: "m"
color: 0, 0, 1, 1
font_size: 20
BoxLayout:
pos: 0, 150
height: 40
center_x: (root.width / 2) + 25
width: root.width
size_hint_x: None
size_hint_y: None
Label:
width: 40
size_hint_x: None
text: "niedriger als "
color: 0, 0, 1, 1
font_size: 20
TextInput:
width: 60
size_hint_x: None
id: searchLowerThan
focus: True
multiline: False
Label:
width: 30
size_hint_x: None
text: "m"
color: 0, 0, 1, 1
font_size: 20
BoxLayout:
pos: 0, 100
height: 40
center_x: (root.width / 2) - 95
width: root.width
size_hint_y: None
size_hint_x: None
Label:
width: 100
size_hint_x: None
text: "zwischen"
color: 0, 0, 1, 1
font_size: 20
TextInput:
width: 60
size_hint_x: None
id: searchBetweenLower
focus: True
multiline: False
Label:
width: 60
size_hint_x: None
text: "und"
color: 0, 0, 1, 1
font_size: 20
TextInput:
width: 60
size_hint_x: None
id: searchBetweenHigher
focus: True
multiline: False
Label:
width: 30
size_hint_x: None
text: "m"
color: 0, 0, 1, 1
font_size: 20
BoxLayout:
pos: 0, 25
center_x: root.width / 2
Button:
text: "LOS!"
font_size: 20
height: 60
width: 130
size_hint_x: None
size_hint_y: None
background_color: 0.2, 0.8, 0.4, 0.9
on_press: app.root.showSearchSummary()
BoxLayout:
canvas:
Color:
rgba: 0.5, 1, 0.5, 0.6
Ellipse:
pos: (root.width / 2) - 20, 10
size: [self.height - 90, self.height -90]
Color:
rgba: 0, 0, 1, 0.2
Ellipse:
pos: root.width / 2, 10
size: [self.height - 90, self.height - 90]
Ellipse:
pos: (root.width / 2) + 20, 10
size: [self.height - 90, self.height - 90]
Ellipse:
pos: (root.width / 2) + 40, 10
size: [self.height - 90, self.height - 90]
<SearchMountain@BoxLayout>:
on_right_to_left_line: app.root.showSearchLocation()
on_left_to_right_line: app.root.showSearchHeight()
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
Image:
source: 'background.png'
BoxLayout:
pos: 0, 550
height: 60
width: root.width
size_hint_x: None
size_hint_y: None
Button:
text: "START"
font_size: 20
background_color: 0.2, 0.8, 0.4, 0.1
on_press: app.root.showStartScreen()
Label:
text: "SUCHE"
font_size: 20
background_color: 0.2, 0.8, 0.4, 0.1
BoxLayout:
center_x: (root.width / 2) - 120
Image:
source: 'MC_icons_02.png'
height: 700
width: 400
size_hint_y: None
size_hint_x: None
BoxLayout:
pos: 0, 200
height: 40
center_x: (root.width / 2) + 15
width: root.width
size_hint_x: None
size_hint_y: None
Label:
width: 40
size_hint_x: None
text: "Exakt "
color: 0, 0, 1, 1
font_size: 20
TextInput:
width: 120
size_hint_x: None
id: searchMountainExact
focus: True
multiline: False
BoxLayout:
pos: 0, 150
height: 40
center_x: (root.width / 2) + 25
width: root.width
size_hint_x: None
size_hint_y: None
Label:
width: 40
size_hint_x: None
text: "Teilsuche "
color: 0, 0, 1, 1
font_size: 20
TextInput:
width: 120
size_hint_x: None
id: searchMountainPart
focus: True
multiline: False
BoxLayout:
pos: 0, 25
center_x: root.width / 2
Button:
text: "LOS!"
font_size: 20
height: 60
width: 130
size_hint_x: None
size_hint_y: None
background_color: 0.2, 0.8, 0.4, 0.9
on_press: app.root.showSearchSummary()
BoxLayout:
canvas:
Color:
rgba: 0, 0, 1, 0.2
Ellipse:
pos: (root.width / 2) - 20, 10
size: [self.height - 90, self.height -90]
Color:
rgba: 0.5, 1, 0.5, 0.6
Ellipse:
pos: root.width / 2, 10
size: [self.height - 90, self.height - 90]
Color:
rgba: 0, 0, 1, 0.2
Ellipse:
pos: (root.width / 2) + 20, 10
size: [self.height - 90, self.height - 90]
Ellipse:
pos: (root.width / 2) + 40, 10
size: [self.height - 90, self.height - 90]
<SearchLocation@BoxLayout>:
on_right_to_left_line: app.root.showSearchSummit()
on_left_to_right_line: app.root.showSearchMountain()
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
Image:
source: 'background.png'
BoxLayout:
pos: 0, 550
height: 60
width: root.width
size_hint_x: None
size_hint_y: None
Button:
text: "START"
font_size: 20
background_color: 0.2, 0.8, 0.4, 0.1
on_press: app.root.showStartScreen()
Label:
text: "SUCHE"
font_size: 20
background_color: 0.2, 0.8, 0.4, 0.1
BoxLayout:
center_x: (root.width / 2) - 120
Image:
source: 'MC_icons_03.png'
height: 600
width: 400
size_hint_y: None
size_hint_x: None
BoxLayout:
pos: 0, 200
height: 40
center_x: (root.width / 2) + 15
width: root.width
size_hint_x: None
size_hint_y: None
Label:
width: 40
size_hint_x: None
text: "Exakt "
color: 0, 0, 1, 1
font_size: 20
TextInput:
width: 120
size_hint_x: None
id: searchLocationExact
focus: True
multiline: False
BoxLayout:
pos: 0, 150
height: 40
center_x: (root.width / 2) + 25
width: root.width
size_hint_x: None
size_hint_y: None
Label:
width: 40
size_hint_x: None
text: "Teilsuche "
color: 0, 0, 1, 1
font_size: 20
TextInput:
width: 120
size_hint_x: None
id: searchLocationPart
focus: True
multiline: False
BoxLayout:
pos: 0, 25
center_x: root.width / 2
Button:
text: "LOS!"
font_size: 20
height: 60
width: 130
size_hint_x: None
size_hint_y: None
background_color: 0.2, 0.8, 0.4, 0.9
on_press: app.root.showSearchSummary()
BoxLayout:
canvas:
Color:
rgba: 0, 0, 1, 0.2
Ellipse:
pos: (root.width / 2) - 20, 10
size: [self.height - 90, self.height -90]
Ellipse:
pos: root.width / 2, 10
size: [self.height - 90, self.height - 90]
Color:
rgba: 0.5, 1, 0.5, 0.6
Ellipse:
pos: (root.width / 2) + 20, 10
size: [self.height - 90, self.height - 90]
Color:
rgba: 0, 0, 1, 0.2
Ellipse:
pos: (root.width / 2) + 40, 10
size: [self.height - 90, self.height - 90]
<SearchSummit@BoxLayout>:
on_left_to_right_line: app.root.showSearchLocation()
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
Image:
source: 'background.png'
BoxLayout:
pos: 0, 550
height: 60
width: root.width
size_hint_x: None
size_hint_y: None
Button:
text: "START"
font_size: 20
background_color: 0.2, 0.8, 0.4, 0.1
on_press: app.root.showStartScreen()
Label:
text: "SUCHE"
font_size: 20
background_color: 0.2, 0.8, 0.4, 0.1
BoxLayout:
center_x: (root.width / 2) - 120
Image:
source: 'MC_icons_04.png'
height: 600
width: 400
size_hint_y: None
size_hint_x: None
BoxLayout:
pos: 0, 200
height: 40
center_x: (root.width / 2) + 15
width: root.width
size_hint_x: None
size_hint_y: None
Label:
width: 40
size_hint_x: None
text: "Gipfelart "
color: 0, 0, 1, 1
font_size: 20
TextInput:
width: 120
size_hint_x: None
id: sarchSummitType
focus: True
multiline: False
BoxLayout:
pos: 0, 100
height: 40
center_x: (root.width / 2)
width: root.width
size_hint_x: None
size_hint_y: None
Label:
cinter_x: root.width / 2 + 20
width: 40
size_hint_x: None
text: "'GGipfel', 'HGipfel', 'KGipfel', 'Massiv' oder 'Grat'"
color: 0, 0, 1, 1
font_size: 20
BoxLayout:
pos: 0, 25
center_x: root.width / 2
Button:
text: "LOS!"
font_size: 20
height: 60
width: 130
size_hint_x: None
size_hint_y: None
background_color: 0.2, 0.8, 0.4, 0.9
on_press: app.root.showSearchSummary()
BoxLayout:
canvas:
Color:
rgba: 0, 0, 1, 0.2
Ellipse:
pos: (root.width / 2) - 20, 10
size: [self.height - 90, self.height -90]
Ellipse:
pos: root.width / 2, 10
size: [self.height - 90, self.height - 90]
Ellipse:
pos: (root.width / 2) + 20, 10
size: [self.height - 90, self.height - 90]
Color:
rgba: 0.5, 1, 0.5, 0.6
Ellipse:
pos: (root.width / 2) + 40, 10
size: [self.height - 90, self.height - 90]
<CurrLocation@BoxLayout>:
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
Image:
source: 'background.png'
BoxLayout:
pos: 0, 550
height: 60
width: root.width
size_hint_x: None
size_hint_y: None
Button:
text: "START"
font_size: 20
background_color: 0.2, 0.8, 0.4, 0.1
on_press: app.root.showStartScreen()
Label:
text: "STANDORT"
font_size: 20
background_color: 0.2, 0.8, 0.4, 0.1
<SearchSummary@BoxLayout>:
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
Image:
source: 'background.png'
BoxLayout:
pos: 0, 550
height: 60
width: root.width
size_hint_x: None
size_hint_y: None
Button:
text: "START"
font_size: 20
background_color: 0.2, 0.8, 0.4, 0.1
on_press: app.root.showStartScreen()
Label:
text: "ZUSAMMENFASSUNG"
font_size: 20
background_color: 0.2, 0.8, 0.4, 0.1
我知道它很多,但它相对容易理解。这是我尝试与两位同事一起实施的应用程序的代码。它显示了一个开始屏幕,一个位置屏幕和四个搜索屏幕。在所有搜索屏幕中,您可以输入文本或值。在摘要屏幕上,用户应该看到他输入的所有参数以及应该用于处理其查询的参数。如何从摘要小部件中访问四个搜索小部件中定义的元素?
帮助将不胜感激!
Tnat的所有人!
菲利普
答案 0 :(得分:1)
如果SearchSummary
从Search类继承,那该怎么办? Python支持多重继承。
<强>实施例强>
Class A(object):
size = 'big'
colour = 'green'
Class B(object):
weight = 'heavy'
says = 'how do?'
Class C(object):
mute = True
legs = 4
class D(A, B, C): # multiple inheritance
pass
>>> print D.weight
'heavy'
>>> print D.legs
4
>>> print D.size
'big'
使用kivy:
class SearchHeight(Widget):
num = NumericProperty(10)
class SearchMountain(Widget):
string = StringProperty('hi there')
class SearchLocation(Widget):
listy = ListProperty([1,2,3,3,4])
class SearchSummary(A, B, C): # multiple inheritance
def __init__(self):
# SearchSummary now has access to the other classes attributes
print self.listy
print self.string
print self.num
class MyApp(App):
def build(self):
return D()
if __name__ == "__main__":
MyApp().run()
<强>输出:强>
[1, 2, 3, 3, 4]
hi there
10
答案 1 :(得分:0)
使用kivy开发人员的screenmanager类:http://kivy.org/docs/api-kivy.uix.screenmanager.html
您不仅可以在各种屏幕上访问子窗口小部件,还可以利用已经内置的各种复杂行为,例如屏幕之间的动画过渡。
这里提供了一个很棒的教程:https://www.youtube.com/watch?v=xx-NLOg6x8o(&#34; Kivy速成课程14:使用ScreenManager&#34;)
screenmanager有get_screen(name)
方法可用于访问屏幕,您可以使用ids来引用子窗口小部件。
这允许访问,但通常更好的做法是将自定义属性用于基本窗口小部件(在本例中为screenmanager),以允许在没有ID的情况下直接访问。对于textinput,您可以创建一个ObjectProperty并将其分配给kv文件中目标textinput的id,或者您可以创建一个StringProperty / NumericProperty并将其绑定到textinput的text属性。