如何在Kivy中更新StringProperty变量

时间:2018-07-26 14:38:11

标签: python python-2.7 kivy kivy-language

我在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: 

感谢您所做的一切,如果我不太清楚,请随时告诉我。

1 个答案:

答案 0 :(得分:0)

解决方案

有关详细信息,请参见代码片段和示例。

kv文件

  1. 在每个屏幕上添加ids
  2. 在“按钮(计算)”的on_release事件下,调用root.update_info()方法,然后切换到下一个屏幕。

摘要

<ScreenManagement>:
    Manual_insert:
        id: manual_insert

    First_room:
        id: first_room

    Welcome:
        id: welcome

Python代码

  1. 在类方法中将self添加到StringProperty。
  2. 添加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实例。

ScreenManager Events

  

事件:

on_pre_enter: ()
     

将要使用屏幕时触发的事件:进入   动画开始了。

示例

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!'

输出

Img01