Skip to content
Advertisement

Kivy Error when I close the DatePicker (MultiScreen)

I’m trying to learn Kivy. Most stuff works when I just code it separate. But when I combine like MultiScreen with other code I always strugle.

Here I want that after closing the DatePicker to change the Text(id: date_label. Same code worked without the MultiScreen

Error: line 31, in on_cancel self.root.ids.date_label.text = “Cancel” AttributeError: ‘CreateScreen’ object has no attribute ‘root’

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.picker import MDDatePicker

Window.size = (350, 600)



class MenuScreen(Screen):
    def input_user(self):
        global username_1
        username_1 = self.userinput.text
        print(username_1)


class MainScreen(Screen):
    def user_name(self):
        print(username_1)


class CreateScreen(Screen):

    # Click Ok
    def on_save(self, instance, value, date_range):
        print(instance, value, date_range)

    
    # Click Close
    def on_cancel(self, instance, value): # HERE IS THE ERROR
        self.root.ids.date_label.text = "Cancel"

    def show_date_picker(self):
        date_dialog = MDDatePicker()
        date_dialog.bind(on_save=self.on_save, on_cancel=self.on_cancel)
        date_dialog.open()


sm = ScreenManager()
sm.add_widget(MenuScreen(name="menu"))
sm.add_widget(MainScreen(name="main-screen"))
sm.add_widget(CreateScreen(name="create-screen"))


class MyApp(MDApp):
    def build(self):

        screen = (Builder.load_file("kvfile.kv"))
        return screen


if __name__ == '__main__':
    MyApp().run()

KV File

ScreenManager:
    MenuScreen:
    MainScreen:
    CreateScreen:


<MenuScreen>:
    userinput: input
    name: "menu"
    BoxLayout:
        padding: "50dp"
        MDTextField:
            id: input
            padding: "10dp"
            hint_text: "Enter Username"
            pos_hint: {"center_x": 0.5, "center_y": 0.6}
    MDRectangleFlatButton:
        text: "Continue"
        pos_hint: {"center_x": 0.5, "center_y":0.5}
        on_release: root.manager.current = "main_screen" 
        on_press: root.input_user()


<MainScreen>:
    name: "main_screen"
    MDLabel:
        text: "Welcome"
        halign: "center"
        pos_hint: {"center_x": 0.5, "center_y": 0.9}
    MDRectangleFlatButton:
        text: "Back"
        pos_hint: {"center_x": 0.5, "center_y":0.2}
        on_press: root.manager.current = "menu"
    MDRectangleFlatButton:
        text: "Print"
        pos_hint: {"center_x": 0.5, "center_y":0.6}
        on_release: root.manager.current = "create_screen"
    MDRectangleFlatButton:
        text: "Empty"
        pos_hint: {"center_x": 0.5, "center_y":0.4}
        on_press: root.manager.current = "menu"


<CreateScreen>
    name: "create_screen"
    MDRaisedButton:
        text: "Date"
        pos_hint: {"center_x": 0.5, "center_y" : 0.5}
        on_release: root.show_date_picker()

    MDLabel:
        id: date_label
        text: "Select Date"
        pos_hint: {"center_x": 1, "center_y" : 0.4}

Thank you

Advertisement

Answer

The error is telling you that 'CreateScreen' object has no attribute 'root'. You are trying to refer to root in the line:

self.root.ids.date_label.text = "Cancel"

In that line, self is the instance of CreateScreen, so self.root is trying to access a root attribute of self (CreateScreen). If you are trying to access the ids of CreateScreen, then just use:

self.ids.date_label.text = "Cancel"
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement