我目前正在Kivy制作一个简单的应用程序,涉及一个包含自定义小部件的Popup对象。我希望能够从主屏幕访问窗口小部件中的信息(按下按钮等),但是我没有实例化按钮的问题。
下面给出(大多数)最小(部分)工作的例子。
import kivy
kivy.require('1.9.1')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.checkbox import CheckBox
from kivy.uix.button import Button
from kivy.properties import ObjectProperty
from kivy.lang import Builder
Builder.load_string('''
<Main>
popup: popup
BoxLayout:
id: boxlayout
Button:
text: "Open Popup"
on_release: popup.open(); root.create_popup()
Popup:
id: popup
title: "Example popup"
on_parent: if self.parent == boxlayout: boxlayout.remove_widget(self)
PopupContent:
<PopupContent>:
closer: closer
# Suppose there is a more complicated nested layout structure here
BoxLayout:
orientation: 'vertical'
CheckBox:
Button:
id: closer
text: "Close popup"
''')
class PopupContent(BoxLayout):
def __init__(self,**kwargs):
super(PopupContent, self).__init__(**kwargs)
class Main(BoxLayout):
popup = ObjectProperty()
def __init__(self,**kwargs):
super(Main, self).__init__(**kwargs)
# Callback from button in PopupContent
def closer_callback(self,*args):
print("callback called!")
self.popup.dismiss()
# Creates container widget for popup and binds button to perform callback
def create_popup(self,*args):
popup_content = PopupContent()
popup_content.closer.bind(on_release = self.closer_callback)
# Problem: This returns "<type 'kivy.weakproxy.WeakProxy'>"
print(type(popup_content.closer))
class TestApp(App):
def build(self):
return Main()
if __name__ == '__main__':
TestApp().run()
当我在我的代码中注释时,我试图绑定PopupContent小部件中的按钮以在按下时在我的主屏幕中运行方法,但是按钮还没有被创建,所以没有进行这样的绑定。 / p>
如何绑定此按钮呢?
答案 0 :(得分:0)
原来我在创建示例时犯了一个错误 - 虽然我创建了一个PopupContent小部件的实例,但我从未将它添加到Popup小部件。解决这个问题,现在工作正常。
import kivy
kivy.require('1.9.1')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.checkbox import CheckBox
from kivy.uix.button import Button
from kivy.properties import ObjectProperty
from kivy.lang import Builder
Builder.load_string('''
<Main>
popup: popup
BoxLayout:
id: boxlayout
Button:
text: "Open Popup"
on_release: popup.open(); root.create_popup()
Popup:
id: popup
title: "Example popup"
on_parent: if self.parent == boxlayout: boxlayout.remove_widget(self)
<PopupContent>:
closer: closer
# Suppose there is a more complicated nested layout structure here
BoxLayout:
orientation: 'vertical'
CheckBox:
Button:
id: closer
text: "Close popup"
''')
class PopupContent(BoxLayout):
def __init__(self,**kwargs):
super(PopupContent, self).__init__(**kwargs)
class Main(BoxLayout):
popup = ObjectProperty()
def __init__(self,**kwargs):
super(Main, self).__init__(**kwargs)
# Callback from button in PopupContent
def closer_callback(self,*args):
print("callback called!")
self.popup.dismiss()
# Creates container widget for popup and binds button to perform callback
def create_popup(self,*args):
popup_content = PopupContent()
self.popup.content = popup_content
popup_content.closer.bind(on_release = self.closer_callback)
# Note: This still returns "<type 'kivy.weakproxy.WeakProxy'>"
# But the button is correctly bound regardless
print(type(popup_content.closer))
class TestApp(App):
def build(self):
return Main()
if __name__ == '__main__':
TestApp().run()
我的理解是,当首次创建PopupContent的实例时,出于性能原因,实际的窗口小部件在显示之前不会被加载(可能是通过一些加载函数)。然而,在那之前,我们仍然通过WeakProxy获得“弱引用”,这使得绑定成为可能。