Python kivy (kivyMD) 如何获取 MDDropdownMenu 的值
Python kivy (kivyMD) how to fetch values of MDDropdownMenu
我正在尝试在我的 kv 文件中获取 MDDropwdownMenu
的选择我正在通过以下代码使用 MDRaisedButton
MDRaisedButton:
id: select_warning_image_Button
text: "Menu labels"
opposite_colors: True
elevation_normal: 0
on_release: MDDropdownMenu(items=app.menu_labels, width_mult=4).open(self)
在我的 main.py
中(见下文)我有一个名为 MainApp
的 class,它继承自 App
。在此 class 中,我创建了属性变量 menu_labels
。我想使用函数 change_variable
将变量 VARIABLE
的值设置为菜单的值。但我似乎无法使用 self.change_variable(…)。选择下拉列表中的特定值时,是否有一个值?
# main.py
import …
class MainApp(App):
VARIABLE = ""
menu_labels = [
{"viewclass": "MDMenuItem",
"text": "Label1",
"on_release": self.change_variable("Label1")},
{"viewclass": "MDMenuItem",
"text": "Label2",
"on_release": self.change_variable("Label2")},
]
def change_variable(self, label):
self.VARIABLE = label
解决方案
在输出中,选择了 Label2
。有关详细信息,请参阅代码段、示例和输出。
kv 文件
- 为
<MDMenuItem>:
添加 class 规则
- 添加
on_release
事件以调用 App change_variable()
方法 class 并将 self.text
传递给它。
片段 - kv 文件
<MDMenuItem>:
on_release: app.change_variable(self.text)
例子
main.py
from kivy.app import App
from kivymd.theming import ThemeManager
class MainApp(App):
title = "KivyMD MDDropdownMenu Demo"
theme_cls = ThemeManager()
VARIABLE = ""
menu_labels = [
{"viewclass": "MDMenuItem",
"text": "Label1"},
{"viewclass": "MDMenuItem",
"text": "Label2"},
]
def change_variable(self, value):
print("\nvalue=", value)
self.VARIABLE = value
print("\tself.VARIABLE=", self.VARIABLE)
if __name__ == "__main__":
MainApp().run()
main.kv
#:kivy 1.11.0
#:import MDDropdownMenu kivymd.menu.MDDropdownMenu
#:import MDRaisedButton kivymd.button.MDRaisedButton
<MDMenuItem>:
on_release: app.change_variable(self.text)
Screen:
name: 'menu'
MDRaisedButton:
id: select_warning_image_Button
size_hint: None, None
size: 3 * dp(48), dp(48)
text: 'Menu labels'
opposite_colors: True
elevation_normal: 0
pos_hint: {'center_x': 0.1, 'center_y': 0.9}
on_release: MDDropdownMenu(items=app.menu_labels, width_mult=4).open(self)
输出
我正在尝试在我的 kv 文件中获取 MDDropwdownMenu
的选择我正在通过以下代码使用 MDRaisedButton
MDRaisedButton:
id: select_warning_image_Button
text: "Menu labels"
opposite_colors: True
elevation_normal: 0
on_release: MDDropdownMenu(items=app.menu_labels, width_mult=4).open(self)
在我的 main.py
中(见下文)我有一个名为 MainApp
的 class,它继承自 App
。在此 class 中,我创建了属性变量 menu_labels
。我想使用函数 change_variable
将变量 VARIABLE
的值设置为菜单的值。但我似乎无法使用 self.change_variable(…)。选择下拉列表中的特定值时,是否有一个值?
# main.py
import …
class MainApp(App):
VARIABLE = ""
menu_labels = [
{"viewclass": "MDMenuItem",
"text": "Label1",
"on_release": self.change_variable("Label1")},
{"viewclass": "MDMenuItem",
"text": "Label2",
"on_release": self.change_variable("Label2")},
]
def change_variable(self, label):
self.VARIABLE = label
解决方案
在输出中,选择了 Label2
。有关详细信息,请参阅代码段、示例和输出。
kv 文件
- 为
<MDMenuItem>:
添加 class 规则
- 添加
on_release
事件以调用 Appchange_variable()
方法 class 并将self.text
传递给它。
片段 - kv 文件
<MDMenuItem>:
on_release: app.change_variable(self.text)
例子
main.py
from kivy.app import App
from kivymd.theming import ThemeManager
class MainApp(App):
title = "KivyMD MDDropdownMenu Demo"
theme_cls = ThemeManager()
VARIABLE = ""
menu_labels = [
{"viewclass": "MDMenuItem",
"text": "Label1"},
{"viewclass": "MDMenuItem",
"text": "Label2"},
]
def change_variable(self, value):
print("\nvalue=", value)
self.VARIABLE = value
print("\tself.VARIABLE=", self.VARIABLE)
if __name__ == "__main__":
MainApp().run()
main.kv
#:kivy 1.11.0
#:import MDDropdownMenu kivymd.menu.MDDropdownMenu
#:import MDRaisedButton kivymd.button.MDRaisedButton
<MDMenuItem>:
on_release: app.change_variable(self.text)
Screen:
name: 'menu'
MDRaisedButton:
id: select_warning_image_Button
size_hint: None, None
size: 3 * dp(48), dp(48)
text: 'Menu labels'
opposite_colors: True
elevation_normal: 0
pos_hint: {'center_x': 0.1, 'center_y': 0.9}
on_release: MDDropdownMenu(items=app.menu_labels, width_mult=4).open(self)