如何在Kivy中更新StringProperty变量 [英] How to update StringProperty variable in Kivy

查看:418
本文介绍了如何在Kivy中更新StringProperty变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  1. 在每个屏幕上添加ids
  2. 在按钮(计算)的on_release事件下,调用root.update_info()方法,然后切换到下一个屏幕.
  1. Add ids to each screen
  2. Under on_release event for Button (Calculate), invoke root.update_info() method before switching to the next screen.

摘要

<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()
  3. 中的字符串属性
  1. Add self to the StringProperty in class methods.
  2. Add manager.ids.manual_insert to access string attributes in 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

屏幕默认属性管理器

每个屏幕默认都有一个属性管理器,可为您提供 使用的ScreenManager实例.

Each screen has by default a property manager that gives you the instance of the ScreenManager used.

ScreenManager事件

事件:

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变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆