Python 向下拉列表添加可变数量的控件-弱引用对象不再存在
我有一个下拉列表,里面有几个月的列表。选择月份后,我将尝试在第二个下拉列表中动态填充具有正确天数的按钮。当我这样做时,我得到:Python 向下拉列表添加可变数量的控件-弱引用对象不再存在,python,kivy,Python,Kivy,我有一个下拉列表,里面有几个月的列表。选择月份后,我将尝试在第二个下拉列表中动态填充具有正确天数的按钮。当我这样做时,我得到: ReferenceError: weakly-referenced object no longer exists 以下是我的文件供参考: main.py: from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.button import Button glob
ReferenceError: weakly-referenced object no longer exists
以下是我的文件供参考:
main.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()
千伏电压:
<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
:
按钮:
id:ddRoot
文本:“优先级”
发布时:drop.open(ddRoot)
尺寸提示:无
高度:root.height
下拉列表:
id:下降
在父对象上:self.discouse()
on_选择:root.changeValue(args[1],ddRoot)
按钮:
文字:“顶部”
尺寸提示:无
高度:root.height
发布时:drop.select(self.text)
按钮:
文字:“高”
尺寸提示:无
高度:root.height
发布时:drop.select(self.text)
按钮:
文本:“中等”
尺寸提示:无
高度:root.height
发布时:drop.select(self.text)
按钮:
文字:“低”
尺寸提示:无
高度:root.height
发布时:drop.select(self.text)
:
按钮:
id:ddRoot
文字:“月份”
发布时:drop.open(ddRoot)
尺寸提示:无
高度:root.height
下拉列表:
id:下降
在父对象上:self.discouse()
on_select:root.monthSelect(args[1])
按钮:
文字:“Jan”
尺寸提示:无
高度:root.height
发布时:drop.select(self.text)
按钮:
文字:“二月”
尺寸提示:无
高度:root.height
发布时:drop.select(self.text)
按钮:
文字:“Mar”
尺寸提示:无
高度:root.height
发布时:drop.select(self.text)
按钮:
文字:“Apr”
尺寸提示:无
高度:root.height
发布时:drop.select(self.text)
按钮:
文字:“五月”
尺寸提示:无
高度:root.height
发布时:drop.select(self.text)
按钮:
文字:“Jun”
尺寸提示:无
高度:root.height
发布时:drop.select(self.text)
按钮:
文字:“七月”
尺寸提示:无
高度:root.height
发布时:drop.select(self.text)
按钮:
文字:“8月”
尺寸提示:无
高度:root.height
发布时:drop.select(self.text)
按钮:
文本:“Sep”
尺寸提示:无
高度:root.height
发布时:drop.select(self.text)
按钮:
文字:“十月”
尺寸提示:无
高度:root.height
发布时:drop.select(self.text)
按钮:
文字:“11月”
尺寸提示:无
高度:root.height
发布时:drop.select(self.text)
按钮:
文字:“十二月”
尺寸提示:无
高度:root.height
发布时:drop.select(self.text)
:
高度:root.height
按钮:
id:ddRoot
文字:“一天”
发布时:drop.open(ddRoot)
尺寸提示:无
高度:root.height
下拉列表:
id:下降
在父对象上:self.discouse()
on_选择:root.changeValue(args[1],ddRoot)
:
方向:“垂直”
标签:
大小\u提示\u y:.1
文字:“你好,世界”
网格布局:
大小\u提示\u y:.1
宽度:root.width
科尔斯:3
按钮:
按钮:
按钮:
滚动视图:
在以下情况之前:
颜色:
rgba:.3、.3、5
矩形:
pos:self.pos
大小:self.size
网格布局:
科尔斯:3
标签:
id:textReceiver
文字:“文字”
文本大小:self.size
哈利格:“左”
valign:“顶级”
标签:
标签:
盒子布局:
大小提示y:.125
文本输入:
大小\u提示\u x:.7
优先级下拉列表:
大小\u提示\u x:.3
盒子布局:
大小提示y:.125
MonthDropDown:
尺寸提示:35
日期下拉列表:
id:“dayDrop”
尺寸提示:1
大小\u提示\u x:.2
文本输入:
尺寸提示:45
我认为这个问题源于使用Kivy代码而不是Python创建的控件。我所做的测试让我相信我错误地引用了DayDropDown小部件。然而,我不知道我还能怎么做。考虑到这一点,我将如何使用我已有的内容来引用我的DayDropDown?如果这不是我的问题,还有什么可能导致抛出ReferenceError
编辑:
我的代码有点乱。我创建了一个新类“globAddable”,其中包含方法“getID”——一个简单的返回self——并将setID放在其中。然后,我设置我的setID,现在将self.getID()赋值给一个变量,然后将该变量用作要添加到globObjects(以前称为globIDs)字典的对象
我还为我的DropDown对象创建了一个名为ExtDropArray的新类,它位于我的DayDropDown中。我将populate()方法移动到这个新类中,以便下拉菜单可以直接调用它,而不是它的父BoxLayout。我让ExtDropArray和ExtDropDown从globAddable继承,以公开setID(隐式的getID)方法
所有这一切的最终结果完全相同。在DayDropDown上单击按钮时,我仍然看不到我的day下拉列表,在MonthDropDown上使用不同的值进行测试后,我再次得到“ReferenceError:Welly-r”
<DayDropDown>:
drop: drop.__self__
...
python3==3.7.5, hostpython3==3.7.5