如何通过按钮关闭 Kivy 弹出窗口?
How to dismiss the Kivy pop-up via a Button?
我有一个使用 Kivy 创建的弹出窗口,其中包含 2 个按钮。用户可以通过在弹出区域外按下 (auto_dismiss = True) 或单击 "No" 按钮来关闭弹出窗口。
选择 "Yes" 按钮,将退出整个应用程序。
请看相关代码:
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()
现在的问题在于按下 "No" 按钮。按下时,代码退出并出现此错误:
on_press = self.popup_exit.dismiss,
AttributeError: 'Button' object has no attribute 'popup_exit'
知道如何尽可能轻松地解决这个问题吗?
试试这个:用 on_press = self.popup.dismiss()
代替 on_press = self.popup_exit.dismiss
将on_press = self.popup_exit.dismiss
改为on_press = lambda: self.popup_exit.dismiss()
,因为dismiss是一个函数,需要在按下按钮时调用。
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.popup_exit = Popup(title = "Exit",
content = self.box_popup,
size_hint = (0.4, 0.4),
auto_dismiss = True)
self.box_popup.add_widget(Button(
text = "No",
on_press = lambda: self.popup_exit.dismiss() ,
size_hint=(0.215, 0.075)))
self.popup_exit.open()
你可以通过惰性函数解决这个问题
on_press = lambda *args: self.popup_exit.dismiss()
这样,当按下按钮并且 popup_exit 已经到位时,仅 会进行查找。 .
这样的东西在 kivy
中行得通吗?
on_press : root.dismiss()
#我发现通过按钮关闭 Kivy pop-up 的最佳方法是定义全局
#Kivy v2.0.0
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
class GetPopUpFunctions(FloatLayout):
def yes_click(self):
print("banana")
def no_click(self):
print("closing the popup")
self.close_popup()
def close_popup(self):
admin_popup.dismiss()
class AdminScreen(Screen):
"""
Description:
Owner:Yaki Hakimi
"""
def open_popup(self):
popup_functions = GetPopUpFunctions() # Create a new instance of the pop class
global admin_popup
admin_popup = Popup(title="Magnets", content=popup_functions, size_hint=(None,None),size=(400,400))
admin_popup.open()
GUI = Builder.load_string("""
GridLayout:
cols: 1
ScreenManager:
id: screen_manager
AdminScreen:#name of the class
name: "admin_screen"
id: admin_screen
<AdminScreen>:
GridLayout:
cols:1
size: root.width, root.height
Button:
text : "popup debug"
on_press :
root.open_popup()
##############################################################
<GetPopUpFunctions>:
Label:
text:"popup message"
size_hint: 0.6, 0.2
pos_hint: {"x":0.2, "top":1}
Button:
text: "Yes"
size_hint: 0.2, 0.2
pos_hint: {"x":0.45, "y":0.4}
on_release:
root.yes_click()
Button:
text: "No"
size_hint: 0.2, 0.2
pos_hint: {"x":0.45, "y":0.1}
on_release:
root.no_click()
""")
class PopupOpenClose(App):
def build(self):
return GUI
if __name__ == '__main__':
PopupOpenClose().run()
我有一个使用 Kivy 创建的弹出窗口,其中包含 2 个按钮。用户可以通过在弹出区域外按下 (auto_dismiss = True) 或单击 "No" 按钮来关闭弹出窗口。 选择 "Yes" 按钮,将退出整个应用程序。
请看相关代码:
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()
现在的问题在于按下 "No" 按钮。按下时,代码退出并出现此错误:
on_press = self.popup_exit.dismiss,
AttributeError: 'Button' object has no attribute 'popup_exit'
知道如何尽可能轻松地解决这个问题吗?
试试这个:用 on_press = self.popup.dismiss()
代替 on_press = self.popup_exit.dismiss
将on_press = self.popup_exit.dismiss
改为on_press = lambda: self.popup_exit.dismiss()
,因为dismiss是一个函数,需要在按下按钮时调用。
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.popup_exit = Popup(title = "Exit",
content = self.box_popup,
size_hint = (0.4, 0.4),
auto_dismiss = True)
self.box_popup.add_widget(Button(
text = "No",
on_press = lambda: self.popup_exit.dismiss() ,
size_hint=(0.215, 0.075)))
self.popup_exit.open()
你可以通过惰性函数解决这个问题
on_press = lambda *args: self.popup_exit.dismiss()
这样,当按下按钮并且 popup_exit 已经到位时,仅 会进行查找。 .
这样的东西在 kivy
中行得通吗?
on_press : root.dismiss()
#我发现通过按钮关闭 Kivy pop-up 的最佳方法是定义全局
#Kivy v2.0.0
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
class GetPopUpFunctions(FloatLayout):
def yes_click(self):
print("banana")
def no_click(self):
print("closing the popup")
self.close_popup()
def close_popup(self):
admin_popup.dismiss()
class AdminScreen(Screen):
"""
Description:
Owner:Yaki Hakimi
"""
def open_popup(self):
popup_functions = GetPopUpFunctions() # Create a new instance of the pop class
global admin_popup
admin_popup = Popup(title="Magnets", content=popup_functions, size_hint=(None,None),size=(400,400))
admin_popup.open()
GUI = Builder.load_string("""
GridLayout:
cols: 1
ScreenManager:
id: screen_manager
AdminScreen:#name of the class
name: "admin_screen"
id: admin_screen
<AdminScreen>:
GridLayout:
cols:1
size: root.width, root.height
Button:
text : "popup debug"
on_press :
root.open_popup()
##############################################################
<GetPopUpFunctions>:
Label:
text:"popup message"
size_hint: 0.6, 0.2
pos_hint: {"x":0.2, "top":1}
Button:
text: "Yes"
size_hint: 0.2, 0.2
pos_hint: {"x":0.45, "y":0.4}
on_release:
root.yes_click()
Button:
text: "No"
size_hint: 0.2, 0.2
pos_hint: {"x":0.45, "y":0.1}
on_release:
root.no_click()
""")
class PopupOpenClose(App):
def build(self):
return GUI
if __name__ == '__main__':
PopupOpenClose().run()