【问题标题】:AttributeError: ' object has no attribute 'current'AttributeError: \' object has no attribute \'current\'
【发布时间】:2022-12-01 18:56:53
【问题描述】:

Error: AttributeError: 'NoneType' object has no attribute 'current'.

'sign in' button should transition to 'home' page, but gives an error instead. please help me understand what's wrong here. i have several codes and i get different error every time: it used to run perfect till i split login and home screens into two separate kv files and change the python code. it seems im doing something wrong.


from kivymd.app import MDApp
from kivy.core.window import Window
from kivy.lang.builder import Builder
from kivymd.uix.screen import MDScreen
from kivymd.uix.screenmanager import MDScreenManager
Window.size = [320, 600]



class ShopApp(MDApp):

    def build(self):
        self.load_all_kv_files()
        return

    def load_all_kv_files(self):
        sm = MDScreenManager()
        sm.add_widget(Builder.load_file('login.kv'))
        sm.add_widget(Builder.load_file('home.kv'))
        return sm


ShopApp().run()

kv file

MDScreen:
    name: 'shop'
    md_bg_color: 1, 1, 1, 1
    background_normal: ''

    MDLabel:
        text: 'Welcome'
        bold: True
        font_size: 30
        background_normal: ''
        pos_hint: {'center_x': 0.55, 'center_y': 0.9}
        theme_text_color: 'Custom'
        text_color: [0/255, 0/255, 0/255, 1]

    MDFloatLayout:
        id: GREEN
        pos_hint: {'center_y': 0.35}
        size_hint: 1, 0.7
        canvas:
            Color:
                rgba:[0/255, 147/255, 114/255, 1]
            RoundedRectangle:
                pos: self.pos
                size: self.size
                radius: [35, 35, 0, 0 ]

        MDFloatLayout:
            id: WHITE
            size_hint: 0.85, 0.7
            pos_hint: {'center_x': 0.5, 'center_y': 0.45}
            canvas:
                Color:
                    rgb: 1, 1, 1, 1
                RoundedRectangle:
                    size: self.size
                    pos: self.pos
                    radius: [25]

            MDFloatLayout:
                id: USERNAME
                size_hint: .85, .14
                pos_hint: {'center_x': .5, 'center_y': 0.8}
                canvas:
                    Color:
                        rgb: [255/255, 147/255, 114/255, 1]
                    RoundedRectangle:
                        size: self.size
                        pos: self.pos
                        radius: [15]
                TextInput:
                    hint_text: "Username"
                    size_hint: 1, None
                    pos_hint: {'center_x': .5, 'center_y': .5}
                    height: self.minimum_height
                    multiline: False
                    cursor_width: '2sp'
                    cursor_color: 1, 1, 1, 1
                    foreground_color: [0/255, 147/255, 114/255, 1]
                    background_color: 0, 0, 0, 0
                    padding: 15

            MDFloatLayout:
                id: PASSWORD
                size_hint: .85, .14
                pos_hint: {'center_x': .5, 'center_y': 0.6}
                canvas:
                    Color:
                        rgb: [255/255, 147/255, 114/255, 1]
                    RoundedRectangle:
                        size: self.size
                        pos: self.pos
                        radius: [15]
                TextInput:
                    hint_text: "Password"
                    password: True
                    size_hint: 1, None
                    pos_hint: {'center_x': .5, 'center_y': .5}
                    height: self.minimum_height
                    multiline: False
                    cursor_width: '2sp'
                    cursor_color: 1, 1, 1, 1
                    foreground_color: [0/255, 147/255, 114/255, 1]
                    background_color: 0, 0, 0, 0
                    padding: 15

            MDTextButton:
                text: "Forgot password?"
                font_size: 15
                theme_text_color: 'Custom'
                text_color: [0/255, 147/255, 114/255, 1]
                pos_hint: {'center_x': 0.35, 'center_y': 0.48}

            Button:
                text: "Sign In"
                font_size: "15sp"
                size_hint: .4, .11
                pos_hint: {'center_x': 0.5, 'center_y': 0.35}
                background_color: 0, 0, 0, 0
                on_press: root.manager.current = 'home_screen'
                canvas.before:
                    Color:
                        rgba: [0/255, 147/255, 114/255, 1]
                    RoundedRectangle:
                        pos: self.pos
                        size: self.size
                        radius: [13]

            Button:
                text: "Sign Up"
                font_size: "15sp"
                halign: 'left'
                size_hint: .4, .11
                pos_hint: {'center_x': 0.5, 'center_y': 0.2}
                background_color: 0, 0, 0, 0
                on_press: screen_manager.current = 'sign_up'
                canvas.before:
                    Color:
                        rgba: [255/255, 147/255, 114/255, 1]
                    RoundedRectangle:
                        pos: self.pos
                        size: self.size
                        radius: [13]

    MDFloatLayout:
        id: ORANGE
        pos_hint: {'center_x': 0.5, 'center_y': 0.7}
        size_hint: 0.9, 0.15
        canvas:
            Color:
                rgba: [255/255, 147/255, 114/255, 1]
            RoundedRectangle:
                pos: self.pos
                size: self.size
                radius: [40]

        MDLabel:
            text: 'Login'
            bold: True
            font_size: 35
            background_normal: ''
            pos_hint: {'center_x': 0.85, 'center_y': 0.5}
            theme_text_color: 'Custom'
            text_color: 1, 1, 1, 1

MDScreen:
    name: 'home_screen'
    md_bg_color: 1, 1, 1, 1
    background_normal: ''

【问题讨论】:

    标签: kivy kivy-language kivymd


    【解决方案1】:

    I guess your root widget does not have an object named manager.

    on_press: root.manager.current = 'home_screen'
    

    this code refers torootwhich will return an object reference to your widget - possibly a Screen or some object that inherits Screen.

    you need the above code to return a reference to your screen manager. this is probably what your other line here does.

    on_press: screen_manager.current = 'sign_up'
    

    you can use app.something to reach a method in your app and you can use root.something to reach a method in your widget class instance.

    it will help you to retain a reference to your screen manager object in your app like this:

    def load_all_kv_files(self):
        # create and retain a reference to my Screen Manager object
        self.sm = MDScreenManager()
        self.sm.add_widget(Builder.load_file('login.kv'))
        self.sm.add_widget(Builder.load_file('home.kv'))
        return self.sm
    

    but you also need your build method to return a reference to your top level widget.

    def build(self):
        _ = self.load_all_kv_files()
        return _
    

    【讨论】:

      猜你喜欢
      • 2020-04-26
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 2022-12-15
      • 2022-12-27
      • 2022-01-10
      • 2020-06-17
      • 2014-05-18
      相关资源
      最近更新 更多