Skip to main content
 首页 » 编程设计

python之向 DropDown 添加可变数量的控件之弱引用对象不再存在

2025年04月02日8JustinYoung

我有一个下拉列表,其中包含月份列表。选择月份后,我试图用正确的天数在第二个下拉列表中动态填充按钮。当我这样做时,我得到:

ReferenceError: weakly-referenced object no longer exists 

这是我的文件供引用:

主要.py:

from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.button import Button 
 
globIDs = {} 
 
class appScreen(BoxLayout): 
 
    def dayDropPop(self, num): 
        globIDs['dayDropDown'].populate(num) 
 
 
class ExtDropDown(BoxLayout): 
    heldValue = '' 
 
    def setID(self, key): 
        globIDs[key] = self 
 
    def changeValue(self, sentText, parent): 
        self.heldValue = sentText 
        parent.text = sentText 
 
class PriorityDropDown(ExtDropDown): 
    pass 
 
class MonthDropDown(ExtDropDown): 
 
    def __init__(self, **kwargs): 
        super(MonthDropDown, self).__init__(**kwargs) 
        self.setID('monthDropDown') 
 
    def monthSelect(self, month): 
        monthDay = {'Jan': 31, 'Feb': 29, 'Mar': 31, 'Apr': 30, 'May': 31, 'Jun': 30, 'Jul': 31, 'Aug': 31, 'Sep': 30, 
                    'Oct': 31, 'Nov': 30, 'Dec': 31} 
 
        numOfDays = monthDay[month] 
        appScreen().dayDropPop(numOfDays) 
 
    def testingFurther(self): 
        print() 
 
class DayDropDown(ExtDropDown): 
 
    def __init__(self, **kwargs): 
        super(DayDropDown, self).__init__(**kwargs) 
        self.setID('dayDropDown') 
 
    def populate(self, num): 
 
        for i in range(0, num): 
            newButt = Button(text=str(num + 1)) 
            self.ids.drop.add_widget(newButt) 
 
class schedulerApp(App): 
 
    def build(self): 
        return appScreen() 
 
if __name__ == '__main__': 
    schedulerApp().run() 

调度器.kv:

<PriorityDropDown>: 
    Button: 
        id: ddRoot 
        text: 'Priority' 
        on_release: drop.open(ddRoot) 
        size_hint_y: None 
        height: root.height 
 
    DropDown: 
        id: drop 
        on_parent: self.dismiss() 
        on_select: root.changeValue(args[1], ddRoot) 
 
        Button: 
            text: 'Top' 
            size_hint_y: None 
            height: root.height 
            on_release: drop.select(self.text) 
        Button: 
            text: 'High' 
            size_hint_y: None 
            height: root.height 
            on_release: drop.select(self.text) 
        Button: 
            text: 'Medium' 
            size_hint_y: None 
            height: root.height 
            on_release: drop.select(self.text) 
        Button: 
            text: 'Low' 
            size_hint_y: None 
            height: root.height 
            on_release: drop.select(self.text) 
 
<MonthDropDown>: 
    Button: 
        id: ddRoot 
        text: 'Month' 
        on_release: drop.open(ddRoot) 
        size_hint_y: None 
        height: root.height 
 
    DropDown: 
        id: drop 
        on_parent: self.dismiss() 
        on_select: root.monthSelect(args[1]) 
 
        Button: 
            text: 'Jan' 
            size_hint_y: None 
            height: root.height 
            on_release: drop.select(self.text) 
        Button: 
            text: 'Feb' 
            size_hint_y: None 
            height: root.height 
            on_release: drop.select(self.text) 
        Button: 
            text: 'Mar' 
            size_hint_y: None 
            height: root.height 
            on_release: drop.select(self.text) 
        Button: 
            text: 'Apr' 
            size_hint_y: None 
            height: root.height 
            on_release: drop.select(self.text) 
        Button: 
            text: 'May' 
            size_hint_y: None 
            height: root.height 
            on_release: drop.select(self.text) 
        Button: 
            text: 'Jun' 
            size_hint_y: None 
            height: root.height 
            on_release: drop.select(self.text) 
        Button: 
            text: 'Jul' 
            size_hint_y: None 
            height: root.height 
            on_release: drop.select(self.text) 
        Button: 
            text: 'Aug' 
            size_hint_y: None 
            height: root.height 
            on_release: drop.select(self.text) 
        Button: 
            text: 'Sep' 
            size_hint_y: None 
            height: root.height 
            on_release: drop.select(self.text) 
        Button: 
            text: 'Oct' 
            size_hint_y: None 
            height: root.height 
            on_release: drop.select(self.text) 
        Button: 
            text: 'Nov' 
            size_hint_y: None 
            height: root.height 
            on_release: drop.select(self.text) 
        Button: 
            text: 'Dec' 
            size_hint_y: None 
            height: root.height 
            on_release: drop.select(self.text) 
 
<DayDropDown>: 
    height: root.height 
    Button: 
        id: ddRoot 
        text: 'Day' 
        on_release: drop.open(ddRoot) 
        size_hint_y: None 
        height: root.height 
 
    DropDown: 
        id: drop 
        on_parent: self.dismiss() 
        on_select: root.changeValue(args[1], ddRoot) 
 
<appScreen>: 
    orientation: 'vertical' 
    Label: 
        size_hint_y: .1 
        text: 'Hello World' 
    GridLayout: 
        size_hint_y:.1 
        width: root.width 
        cols: 3 
        Button: 
        Button: 
        Button: 
    ScrollView: 
        canvas.before: 
            Color: 
                rgba: .3, .3, .3, 5 
            Rectangle: 
                pos: self.pos 
                size: self.size 
        GridLayout: 
            cols: 3 
            Label: 
                id: textReceiver 
                text: 'Words' 
                text_size: self.size 
                halign: 'left' 
                valign: 'top' 
            Label: 
            Label: 
    BoxLayout: 
        size_hint_y: .125 
        TextInput: 
            size_hint_x: .7 
        PriorityDropDown: 
            size_hint_x: .3 
    BoxLayout: 
        size_hint_y: .125 
        MonthDropDown: 
            size_hint_x: .35 
        DayDropDown: 
            id: 'dayDrop' 
            size_hint_y: 1 
            size_hint_x: .2 
        TextInput: 
            size_hint_x: .45 

我认为问题源于所讨论的控件是在 Kivy 代码中创建的,而不是在 Python 中。我所做的测试让我相信我错误地引用了我的 DayDropDown 小部件。但是,我不知道我还能怎么做。考虑到这一点,我将如何使用我已有的来引用我的 DayDropDown?如果这不是我的问题,还有什么可能导致抛出 ReferenceError?

编辑:

我的代码有点乱。我用方法“getID”创建了一个新类“globAddable”——一个简单的返回 self ——然后把 setID 放在那里。然后我设置我的 setID 现在将 self.getID() 分配给一个变量,然后将该变量用作要添加到 globObjects(以前称为 globIDs)字典的对象。

我还为我的 DropDown 对象创建了一个名为 ExtDropArray 的新类,它位于我的 DayDropDown 中。我将 populate() 方法移到了这个新类中,这样它就可以被 Dropdown 直接调用,而不是它的父 BoxLayout。我使 ExtDropArray 和 ExtDropDown 继承自 globAddable 以公开 setID(和隐式 getID)方法。

所有这一切的最终结果是完全一样的。单击 DayDropDown 上的按钮时,我仍然看不到我的一天 DropDown,并且在 MonthDropDown 上使用不同的值进行测试后,我再次收到“ReferenceError:弱引用对象不再存在”错误。但是,我注意到有问题的行实际上是打开下拉列表的方法(drop.open(ddRoot),在我的 .kv 文件的第 114 行调用)。这仍然没有给我足够的信息来知道是过程的哪一部分导致了错误,无论是将按钮添加到 DropDown 还是只是调用 open 方法。鉴于这些新信息,是否有人能够推断出需要更改的内容?

请您参考如下方法:

好吧,我终于弄明白了。

我的 ReferenceError 不是我方法的结果,而是我对 DropDown 对象实现的根本误解。修复很简单

<DayDropDown>: 
    drop: drop.__self__ 
    ... 

这花了我很长时间才找到,但以 this 的形式出现文档。鉴于没有其他提及这些 ReferenceErrors 的帖子提及此文档,我将它留在这里供其他人使用,以防他们遇到类似问题。