Python 如何在Kivy中更新StringProperty变量
我在更新Kivy中的屏幕时遇到问题 我试图在Kivy中创建一个登录屏幕,我想将用户的所有信息存储在不同的变量中,这样算法就可以解析它们,给它们打分,并给它们分配一个位置 基本上是:填充信息-->算法计算-->进入x房间 我的问题是,在计算之后,“转到房间”屏幕不会更新,这意味着人的名字和房间是初始值 以下是简化代码: Python:Python 如何在Kivy中更新StringProperty变量,python,python-2.7,kivy,kivy-language,Python,Python 2.7,Kivy,Kivy Language,我在更新Kivy中的屏幕时遇到问题 我试图在Kivy中创建一个登录屏幕,我想将用户的所有信息存储在不同的变量中,这样算法就可以解析它们,给它们打分,并给它们分配一个位置 基本上是:填充信息-->算法计算-->进入x房间 我的问题是,在计算之后,“转到房间”屏幕不会更新,这意味着人的名字和房间是初始值 以下是简化代码: Python: class Manual_insert(Screen): name_x = StringProperty() room = ""
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
:
名称:“手册”
标签:
文本输入:
id:全名
正文:“全名”
位置:root.center_x-(自身大小[0]*0.5),root.top*0.75
按钮:
正文:“计算”
发布时:app.root.current=“first”
发布时:root.update\u info()
位置:root.center_x-(自身大小[0]*0.5),root.top*0.05
背景颜色:[3,1,0.2,9]
:
名字:“第一”
按钮:
文本:root.names
发布时:app.root.current=“欢迎”
背景颜色:[1.78,3,2.91,0.8]
字体大小:50
当我运行此程序时,我会在控制台上获得名称,但在应用程序屏幕上,我会获得:
<StringProperty name=>, your assigned room is:
,您指定的房间是:
感谢您所做的一切,如果我不够清楚,请不要犹豫告诉我。解决方案
有关详细信息,请参阅代码片段和示例
kv文件
id
on\u release
事件下,在切换到下一屏幕之前调用root.update\u info()
方法on_pre_enter: ()
即将使用屏幕时激发的事件:输入
动画开始
例子
main.py
试验电压(千伏)
#:kivy 1.11.0
:
手动插入:
id:手动插入
第一个房间:
id:第一个房间
欢迎:
欢迎光临
:
名称:“手册”
盒子布局:
标签:
正文:“全名”
文本输入:
id:全名
位置:root.center_x-(自身大小[0]*0.5),root.top*0.75
按钮:
正文:“计算”
位置:root.center_x-(自身大小[0]*0.5),root.top*0.05
背景颜色:[3,1,0.2,9]
发布时:
root.update_info()
root.manager.current=“第一”
:
名字:“第一”
按钮:
文本:root.names
发布时:root.manager.current=“欢迎”
背景颜色:[1.78,3,2.91,0.8]
字体大小:50
:
标签:
文字:“欢迎!”
输出
Ikolim非常感谢您!有一点:on_pre_enter无法正常工作,因为初始文本只会在您第二次填写表单时出现。相反,我只是用在了_enter上,效果很好!非常感谢@PaturonChimere:在您的kv文件中,在按钮(计算)的释放事件下,您必须在切换屏幕之前调用
root.update\u info()
方法。请记住投票和/或将答案标记为已接受,嗯!非常感谢。
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: ()
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!'