我有 3 个屏幕,想显示用户在 3ed 屏幕 kivymd 上输入的名称

Posted

技术标签:

【中文标题】我有 3 个屏幕,想显示用户在 3ed 屏幕 kivymd 上输入的名称【英文标题】:i have 3 screens and wanna display the name that the user enter it on the 3ed screen kivymd 【发布时间】:2021-06-22 05:49:00 【问题描述】:

我有 3 个屏幕 第二个包括用户可以输入其姓名的文本字段 并想显示用户在 3ed 屏幕上输入的名称,如标签 像这样 : Welcome +'用户输入的名字' 所以这个问题我很累得到用户输入并显示在anathor屏幕上的值

这是我的代码

WindowManager:
    WelcomeScreen:
    UsernameScreen:
    DobScreen:

<WelcomeScreen>:
    name: 'welcomescreen'
    MDLabel:
        text:'welcome screen'
        font_style:'H2'
        halign:'center'
        pos_hint:'center_y':0.65
    MDFloatingActionButton:
        icon:'android'
        md_bg_color:app.theme_cls.primary_color
        user_font_size: '60sp'
        pos_hint:'center_x':0.5, 'center_y':0.32
        on_press:
            root.manager.current='usernamescreen'
            root.manager.transition.direction='left'
    MDProgressBar:
        value:30
        pos_hint:'center_y':0.02
<UsernameScreen>: 
    name:'usernamescreen'
    MDFloatingActionButton:
        icon:'arrow-left'
        md_bg_color:app.theme_cls.primary_color
        pos_hint:'center_x':0.15, 'center_y':0.1
        user_font_size: '45sp'
        on_press:
            root.manager.current='welcomescreen'
            root.manager.transition.direction='right'
    MDFloatingActionButton:
        id:disabled_button
        disabled:True
        icon:'arrow-right'                
        md_bg_color:app.theme_cls.primary_color
        pos_hint:'center_x':0.85, 'center_y':0.1
        user_font_size: '45sp'
        on_press:
            root.manager.current='dob'
            root.manager.transition.direction='left'
    MDProgressBar:
        value:60
        pos_hint:'center_y':0.02
    MDLabel:
        text:'username'                                 
        font_style:'H2'
        halign:'center'
        pos_hint:'center_y':0.75
    MDTextField:
        id:username_text_field  
        pos_hint:'center_x':0.5, 'center_y':0.6
        size_hint:(0.7,0.1)           
        hint_text:'user name'
        icon_right:'account' 
        icon_right_color:app.theme_cls.primary_color    
    MDFloatingActionButton:   
        icon:'account-plus'   
        md_bg_color:app.theme_cls.primary_color 
        pos_hint:'center_x':0.5, 'center_y':0.35    
        user_font_size: '50sp'
        on_press:
            app.check_username()
<DobScreen>:    
    name:'dob'
    MDLabel:
        id:profile_name
        text:'dob screen'                                 
        font_style:'H2'
        halign:'center'
        pos_hint:'center_y':0.75
            
                                                               
from kivy.config import Config
Config.set('graphics', 'width', '300') 
Config.set('graphics', 'height', '560')
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDFlatButton,MDFloatingActionButton
from kivy.storage.jsonstore import JsonStore
from kivymd.uix.picker import MDDatePicker


class WelcomeScreen(Screen):
    pass
class UsernameScreen(Screen):
    pass
class DobScreen(Screen):
    pass
class WindowManager(ScreenManager):
    pass

class NewApp(MDApp):
    def build(self):
        self.strng=Builder.load_file('screens.kv')
        return self.strng
    def check_username(self):
        self.username_text=self.strng.get_screen('usernamescreen').ids.username_text_field.text
        username_check_false=True
        try:
            int(self.username_text)
        except:
            username_check_false=False
        if username_check_false or self.username_text.split() == []:
            cancel_btn_username_dialogue = MDFlatButton(text='Retry',on_release=self.close_username_dialogue)
            self.dialog= MDDialog(title='Invalid Username',text='plase enter ur name',size_hint=(0.7,0.2),buttons=[cancel_btn_username_dialogue])
            self.dialog.open()
        else:
            self.strng.get_screen('usernamescreen').ids.disabled_button.disabled =False
            
    
NewApp().run()

【问题讨论】:

【参考方案1】:

在用户名屏幕内

class UsernameScreen(widget):
    def __init__(self, **kwargs):
        supper().__init__(**kwargs)
    def check_username(self):
        self.username_field = self.ids.ID_username_field

在 file.kv 中使用它

<UsernameScreen>
     MDTextField:
        id: ID_username_text_field

在 DobScreen 类中

class DobScreen(widget):
    username = StringProperty()
    def __init__(self, **kwargs):
        supper().__init__(**kwargs) 
        self.username = Usernamescreen.username_field    #### the variable u need from the second class(second screen)

在显示标签的kv文件中

<DobScreen>
   #### ur widgets###
   #########
   Label:
       text: root.username

建议:尝试在 python 上更可靠一点,并在 kv lang 和 python 之间分担工作

【讨论】:

以上是关于我有 3 个屏幕,想显示用户在 3ed 屏幕 kivymd 上输入的名称的主要内容,如果未能解决你的问题,请参考以下文章

如何在需要的地方显示和隐藏标签栏?

用户返回上一个屏幕而不是显示侧面菜单

如何使用 kivy StringProperty?

使用 RecyclerView 在屏幕上显示我的列表的特定数量

将视图转换为位图而不在 Android 中显示?

如何将屏幕垂直分成三部分?