I'm trying to learn KivyMD and decided to write a simple shopping list app.
I want to add new entries to the shopping list by adding them via dialog window. I am getting an error when trying to run the below code:
from kivymd.app import MDApp
from kivymd.uix.list import MDList, IconLeftWidget, OneLineIconListItem
from kivymd.icon_definitions import md_icons
from kivymd.uix.toolbar import MDToolbar
from kivymd.uix.dialog import MDDialog
from kivy.properties import ObjectProperty
from kivymd.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivymd.uix.screen import Screen
from kivymd.uix.button import MDFlatButton
from kivymd.uix.textfield import MDTextFieldRect, MDTextField
class ShoppingListApp(MDApp):
def build(self):
screen = Screen()
scroll = ScrollView()
toolbar = MDToolbar(title="Shopping List", anchor_title="center")
toolbar.right_action_items = [['plus', self.dialog_window]]
screen.add_widget(scroll)
self.kvmd_list = MDList()
#self.test_button = MDFlatButton(text="TEST BUTTON", on_press=self.dialog_window)
#screen.add_widget(self.test_button)
scroll.add_widget(self.kvmd_list)
self.theme_cls.theme_style = "Light"
self.theme_cls.primary_palette = "BlueGray"
self.theme_cls.primary_hue = '400'
self.kvmd_list.add_widget(toolbar)
return screen
def dialog_window(self, obj):
self.dialog = MDDialog(
title="Add new item:",
buttons=[
MDFlatButton(
text="Add",
),
],
)
self.dialog.set_normal_height()
self.dialog.open()
ShoppingListApp().run()
The error I'm getting is:
weakly-referenced object no longer exists
I narrowed it down to toolbar.right_action_items = [['plus', self.dialog_window]]
, tough it doesn't matter if it's left or right.
The issue disappears when I uncomment the self.test.button
.
I fail to understand why this happens. Am I missing something really basic here?
Thank you.