我正在尝试学习 KivyMD,并决定编写一个简单的购物清单应用程序。
我想通过对话窗口将新条目添加到购物清单中。尝试运行以下代码时出现错误:
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()
我得到的错误是:
弱引用对象不再存在
我把它缩小到toolbar.right_action_items = [['plus', self.dialog_window]]
,不管它是左还是右都没关系。
当我取消注释时,问题就消失了self.test.button
。
我不明白为什么会这样。我在这里错过了一些非常基本的东西吗?
谢谢你。