如何在 Kivy 中更新 StringProperty 变量
How to update StringProperty variable in Kivy
我在 Kivy 中更新屏幕时遇到问题。
我正在尝试在 Kivy 中制作一个登录屏幕,我想将用户的所有信息存储在不同的变量中,以便算法可以解析它们并给它们打分,然后分配他们是个好去处。
基本上就是:填写信息---->算法计算---->去x房间
我的问题是在计算之后,"go to room"屏幕没有更新,也就是说人名和房间都是初始值。
这里是简化代码:
Python:
class Manual_insert(Screen):
name_x = StringProperty()
room = ""
def update_info(self):
name_x = self.ids.full_name.text
print name_x
class First_room(Screen):
names = StringProperty(str(Manual_insert.name_x)+ ", your assigned room is: " + str(Manual_insert.room))
和 KIVY 文件:
<Manual_insert>:
name: "manual"
Label:
TextInput:
id: full_name
text: "Full Name"
pos: root.center_x - (self.size[0] * 0.5), root.top * 0.75
Button:
text: "Calculate"
on_release: app.root.current = "first"
on_release: root.update_info()
pos: root.center_x - (self.size[0] * 0.5) , root.top * 0.05
background_color: [3,1,0.2,.9]
<First_room>:
name: 'first'
Button:
text: root.names
on_release: app.root.current = "welcome"
background_color: [1.78,3,2.91,0.8]
font_size: 50
当我 运行 这个时,我在控制台上得到了名字,但在应用程序屏幕上我得到:
<StringProperty name=>, your assigned room is:
谢谢你所做的一切,如果我不够清楚,请不要犹豫告诉我。
解决方案
详情请参考代码片段和示例。
kv 文件
- 每个屏幕添加
ids
- 在按钮(计算)的
on_release
事件下,在切换到下一个屏幕之前调用 root.update_info()
方法。
片段
<ScreenManagement>:
Manual_insert:
id: manual_insert
First_room:
id: first_room
Welcome:
id: welcome
Python代码
- 将
self
添加到 class 方法中的 StringProperty。
- 添加
manager.ids.manual_insert
以访问 class Manual_insert()
中的字符串属性
片段
class Manual_insert(Screen):
name_x = StringProperty('')
room = StringProperty('')
def update_info(self):
self.name_x = self.ids.full_name.text
print(self.name_x)
class First_room(Screen):
names = StringProperty('')
def on_pre_enter(self, *args):
self.names = self.manager.ids.manual_insert.name_x + ", your assigned room is: " + self.manager.ids.manual_insert.room
Screen default property manager
Each screen has by default a property manager that gives you the
instance of the ScreenManager used.
Events:
on_pre_enter: ()
Event fired when the screen is about to be used: the entering
animation is started.
例子
main.py
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty
class ScreenManagement(ScreenManager):
pass
class Manual_insert(Screen):
name_x = StringProperty('')
room = StringProperty('')
def update_info(self):
self.name_x = self.ids.full_name.text
print(self.name_x)
class First_room(Screen):
names = StringProperty('')
def on_pre_enter(self, *args):
self.names = self.manager.ids.manual_insert.name_x + ", your assigned room is: " + self.manager.ids.manual_insert.room
class Welcome(Screen):
pass
class TestApp(App):
def build(self):
return ScreenManagement()
if __name__ == "__main__":
TestApp().run()
test.kv
#:kivy 1.11.0
<ScreenManagement>:
Manual_insert:
id: manual_insert
First_room:
id: first_room
Welcome:
id: welcome
<Manual_insert>:
name: "manual"
BoxLayout:
Label:
text: "Full Name"
TextInput:
id: full_name
pos: root.center_x - (self.size[0] * 0.5), root.top * 0.75
Button:
text: "Calculate"
pos: root.center_x - (self.size[0] * 0.5) , root.top * 0.05
background_color: [3,1,0.2,.9]
on_release:
root.update_info()
root.manager.current = "first"
<First_room>:
name: 'first'
Button:
text: root.names
on_release: root.manager.current = "welcome"
background_color: [1.78,3,2.91,0.8]
font_size: 50
<Welcome>:
Label:
text: 'Welcome!'
输出
我在 Kivy 中更新屏幕时遇到问题。
我正在尝试在 Kivy 中制作一个登录屏幕,我想将用户的所有信息存储在不同的变量中,以便算法可以解析它们并给它们打分,然后分配他们是个好去处。
基本上就是:填写信息---->算法计算---->去x房间
我的问题是在计算之后,"go to room"屏幕没有更新,也就是说人名和房间都是初始值。
这里是简化代码:
Python:
class Manual_insert(Screen):
name_x = StringProperty()
room = ""
def update_info(self):
name_x = self.ids.full_name.text
print name_x
class First_room(Screen):
names = StringProperty(str(Manual_insert.name_x)+ ", your assigned room is: " + str(Manual_insert.room))
和 KIVY 文件:
<Manual_insert>:
name: "manual"
Label:
TextInput:
id: full_name
text: "Full Name"
pos: root.center_x - (self.size[0] * 0.5), root.top * 0.75
Button:
text: "Calculate"
on_release: app.root.current = "first"
on_release: root.update_info()
pos: root.center_x - (self.size[0] * 0.5) , root.top * 0.05
background_color: [3,1,0.2,.9]
<First_room>:
name: 'first'
Button:
text: root.names
on_release: app.root.current = "welcome"
background_color: [1.78,3,2.91,0.8]
font_size: 50
当我 运行 这个时,我在控制台上得到了名字,但在应用程序屏幕上我得到:
<StringProperty name=>, your assigned room is:
谢谢你所做的一切,如果我不够清楚,请不要犹豫告诉我。
解决方案
详情请参考代码片段和示例。
kv 文件
- 每个屏幕添加
ids
- 在按钮(计算)的
on_release
事件下,在切换到下一个屏幕之前调用root.update_info()
方法。
片段
<ScreenManagement>:
Manual_insert:
id: manual_insert
First_room:
id: first_room
Welcome:
id: welcome
Python代码
- 将
self
添加到 class 方法中的 StringProperty。 - 添加
manager.ids.manual_insert
以访问class Manual_insert()
中的字符串属性
片段
class Manual_insert(Screen):
name_x = StringProperty('')
room = StringProperty('')
def update_info(self):
self.name_x = self.ids.full_name.text
print(self.name_x)
class First_room(Screen):
names = StringProperty('')
def on_pre_enter(self, *args):
self.names = self.manager.ids.manual_insert.name_x + ", your assigned room is: " + self.manager.ids.manual_insert.room
Screen default property manager
Each screen has by default a property manager that gives you the instance of the ScreenManager used.
Events:
on_pre_enter: ()
Event fired when the screen is about to be used: the entering animation is started.
例子
main.py
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty
class ScreenManagement(ScreenManager):
pass
class Manual_insert(Screen):
name_x = StringProperty('')
room = StringProperty('')
def update_info(self):
self.name_x = self.ids.full_name.text
print(self.name_x)
class First_room(Screen):
names = StringProperty('')
def on_pre_enter(self, *args):
self.names = self.manager.ids.manual_insert.name_x + ", your assigned room is: " + self.manager.ids.manual_insert.room
class Welcome(Screen):
pass
class TestApp(App):
def build(self):
return ScreenManagement()
if __name__ == "__main__":
TestApp().run()
test.kv
#:kivy 1.11.0
<ScreenManagement>:
Manual_insert:
id: manual_insert
First_room:
id: first_room
Welcome:
id: welcome
<Manual_insert>:
name: "manual"
BoxLayout:
Label:
text: "Full Name"
TextInput:
id: full_name
pos: root.center_x - (self.size[0] * 0.5), root.top * 0.75
Button:
text: "Calculate"
pos: root.center_x - (self.size[0] * 0.5) , root.top * 0.05
background_color: [3,1,0.2,.9]
on_release:
root.update_info()
root.manager.current = "first"
<First_room>:
name: 'first'
Button:
text: root.names
on_release: root.manager.current = "welcome"
background_color: [1.78,3,2.91,0.8]
font_size: 50
<Welcome>:
Label:
text: 'Welcome!'