1

So I'm trying to make a 'clear' button that is able to clear textfields. I've looked at tutorials and practically copied the code word for word, yet there's still errors. If someone could please provide some insight, that would be greatly appreciated.

python code

class MainApp(MDApp):
icon = StringProperty()
nav_drawer = ObjectProperty()

def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self.data = {}

def build(self):
    self.theme_cls.primary_palette = "Red"
    self.theme_cls.primary_hue = "A700"
    self.nav_drawer = ContentNavigationDrawer()
    return Builder.load_file('Car.kv')

def logger(self):
    print("Nothing")

def on_button_clear(self):
    self.root.ids.welcome_label = "no"
    self.root.ids.user.text = ""
    self.root.ids.email.text = ""
    self.root.ids.phone_number.text = ""
    self.root.ids.password.text = ""`

MainApp().run()

(this is just a snippet of the app building code)

Kivy code: (also just a snippet)

\`\<MainScreen\>:
name: 'main'
MDCard:
size_hint: None, None
size: 900, 1000
pos_hint: {"center_x":0.5,"center_y":0.5}
elevation: 10
padding: 25
radius: 50
spacing: 25
orientation: "vertical"

    MDTextField:
        id: user
        hint_text: "Username"
        icon_right: "account"
        size_hint_x: 0.3
        width: 200
        font_size: 18
        pos_hint: {"center_x": 0.5}
        adaptive_size: True

    MDTextField:
        id: email
        hint_text: "Email"
        icon_right: "email"
        size_hint_x: 0.3
        width: 200
        font_size: 18
        pos_hint: {"center_x": 0.5}
        adaptive_size: True

    MDTextField:
        id: phone_number
        hint_text: "Phone Number"
        icon_right: "phone"
        size_hint_x: 0.3
        width: 200
        font_size: 18
        pos_hint: {"center_x": 0.5}
        adaptive_size: True

    MDTextField:
        id: password
        hint_text: "Password"
        icon_right: "eye-off"
        size_hint_x: 0.3
        width: 200
        font_size: 18
        pos_hint: {"center_x": 0.5}
        password: True
        adaptive_size: True

    MDRectangleFlatButton:
        text: "LOG IN"
        font_size: 20
        pos_hint: {"center_x": 0.5}
        on_press: app.logger()

    MDRaisedButton:
        text: "CLEAR"
        font_size: 20
        pos_hint: {"center_x": 0.5}
        on_press: app.on_button_clear()

    Widget:
        size_hint_y: None
        height: 10

    FloatLayout:
        MDIconButton:
            icon: "exit-to-app"
            pos_hint: {'x':0.8,'y':0.085}
            on_release: app.stop()

    MDLabel
        id: welcome_label
        text: "no"
        font_size: 32
        halign: 'center'
        pos_hint: {"center_x": 0.5, "center_y": 0.20}```

I was expecting for whatever input was put into the textfields would be cleared, but instead it comes out with this error.

\`Traceback (most recent call last):
File "kivy/properties.pyx", line 961, in kivy.properties.ObservableDict.__getattr__
KeyError: 'user'

During handling of the above exception, another exception occurred:

File "kivy/properties.pyx", line 964, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'\`\`
Kovy Jacob
  • 489
  • 2
  • 16

0 Answers0