【问题标题】:change screen in kivy or kivymd with press button使用按钮更改 kivy 或 kivymd 中的屏幕
【发布时间】:2021-08-13 17:52:10
【问题描述】:

我想通过按下按钮并检查文本框是否为空来更改屏幕。 这是我的 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()

我遇到的错误:

错误:

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

请帮帮我!

请不要阅读文章的其余部分 我不知道该说什么了,因为它在发布时出错! '''看起来你的帖子主要是代码;请添加更多详细信息。'''

【问题讨论】:

    标签: python kivy kivymd


    【解决方案1】:

    您的代码:

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

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

    self.root.current = 'screen2'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-07
      相关资源
      最近更新 更多