I am stuck with this problem for quite a few days now. And also, now, I have a slight idea about this kind of error and that it has something to do with the python file trying to access an element (Dropdown menu) using its id in the kivy string.
But since I am new to OOP, I cannot figure out what exactly has to be done.
Here's the code:
*The arrangement of elements in the code is going to be changed later. So don't worry about that.
All I need is to get the dropdown menu working in this kind of code (i.e., where there are multiple screens within a screen manager, and each screen is represented by a separate class.)
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.menu import MDDropdownMenu
from kivymd.uix.datatables import MDDataTable
from kivy.metrics import dp
from kivymd.uix.button import MDFloatingActionButton
class MenuScreen(Screen):
pass
class ProScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
menu_items = [{"text": f"{i}"} for i in ["XML","Bootstrap","Mysql","None"]]
self.menu = MDDropdownMenu(
caller=self.ids.drop_item,
items=menu_items,
width_mult=4,
callback=self.set_item)
self.menu.bind(on_release=self.set_item)
def set_item(self, instance_menu_item):
self.ids.drop_item.text = instance_menu_item.text
self.menu.dismiss()
def display_selection(self):
select1 = self.ids.drop_item.text
#lx = self.root.ids.lx.text
#res = ["the dropdown menu selection is:",]
print (select1)
def on_pre_enter(self, *args):
self.app.title = "Slab"
def show_data(self, *args):
ly = self.ids.ly.text
#lx = self.root.ids.lx.text
res = ["hi we are bts" ,float(ly)]
print (res)
res2 = float(ly)+1
self.ids.res11.text = str(res)
print (res2)
class UploadScreen(Screen):
pass
class DemoApp18(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.theme_cls.primary_palette = "Blue"
#self.scr22 = Builder.load_string(temp_kv.KV)
self.screen_manager = Builder.load_string(screen_helper)
self.screen_manager = ScreenManager()
self.screen_manager.add_widget(MenuScreen(name='menupage'))
self.screen_manager.add_widget(ProScreen(name='pro'))
self.screen_manager.add_widget(UploadScreen(name='upload'))
def build(self):
return self.screen_manager
DemoApp18().run()
And the Kivy string is called "screen_helper", and is shared below:
"""
ScreenManager:
MenuScreen:
ProScreen:
UploadScreen:
<MenuScreen>:
name: 'menupage'
MDRectangleFlatButton:
text: 'Pro'
pos_hint: {'center_x':0.5,'center_y':0.6}
on_press: root.manager.current = 'pro'
MDRectangleFlatButton:
text: 'Upload'
pos_hint: {'center_x':0.5,'center_y':0.5}
on_press: root.manager.current = 'upload'
<ProScreen>:
name: 'pro'
MDLabel:
text: 'Pro'
halign: 'center'
MDTextField:
id: ly
hint_text: "Enter longer span of slab"
helper_text: "(should be in m)"
helper_text_mode: "on_focus"
icon_right: "android"
icon_right_color: app.theme_cls.primary_color
size_hint_x:None
width:250
halign: "center"
valign: "top"
MDRectangleFlatButton:
text: 'Pro'
pos_hint: {'center_x':0.2,'center_y':0.1}
on_press:
#root.manager.current = 'menupage'
#app.screen_manager.pro.show_data()
root.show_data()
MDLabel:
#text: "Enter details:"
theme_font_styles: "H1"
id: res11
#halign: "center"
MDDropDownItem:
id: drop_item
pos_hint: {'center_x': .5, 'center_y': .5}
text: 'Select OPtion'
on_release: app.menu.open()
MDRaisedButton:
id: btn1
text: "Display"
md_bg_color: app.theme_cls.primary_color
pos_hint: {"center_x": .5, "center_y": .75}
on_release:
root.display_selection()
MDRectangleFlatButton:
text: 'Back'
pos_hint: {'center_x':0.5,'center_y':0.1}
on_press: root.manager.current = 'menupage'
<UploadScreen>:
name: 'upload'
MDLabel:
text: 'Upload'
halign: 'center'
MDRectangleFlatButton:
text: 'Back'
pos_hint: {'center_x':0.5,'center_y':0.1}
on_press: root.manager.current = 'menupage'
"""
DropDownMenu is such an important part of what I am creating. I can't even just replace it with some other element.
Please suggest me any way to make this work
Oh! and the error message appears like this:
Traceback (most recent call last):
File "kivyproperties.pyx", line 860, in kivy.properties.ObservableDict.__getattr__
KeyError: 'drop_item'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:Userskmd_inpython2.py", line 212, in <module>
DemoApp18().run()
File "C:Userskmd_inpython2.py", line 196, in __init__
self.screen_manager = Builder.load_string(screen_helper)
File "C:Usersuilder.py", line 405, in load_string
rule_children=rule_children)
File "C:Usersuilder.py", line 654, in _apply_rule
child = cls(__no_builder=True)
File "C:Userskmd_inpython2.py", line 115, in __init__
caller=self.ids.drop_item,
File "kivyproperties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'