'KivyMD update: MDDropdownMenu.open() generates an error
After updating an old project after a couple of years, I got an error:
TypeError: MDDropdownMenu.open() takes 1 positional argument but 2 were given
The relevant code (it used to work, two years ago), main.py:
def build(self):
...
return Builder.load_file("main.kv")
def on_start(self):
...
self.menu_lang_append()
...
...
def menu_lang_append(self):
self.menu_lang = MDDropdownMenu(width_mult=2)
for lng in co_lang.LANG:
self.menu_lang.items.append(
{
"viewclass": "MDMenuItem",
"text": lng,
"callback": self.menu_lang_callback,
}
)
main.kv:
MDBottomAppBar:
MDToolbar:
id: ps_toolbar
title: T["co-toolbar-title"]
icon: ACTION_ICON
type: "bottom"
animate_action_button: False
next_icon: app.pulse_icon_counter() # Not a callback.
on_action_button: app.discovery_request()
right_action_items: [ ['minus-box-outline', lambda x: app.discovery_clean()], ['earth-box', lambda x: app.menu_lang.open(x)], ['dots-vertical', lambda x: app.menu_main.open(x)], ]
A very similar case here, unresolved.
I tried to add the caller like this, no luck and no wonder; runtime errors
main.py:
self.menu_lang = MDDropdownMenu(width_mult=2, caller=self.root.ids.ps_toolbar)
or
self.menu_lang = MDDropdownMenu(width_mult=2, caller=self.root.ids.ps_toolbar.right_action_items)
main.kv:
right_action_items: [... , ['earth-box', lambda x: app.menu_lang.open()], ...]
Software:
[tool.poetry.dependencies]
python = "^3.10"
Kivy = "^2.1.0"
kivymd = "^0.104.2"
Solution 1:[1]
According to #1203, copy/paste/run example:
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.clock import Clock
from kivy.metrics import dp
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu
KV = """
BoxLayout:
MDBottomAppBar:
MDToolbar:
id: toolbar
title: "co-toolbar-title"
icon: "eye"
type: "bottom"
animate_action_button: False
right_action_items: [['earth-box', lambda x: app.menu_lang_open(x)]]
"""
class Contero(MDApp):
menu_lang = ObjectProperty()
def menu_lang_open(self, button):
self.menu_lang.caller = button
self.menu_lang.open()
def menu_item_lang_callback(self, lng):
self.menu_lang.dismiss()
# Handle lng.
def menu_lang_append(self):
items = []
for lng in ["EN", "RU"]:
items.append(
{
"viewclass": "OneLineListItem",
"text": lng,
"height": dp(48),
"on_release": lambda x=lng: self.menu_item_lang_callback(x),
}
)
self.menu_lang = MDDropdownMenu(
width_mult=2,
items=items,
caller=self.root.ids.toolbar.ids.right_actions.children[0],
)
def build(self):
return Builder.load_string(KV)
def on_start(self):
def on_start(interval):
self.menu_lang_append()
Clock.schedule_once(on_start)
Contero().run()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alexey Orlov |
