kivy - 绑定弹出窗口关闭以从另一个小部件实例运行
kivy - bind popup dismiss to function from another widget instance
我想将关闭弹出窗口(或按下该弹出窗口小部件中的按钮)绑定到打开该弹出窗口的小部件的函数。
更具体地说,
#:kivy 1.10.0
#:import Factory kivy.factory.Factory
<MainBox>:
SelectButton:
id: selectbutton
text: 'Select'
on_press: Factory.SelectPopup().open()
Button:
text: 'Ask'
background_color: (0,1,0,1) if selectbutton.selected else (1,0,0,1)
<SelectPopup>:
title: 'Select from List'
auto_dismiss: False
on_dismiss: Factory.SelectButton().set_selection()
BoxLayout:
Label:
text: 'hello'
Button:
text: 'ok'
#on_press: Factory.SelectButton().set_selection()
on_press: root.dismiss()
并在 .py 文件中
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty
from kivy.uix.popup import Popup
class SelectButton(Button):
selected = BooleanProperty(False)
def set_selection(self):
self.selected = True
class SelectPopup(Popup):
pass
class MainBox(BoxLayout):
pass
class SelectButtonApp(App):
def build(self):
return MainBox()
if __name__ == "__main__":
SelectButtonApp().run()
也就是说,当我关闭按 SelectButton
打开的弹出窗口时,我想将属性 selected
从 SelectButton
设置为 True
。尝试的方法不起作用,我猜是因为 on_dismiss
调用没有引用 MainBox
中的 SelectButton
实例。我也尝试过使用 ids
,但显然无法在 MainBox
和 SelectPopup
等不相关的小部件之间轻松传递它们。任何帮助将非常感激。
使用app.root.ids
访问项目。有两种解决方法。
解决方案 1 - 直接访问,即不调用 set_selection() 函数
直接引用 selected
即 没有 set_selection()
函数。
Button:
text: 'ok'
on_press:
app.root.ids.selectbutton.selected = True
root.dismiss()
解决方案 2 - 调用 set_selection() 函数
调用 set_selection() 函数。
Button:
text: 'ok'
on_press:
app.root.ids.selectbutton.set_selection()
root.dismiss()
输出
我想将关闭弹出窗口(或按下该弹出窗口小部件中的按钮)绑定到打开该弹出窗口的小部件的函数。
更具体地说,
#:kivy 1.10.0
#:import Factory kivy.factory.Factory
<MainBox>:
SelectButton:
id: selectbutton
text: 'Select'
on_press: Factory.SelectPopup().open()
Button:
text: 'Ask'
background_color: (0,1,0,1) if selectbutton.selected else (1,0,0,1)
<SelectPopup>:
title: 'Select from List'
auto_dismiss: False
on_dismiss: Factory.SelectButton().set_selection()
BoxLayout:
Label:
text: 'hello'
Button:
text: 'ok'
#on_press: Factory.SelectButton().set_selection()
on_press: root.dismiss()
并在 .py 文件中
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty
from kivy.uix.popup import Popup
class SelectButton(Button):
selected = BooleanProperty(False)
def set_selection(self):
self.selected = True
class SelectPopup(Popup):
pass
class MainBox(BoxLayout):
pass
class SelectButtonApp(App):
def build(self):
return MainBox()
if __name__ == "__main__":
SelectButtonApp().run()
也就是说,当我关闭按 SelectButton
打开的弹出窗口时,我想将属性 selected
从 SelectButton
设置为 True
。尝试的方法不起作用,我猜是因为 on_dismiss
调用没有引用 MainBox
中的 SelectButton
实例。我也尝试过使用 ids
,但显然无法在 MainBox
和 SelectPopup
等不相关的小部件之间轻松传递它们。任何帮助将非常感激。
使用app.root.ids
访问项目。有两种解决方法。
解决方案 1 - 直接访问,即不调用 set_selection() 函数
直接引用 selected
即 没有 set_selection()
函数。
Button:
text: 'ok'
on_press:
app.root.ids.selectbutton.selected = True
root.dismiss()
解决方案 2 - 调用 set_selection() 函数
调用 set_selection() 函数。
Button:
text: 'ok'
on_press:
app.root.ids.selectbutton.set_selection()
root.dismiss()