from kivy.config import Config
Config.set('kivy', 'exit_on_escape', '0')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.core.window import Window
class ChildApp(App):
def build(self):
Window.bind(on_request_close=self.on_request_close)
return Label(text='Child')
def on_request_close(self, *args):
self.textpopup(title='Exit', text='Are you sure?')
return True
def textpopup(self, title='', text=''):
"""Open the pop-up with the name.
:param title: title of the pop-up to open
:type title: str
:param text: main text of the pop-up to open
:type text: str
:rtype: None
"""
box = BoxLayout(orientation='vertical')
box.add_widget(Label(text=text))
mybutton = Button(text='OK', size_hint=(1, 0.25))
box.add_widget(mybutton)
popup = Popup(title=title, content=box, size_hint=(None, None), size=(600, 300))
mybutton.bind(on_release=self.stop)
popup.open()
if __name__ == '__main__':
ChildApp().run()
如果您希望您的应用程序在GUI关闭后简单地运行,那么最简单、最小的方法是将任何退出代码放在
TestApp().run()
之后。run()
创建一个无休止的循环,它还清除kivy中的任何事件数据,这样它就不会挂起。当window/gui实例一死,这个无休止的循环就会中断。因此,之后的任何代码也只能在GUI死后执行。在如果您想创建一个优雅的GUI关闭,例如使用socket关闭事件或弹出窗口询问用户是否真的希望这样做,那么为on_request_close事件创建一个钩子是正确的方法:
由pythonic64提供,他在issue的方式中创建了一个gist主题。在
相关问题 更多 >
编程相关推荐