ReferenceError:弱引用对象不再存在 Kivy DropDown [英] ReferenceError: weakly-referenced object no longer exists Kivy DropDown

查看:23
本文介绍了ReferenceError:弱引用对象不再存在 Kivy DropDown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行这个 DropDown 示例是有效的.但是,经过一段时间的使用/时间后,我收到错误 ReferenceError:弱引用对象不再存在

Running this example of DropDown works. However, after some use/time I get the error ReferenceError: weakly-referenced object no longer exists

这可能是对 on_release:dropdown.open(self) 中的一个问题的处理

This is likely do to an issue in on_release:dropdown.open(self)

关于为什么 on_parent: self.dismiss() 也不适用于我设置这些小部件的方式的奖励点.如果没有这个,我会在应用程序第一次运行时出现子菜单项,启用此功能后,子菜单项会闪烁(出现并快速消失).

Bonus points as to why on_parent: self.dismiss() also doesn't work with the way I have these widgets set up. Without this, I have the submenu items appearing when the app first runs and with this enabled, the submenu items flash (appear and quickly disappear).

#!/usr/bin/kivy
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.properties import ObjectProperty
from kivy.uix.dropdown import DropDown
from kivy.core.window import Window

Window.size = (400, 240)

sm = """

ScreenManager:
    id:manager
    canvas.before:
        Color:
            rgba: 0.5, 0.5, 0.5, 0.5
        Rectangle:
            pos: 0,0
            size: 800, 480
    Notes:
        id:Notes
        name: 'Notes'
        manager: manager

<Notes>:
    name: "Notes"
    orientation: "vertical"

    FloatLayout:
        size_hint: None, None

        canvas.before:
            Color:
                rgba: 1, 1, 0, 1    

        Button:

            id: mainbutton
            text: "Menu name"
            font_size: 20
            size_hint: None, None
            size: 150, 50
            pos: 20,400
            on_release:dropdown.open(self)

        CustomDropDown:
            id: dropdown
            #on_parent: self.dismiss()
            on_select: mainbutton.text = '{}'.format(args[1])

            Button:
                id: button1
                text: 'First Item'
                size_hint_y: None
                height: 40
                font_size: 18
                on_release: dropdown.select('First Item')

            Button:
                id: button2
                text: 'Second Item'
                size_hint_y: None
                height: 40
                font_size: 18
                on_release: dropdown.select('Second Item')

            Button:
                id: button3
                text: 'Third Item'
                size_hint_y: None
                height: 40
                font_size: 18
                on_release: dropdown.select('Third Item')

"""


class Notes(Screen):
    pass

class CustomDropDown(DropDown):
    pass

#dropdown = CustomDropDown()


class TestApp(App):

    def build(self):

        return Builder.load_string(sm)

if __name__ == '__main__':
    TestApp().run()

推荐答案

在子小部件中,CustomDropDown

使用 on_parent:self.dismiss()

当您单击主按钮时,菜单名称有时会出现错误,ReferenceError:弱引用对象不再存在.如果没有ReferenceError,下拉列表会闪烁(出现并快速消失).原因是 DropDown 被关闭了.

In child widget, CustomDropDown

With on_parent: self.dismiss()

When you clicked on the main button, Menu name sometimes it gives an error, ReferenceError: weakly-referenced object no longer exists. If there is no ReferenceError, the drop-down list flash (appear and quickly disappear). The reason is that the DropDown was dismissed.

它将在应用程序启动时显示 CustomDropDown 列表.当主按钮菜单名称被点击时,启动时出现的下拉列表消失了,但它显示了两倍长的下拉列表,即子菜单项重复了两次.

It will display the CustomDropDown list at app startup. When the main button, Menu name is clicked, the drop-down list that appeared at startup disappeared but it displayed a drop-down list twice as long i.e. submenu items repeated twice.

下拉列表类似于弹出.它们是特殊的小部件.不要尝试将其作为子项添加到任何其他小部件.如果这样做,它们将像普通小部件一样处理,不会隐藏在后台创建.

Drop-Down List is similar to Popup. They are special widget. Don't try to add it as a child to any other widget. If you do, they will be handled like an ordinary widget and won't be created hidden in the background.

请参考下面的示例来说明如何创建下拉列表.

Please refer to the example below illustrating how to create Drop-Down list.

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.dropdown import DropDown
from kivy.core.window import Window


Window.size = (800, 480)


class CustomDropDown(DropDown):
    pass


class Notes(Screen):
    pass


class MyScreenManager(ScreenManager):
    pass


class TestApp(App):
    title = "Kivy Drop-Down List Demo"

    def build(self):
        return MyScreenManager()


if __name__ == '__main__':
    TestApp().run()

test.kv

#:kivy 1.10.0
#:import Factory kivy.factory.Factory

<CustomDropDown>:
    on_select: app.root.ids.Notes.ids.mainbutton.text = '{}'.format(args[1])

    Button:
        id: button1
        text: 'First Item'
        size_hint_y: None
        height: 40
        font_size: 18
        on_release: root.select(self.text)

    Button:
        id: button2
        text: 'Second Item'
        size_hint_y: None
        height: 40
        font_size: 18
        on_release: root.select(self.text)

    Button:
        id: button3
        text: 'Third Item'
        size_hint_y: None
        height: 40
        font_size: 18
        on_release: root.select(self.text)

<MyScreenManager>:
    canvas.before:
        Color:
            rgba: 0.5, 0.5, 0.5, 0.5
        Rectangle:
            pos: 0,0
            size: 800, 480
    Notes:
        id:Notes
        name: 'Notes'

<Notes>:
    orientation: "vertical"

    FloatLayout:
        size_hint: None, None

        canvas.before:
            Color:
                rgba: 1, 1, 0, 1

        Button:
            id: mainbutton
            text: "Menu name"
            font_size: 20
            size_hint: None, None
            size: 150, 50
            pos: 20,400
            on_release: Factory.CustomDropDown().open(self)

输出

这篇关于ReferenceError:弱引用对象不再存在 Kivy DropDown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆