okay, THIS HAS BEEN DRIVING ME INSANE FOR LIKE THE PAST TWO DAYS. and its really annoying, so i am building an application using kivy, after getting used “easy as pie” tkinter I thought it was time to learn kivy (as it has mobile compatibility).
#I wanted to learn to reference different widgets# I searched YouTube and google for hours but to no avail I keep getting an error message
my code:
.py
# Imports import webbrowser from kivy.app import App from kivy.lang import Builder from kivy.uix.screenmanager import Screen # ToDo: Monetizing the Application def Monetize(): pass def hide(): # hidden func thats not related to code for now # configuring the Application class HyperSearch(App): main_text = "None" # initialing the Application def __init__(self, **kwargs): super(HyperSearch, self).__init__(**kwargs) self.a = Builder.load_file("kivy_file.kv") def get_search(self): engine_input = self.root.ids.raw_search # building the Application def build(self): screen = Screen() screen.add_widget(self.a) # screen.add_widget("") return screen # Running The Application in main loop if __name__ == "__main__": HyperSearch().run() # ToDo: Copyright Claim The Application and its code
.kv
<HyperSearch>: GridLayout: cols: 2 BoxLayout: orientation: "vertical" size_hint: None, 1 size: "120dp", 0 spacing: 5 Button: text: "Toggle Shopping Search" text_size: self.size halign: 'center' valign: 'middle' Button: text: "Toggle Music Search" text_size: self.size halign: 'center' valign: 'middle' Button: text: "View History" text_size: self.size halign: 'center' valign: 'middle' BoxLayout: orientation: "vertical" valign: "top" TextInput: valign: "center" orientation: "vertical" font_size: 30 size_hint: 1, None size: 0, "50dp" pos_hint: {"top":1} hint_text: "Search-Engine" icon_right: "android" helper_text: "Devices with under 1gb RAM may crash" helper_text_mode: "on_focus" multiline: False id: raw_search GridLayout: cols: 2 size_hint: 1, None size: 0, "100dp" spacing: 10 Button: text: "Search!!" text_size: self.size halign: 'center' valign: 'middle' size_hint: .5, 1 on_press: app.get_search() Button: text: "Small Search :(" text_size: self.size halign: 'center' valign: 'middle' size_hint: .5, 1 GridLayout: cols: 2 size_hint: 1, None size: 0, "100dp" Label: text: "log:" Label: text: app.main_text
and
my error
[INFO ] [Base ] Start application main loop [INFO ] [Base ] Leaving application in progress... Traceback (most recent call last): File "kivyproperties.pyx", line 861, in kivy.properties.ObservableDict.__getattr__ KeyError: 'raw_search' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:documentskivy-trialmain.py", line 63, in <module> HyperSearch().run() File "C:UsersmayanAppDataLocalProgramsPythonPython39libsite-packageskivyapp.py", line 950, in run runTouchApp() File "C:UsersmayanAppDataLocalProgramsPythonPython39libsite-packageskivybase.py", line 582, in runTouchApp EventLoop.mainloop() File "C:UsersmayanAppDataLocalProgramsPythonPython39libsite-packageskivybase.py", line 347, in mainloop self.idle() File "C:UsersmayanAppDataLocalProgramsPythonPython39libsite-packageskivybase.py", line 391, in idle self.dispatch_input() File "C:UsersmayanAppDataLocalProgramsPythonPython39libsite-packageskivybase.py", line 342, in dispatch_input post_dispatch_input(*pop(0)) File "C:UsersmayanAppDataLocalProgramsPythonPython39libsite-packageskivybase.py", line 248, in post_dispatch_input listener.dispatch('on_motion', etype, me) File "kivy_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch File "C:UsersmayanAppDataLocalProgramsPythonPython39libsite-packageskivycorewindow__init__.py", line 1412, in on_motion self.dispatch('on_touch_down', me) File "kivy_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch File "C:UsersmayanAppDataLocalProgramsPythonPython39libsite-packageskivycorewindow__init__.py", line 1428, in on_touch_down if w.dispatch('on_touch_down', touch): File "kivy_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch File "C:UsersmayanAppDataLocalProgramsPythonPython39libsite-packageskivyuixrelativelayout.py", line 297, in on_touch_down ret = super(RelativeLayout, self).on_touch_down(touch) File "C:UsersmayanAppDataLocalProgramsPythonPython39libsite-packageskivyuixwidget.py", line 545, in on_touch_down if child.dispatch('on_touch_down', touch): File "kivy_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch File "C:UsersmayanAppDataLocalProgramsPythonPython39libsite-packageskivyuixwidget.py", line 545, in on_touch_down if child.dispatch('on_touch_down', touch): File "kivy_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch File "C:UsersmayanAppDataLocalProgramsPythonPython39libsite-packageskivyuixwidget.py", line 545, in on_touch_down if child.dispatch('on_touch_down', touch): File "kivy_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch File "C:UsersmayanAppDataLocalProgramsPythonPython39libsite-packageskivyuixwidget.py", line 545, in on_touch_down if child.dispatch('on_touch_down', touch): File "kivy_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch File "C:UsersmayanAppDataLocalProgramsPythonPython39libsite-packageskivyuixbehaviorsbutton.py", line 151, in on_touch_down self.dispatch('on_press') File "kivy_event.pyx", line 705, in kivy._event.EventDispatcher.dispatch File "kivy_event.pyx", line 1248, in kivy._event.EventObservers.dispatch File "kivy_event.pyx", line 1132, in kivy._event.EventObservers._dispatch File "C:UsersmayanAppDataLocalProgramsPythonPython39libsite-packageskivylangbuilder.py", line 57, in custom_callback exec(__kvlang__.co_value, idmap) File "C:Documentskivy-trialkivy_file.kv", line 58, in <module> on_press: app.get_search() File "C:documentskivy-trialmain.py", line 51, in get_search engine_input = self.root.ids.raw_search File "kivyproperties.pyx", line 864, in kivy.properties.ObservableDict.__getattr__ AttributeError: 'super' object has no attribute '__getattr__' Process finished with exit code 1
P.S: THE ERROR ONLY SHOWS AFTER I HAVE PRESSED THE BUTTON
please help, thanks in advance, to who ever took the time to read my post
Advertisement
Answer
In order to use ids
, you must define the id
within your kv
. So, to use the raw_search
id
you must define it in your kv
like this:
TextInput: id: raw_search
Then in your code:
def get_search(self): engine_input = self.a.ids.raw_search.text print(engine_input)
Note that the ids
dictionary is built within the widget that is the root of the rule containing the id
. In your case, the ids
are in the GridLayout
. So, to access that GridLayout
widget, you must use self.a
. Also, I assumed that the <HyperSearch>:
line in your kv
was a typo and removed it.