farshad
farshad

Reputation: 33

change screen in kivy or kivymd with press button

I want to change the screen by pressing the button and checking that the text box is not empty. here is my kv file:

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}

and here is my python code:

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()

And the error i have:

error:

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

please help me!

Please do not read the rest of the article I do not know what to say anymore because it gets an error when posting! '''It looks like your post is mostly code; please add some more details.'''

Upvotes: 3

Views: 2782

Answers (1)

John Anderson
John Anderson

Reputation: 38822

Your code:

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

is trying to use an id that does not exist. You cannot make an ids entry for the root object of a rule (the ids only includes lower level widgets). But, since the ScreenManager is the root widget, you don't need to access it using ids anyway. Just replace the above code with:

self.root.current = 'screen2'

Upvotes: 3

Related Questions