Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在应用程序关闭时更改变量_Python_Kivy - Fatal编程技术网

Python 在应用程序关闭时更改变量

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

我试图改变变量的值,这将停止一些线程,我想改变的是当你关闭应用程序时的值,我的意思是当你点击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 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()方法中
  • 有关详细信息,请参考示例

    在关闭窗口之前调用的事件。如果绑定函数返回 是,窗口将不会关闭

    例子 main.py 输出

    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()