在kivy中的屏幕之间传递变量,在本例中

问题描述 投票:0回答:1

我知道对此有很多疑问。 我是 kivy 的新手,我发现有很多方法可以做到同样的事情。 我试图以良好的结构编写(使用我在这里找到的一些代码作为示例)。

我想在这种情况下传递一个变量(令牌)。

main.py / central.kv
login.py / login.kv
settings.py / settings.kv

这是main.py:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager
import login, settings

# hierarhy:
#   ScreensSample (App)
#   |- MyScreens (ScreenManager)
#      |- LoginScreen (Screen)
#      |- SettingsScreen (Screen)

class MyScreens(ScreenManager):
    def screen_manager_method(self):
        print('Hello from screen manager')

class Central(App):
    def app_method(self):
        print('Hello from app Tablero')


Central().run()

这是central.kv:

#:include login.kv
#:include settings.kv

MyScreens:
    LoginScreen:
    SettingsScreen:

这是登录.py:

from kivy.app import App
from kivy.uix.screenmanager import Screen


class LoginScreen(Screen):
    def do_login(token):
        print('Hello from Login')
        print("El token es: ", token)

这是登录.kv:

<LoginScreen>:
    name: 'login'

    BoxLayout:
        orientation: 'vertical'

        Label:
            text: f'I am {root.name}'

        BoxLayout:
            orientation: 'vertical'
            
            Label:
                text:'Ingresar Token'
                font_size: 20

            TextInput:
                id:token
                multiline: False
                font_size: 20
        Button:
            text: 'Guardar'
            font_size: 24
            on_press: root.do_login(token.text)

这是设置.py:

from kivy.uix.screenmanager import Screen

class SettingsScreen(Screen):
    def screen_method(self):
        print('Hello from Settings screen_method')

setting.kv 现在是空的,因为我只想获得登录令牌,但我不知道如何。

您能告诉我这方面的建议吗? 我还想以用户无法访问的方式将此令牌保存到手机中,以避免每次打开应用程序时多次登录,并确保安全。

非常感谢!

kivy
1个回答
0
投票

为了方便测试,我把你的东西放到一个文件里。 我在主对象中放置了一个值。 我认为这里最有用的想法是您可以使用 App.get_running_app() 获取对主应用程序的引用

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager
from kivy.lang import Builder

kv_string = """
<LoginScreen>:
    name: 'login'

    BoxLayout:
        orientation: 'vertical'

        Label:
            text: f'I am {root.name}'

        BoxLayout:
            orientation: 'vertical'
            
            Label:
                text:'Ingresar Token'
                font_size: 20

            TextInput:
                id:token
                multiline: False
                font_size: 20
        Button:
            text: 'Guardar'
            font_size: 24
            on_press: root.do_login(token.text)
MyScreens:
    LoginScreen:
    SettingsScreen:
"""

# hierarchy:
#   ScreensSample (App)
#   |- MyScreens (ScreenManager)
#      |- LoginScreen (Screen)
#      |- SettingsScreen (Screen)


class SettingsScreen(Screen):
    def screen_method(self):
        print(f'Hello from Settings screen_method {self}\n{type(self)}')


class LoginScreen(Screen):
    def do_login(self, token):
        print('Hello from Login')
        print(f'El token es:  {self}\n{type(self)}')
        app = App.get_running_app()
        print(type(app))
        print(app)
        app.shared_token = token
        print(f'the token is {app.shared_token}')


class MyScreens(ScreenManager):
    def screen_manager_method(self):
        print(f'Hello from screen manager {self} {type(self)}\n{App.get_running_app().shared_token}')


class Central(App):
    shared_token: str

    def __init__(self, argument_to_pass, **kwargs):
        super().__init__(**kwargs)
        self.shared_token = ""
        print(f'{argument_to_pass}\n\n\n{self.shared_token}')

    def build(self):
        Builder.load_string(kv_string)
        return LoginScreen()

    def app_method(self):
        print(f'Hello from app Tablero {self}')


Central(argument_to_pass="program_options").run()
© www.soinside.com 2019 - 2024. All rights reserved.