Python Kivy弹出窗口出现在函数运行后而不是之前
我想显示一个弹出窗口或模式,当应用程序在后台执行某项功能时,它会显示“处理时请耐心等待”之类的内容。但是,在后台功能完成后会出现弹出窗口。下面是产生此问题的代码示例Python Kivy弹出窗口出现在函数运行后而不是之前,python,kivy,Python,Kivy,我想显示一个弹出窗口或模式,当应用程序在后台执行某项功能时,它会显示“处理时请耐心等待”之类的内容。但是,在后台功能完成后会出现弹出窗口。下面是产生此问题的代码示例 import os import time from kivy.app import App from kivy.uix.modalview import ModalView from kivy.uix.popup import Popup from kivy.uix.button import Button, Label c
import os
import time
from kivy.app import App
from kivy.uix.modalview import ModalView
from kivy.uix.popup import Popup
from kivy.uix.button import Button, Label
class Poppy(Popup):
def __init__(self, **kwargs):
super(Poppy, self).__init__(**kwargs)
self.content = Label(text='working')
self.open()
print("Working...")
class TApp(App):
def build(self):
return Button(text="Click to run", on_press=self.modal_test)
def modal_test(self, event):
p = Poppy(size_hint=(0.5, 0.5))
self.printer()
def printer(self):
print('Popup works')
time.sleep(5)
TApp().run()
您不应该在同一GUI线程上执行耗时的任务,因为它会阻止eventloop,从而导致GUI无法正确运行。在这些情况下,您必须在新线程中运行它
import threading
# ...
class TApp(App):
def build(self):
return Button(text="Click to run", on_press=self.modal_test)
def modal_test(self, event):
p = Poppy(size_hint=(0.5, 0.5))
threading.Thread(target=self.printer, daemon=True).start()
def printer(self):
print('before: Popup works')
time.sleep(5)
print('after: Popup works')
# ...
还建议您检查一下谢谢,这非常有效,链接信息也非常有用。