Python 在应用程序关闭时更改变量
我试图改变变量的值,这将停止一些线程,我想改变的是当你关闭应用程序时的值,我的意思是当你点击X,它被关闭 我正在尝试一些东西,但不起作用:Python 在应用程序关闭时更改变量,python,kivy,Python,Kivy,我试图改变变量的值,这将停止一些线程,我想改变的是当你关闭应用程序时的值,我的意思是当你点击X,它被关闭 我正在尝试一些东西,但不起作用: def on_request_close(self): close() print("awdw") self.textpopup(title='Exit', text='Are you sure?') 这是我在请求时调用的on\u close()方法: 您可能需要执行类似于Window.bind的操作(on\u request\u
def on_request_close(self):
close()
print("awdw")
self.textpopup(title='Exit', text='Are you sure?')
这是我在请求时调用的on\u close()
方法:
您可能需要执行类似于
Window.bind的操作(on\u request\u close=on\u request\u close)
解决方案
return True
附加到on\u request\u close()
方法Window.bind(on\u request\u close=self.on\u request\u close)
添加到build()方法中
def close(self):
print("Cerrando")
print(str(self.exit))
self.exit = False
on_request_close(*largs, **kwargs)
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.properties import BooleanProperty
from kivy.core.window import Window
class MyApp(App):
exit = BooleanProperty(True)
def build(self):
Window.bind(on_request_close=self.on_request_close)
return Label(text="Hello world")
def on_request_close(self, *largs, **kwargs):
print('on_request_close:')
self.close()
print("awdw")
self.textpopup(title='Exit', text='Are you sure?')
return True
def close(self):
print("Cerrando")
print(str(self.exit))
self.exit = False
def textpopup(self, title='', text=''):
print('textpopup:')
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=(400, 400))
mybutton.bind(on_release=self.stop)
popup.open()
if __name__ == '__main__':
MyApp().run()