問題描述
我有一個使用 Kivy 創建的彈出窗口,其中包含 2 個按鈕.用戶可以通過在彈出區域外按下 (auto_dismiss = True) 或單擊否"按鈕來關閉彈出窗口.選擇是"按鈕,將退出整個應用程序.
請看相關代碼:
類 ExitApp(App):def exit_confirmation(self):# popup 只能有一個 Widget.這可以通過添加 BoxLayout 來解決self.box_popup = BoxLayout(orientation = 'horizo??ntal')self.box_popup.add_widget(Label(text = "真的退出?"))self.box_popup.add_widget(按鈕(文字=是",on_press = ExitApp.exit,size_hint = (0.215, 0.075)))self.box_popup.add_widget(按鈕(文字=否",on_press = self.popup_exit.dismiss,size_hint=(0.215, 0.075)))self.popup_exit = Popup(title = "退出",內容 = self.box_popup,size_hint = (0.4, 0.4),auto_dismiss = 真)self.popup_exit.open()def 退出(自我):App.get_running_app().stop()
現在的問題在于按下否"按鈕.按下該按鈕時,代碼將退出并出現以下錯誤:
<塊引用> on_press = self.popup_exit.dismiss,
AttributeError: 'Button' 對象沒有屬性 'popup_exit'
知道如何盡可能輕松地解決此問題嗎?
你可以通過一個惰性函數來解決這個問題
on_press = lambda *args: self.popup_exit.dismiss()
這樣,僅在按下按鈕且 popup_exit 已就位時才會進行查找...
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.
Please see relevant code:
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: '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()
This way, the lookup will occur only when the button is pressed and popup_exit is already in place...
這篇關于如何通過按鈕關閉 Kivy 彈出窗口?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!