如何通过按钮消除Kivy弹出窗口? [英] How to dismiss the Kivy pop-up via a Button?
问题描述
我用Kivy创建了一个弹出窗口,其中包含2个按钮.用户可以通过在弹出区域之外按下弹出窗口(auto_dismiss = True)或单击否"来关闭该弹出窗口. 选择是"按钮,将退出整个应用程序.
I have a pop-up created with Kivy, which contains 2 buttons. User can dismiss the pop-up by pressing outside of the pop-up area (auto_dismiss = True), or by clicking the "No" button. Selecting the "Yes" button, will exit the whole application.
请参阅相关代码:
class ExitApp(App):
def exit_confirmation(self):
# popup can only have one Widget. This can be fixed by adding a BoxLayout
self.box_popup = BoxLayout(orientation = 'horizontal')
self.box_popup.add_widget(Label(text = "Really exit?"))
self.box_popup.add_widget(Button(
text = "Yes",
on_press = ExitApp.exit,
size_hint = (0.215, 0.075)))
self.box_popup.add_widget(Button(
text = "No",
on_press = self.popup_exit.dismiss,
size_hint=(0.215, 0.075)))
self.popup_exit = Popup(title = "Exit",
content = self.box_popup,
size_hint = (0.4, 0.4),
auto_dismiss = True)
self.popup_exit.open()
def exit(self):
App.get_running_app().stop()
现在问题出在按下否"按钮.当按下该键时,代码退出,并显示以下错误:
The problem now lays with pressing the "No" button. When that is pressed, the code exits with this error:
on_press = self.popup_exit.dismiss,
AttributeError:按钮"对象没有属性"popup_exit"
AttributeError: 'Button' object has no attribute 'popup_exit'
有什么主意,我如何才能尽可能轻松地解决此问题?
Any idea how I can fix this as easily as possible?
推荐答案
您可以通过惰性函数来解决此问题
You can solve this issue by a lazy function
on_press = lambda *args: self.popup_exit.dismiss()
这样,仅当按下按钮并且 popup_exit 已经就位时,查找才会仅进行.
This way, the lookup will occur only when the button is pressed and popup_exit is already in place...
这篇关于如何通过按钮消除Kivy弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!