我在Kivy中更新屏幕时遇到问题。
我试图在Kivy中制作一个登录屏幕,我想将用户的所有信息存储在不同的变量中,以便算法可以解析它们并给他们打分,并为其分配一个去的地方。
基本上是:填充信息---->算法会计算---->转到x室。
我的问题是,计算之后,“去房间”屏幕没有更新,这意味着人名和房间是初始值。
这是简化的代码:
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:
感谢您所做的一切,如果我不太清楚,请随时告诉我。
答案 0 :(得分:0)
有关详细信息,请参见代码片段和示例。
ids
on_release
事件下,调用root.update_info()
方法,然后切换到下一个屏幕。<ScreenManagement>:
Manual_insert:
id: manual_insert
First_room:
id: first_room
Welcome:
id: welcome
self
添加到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
每个屏幕默认都有一个属性管理器,可为您提供 使用的ScreenManager实例。
事件:
on_pre_enter: ()
将要使用屏幕时触发的事件:进入 动画开始了。
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()
#: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!'