使用按钮在 kivy 或 kivymd 中更改屏幕

change screen in kivy or kivymd with press button

我想通过按下按钮并检查文本框不为空来更改屏幕。 这是我的 kv 文件:

test.kv:

ScreenManager:
id: sc_m
Screen:
    name: 'welcome'           
    MDLabel:
        text: 'WeLCOME'
        halign: 'center'
        font_style: 'H3'
        color: (1, 1, 1, 1)
        pos_hint: {'center_x': 0.5, 'center_y':0.8}
    MDTextField:
        id: textbox
        hint_text: "The text box should not be empty"
        text:""
        pos_hint: {'center_x': .5, 'center_y': .5}
    MDRoundFlatIconButton:
        text: "OK"
        icon: "folder"
        pos_hint: {'center_x': .5, 'center_y': .3}
        on_press: app.callback()
    
Screen:
    name:'screen2'
    MDLabel:
        text: 'WeLCOME'
        halign: 'center'
        font_style: 'H3'
        color: (1, 1, 1, 1)
        pos_hint: {'center_x': 0.5, 'center_y':0.8}

这是我的 python 代码:

main.py:

from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivymd.app import MDApp


class MainApp(MDApp):
    
    def build(self):
        self.theme_cls.theme_style="Dark"
        self.theme_cls.primary_palette="Cyan"
        return Builder.load_file('test.kv')
    
    def callback(self):
        text = self.root.ids.textbox.text
        if len(text)==0:
            print('The text box should not be empty')
        else:
            self.root.ids.sc_m.current='screen2'


    
MainApp().run()

我的错误是:

error:

AttributeError: 'super' object has no attribute '__getattr__'

请帮帮我!

请不要阅读本文的其余部分 我不知道该说什么了,因为发帖时出错了! '''It looks like your post is mostly code; please add some more details.'''

您的代码:

self.root.ids.sc_m.current='screen2'

正在尝试使用不存在的 id。您不能为规则的根对象创建 ids 条目(ids 仅包括较低级别的小部件)。但是,由于 ScreenManager 是根小部件,您无论如何都不需要使用 ids 访问它。只需将上面的代码替换为:

self.root.current = 'screen2'