问题描述
我在Kivy中更新屏幕时遇到问题.
I have a problem updating a screen in Kivy.
我试图在Kivy中制作一个登录屏幕,我想将用户的所有信息存储在不同的变量中,以便算法可以解析它们并给他们打分,并为他们分配一个可以去的地方.
I'm trying to make to make a login screen in Kivy, and I'd like to store all the information of the user in different variables so that an algorithm could parse them and give them a score, and assign them a place to go.
基本上是:填充信息---->算法会计算---->转到x室.
Basically it is: filling info----> algorithm calculates ----> go to room x.
我的问题是,计算之后,去房间"屏幕没有更新,这意味着人名和房间是初始值.
My problem is that after the calculation, the "go to room" screen doesn't update, meaning that the name of the person and the room are the initial values.
这是简化的代码:
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
运行此命令时,会在控制台上获得名称,但在应用程序屏幕上会显示:
When I run this I get the name on the console but on the app screen I get:
<StringProperty name=>, your assigned room is:
感谢您所做的一切,如果我不太清楚,请随时告诉我.
Thank you for everything and if I wasn't clear enough don't hesitate to tell me.
推荐答案
解决方案
有关详细信息,请参阅代码片段和示例.
Solution
Please refer to the snippets and example for details.
- 在每个屏幕上添加
ids
- 在按钮(计算)的
on_release
事件下,调用root.update_info()
方法,然后切换到下一个屏幕.
- Add
ids
to each screen - Under
on_release
event for Button (Calculate), invokeroot.update_info()
method before switching to the next screen.
摘要
<ScreenManagement>:
Manual_insert:
id: manual_insert
First_room:
id: first_room
Welcome:
id: welcome
Python代码
- 在类方法中将
self
添加到StringProperty. - 添加
manager.ids.manual_insert
以访问class Manual_insert()
中的字符串属性
- Add
self
to the StringProperty in class methods. - Add
manager.ids.manual_insert
to access string attributes inclass 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
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中更新StringProperty变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!