如何在从属模式下运行Kivy EventLoop?



相关问题在这里

我发现了runTouchApp函数的slave属性,它可以阻止 Kivy 的事件循环运行并强制从其他地方更新它.
这是使用该属性的app.py的一部分:

# we are in a slave mode, don't do dispatching.
if slave:
return
try:
if EventLoop.window is None:
_run_mainloop()
else:
EventLoop.window.mainloop()
finally:
stopTouchApp()

在这里,如果应用程序不是在从属模式下运行的,我们有两个选择来运行mainloop.
第一个,_run_mainloop()函数工作非常简单 - 它只是调用EventLoop.run(),而又无限调用EventLoop.idle().
这可能会导致我们相信,要保持GUI运行, 我们只需要打电话给idle.

但是还有第二个选项,它将kivy.core.window.WindowSDL的方法称为mainloop.
该方法通过调用另一个方法(_mainloop)来工作,这就是它变得有趣的地方。所述方法的定义是巨大的,它处理各种事件。

好吧,我在从属模式下运行我的应用程序:

class TestApp(App):
def start_event(self):
pass
def build(self):
return Button(text = "hello")
def run(self):
# This definition is copied from the superclass 
# except for the start_event call and slave set to True
if not self.built:
self.load_config()
self.load_kv(filename=self.kv_file)
root = self.build()
if root:
self.root = root
if self.root:
Window.add_widget(self.root)
window = EventLoop.window
if window:
self._app_window = window
window.set_title(self.get_application_name())
icon = self.get_application_icon()
if icon:
window.set_icon(icon)
self._install_settings_keys(window)

self.dispatch('on_start')
runTouchApp(slave = True)
self.start_event()  # Here we start updating
self.stop()

现在,如果我把它放在start_event方法中(按期望):

def start_event(self):
while True:
EventLoop.idle()

你猜怎么着,该应用程序不响应触摸事件并冻结.
所以我尝试调用窗口的主循环:

def start_event(self):
EventLoop.window.mainloop()

突然间,一切又开始正常工作。但这里的问题是这样的调用会永远阻塞,因为它是一个无限循环,所以没有像EventLoop.idle这样的一次性更新调用

如何使用这种一次性调用保持应用程序运行?

好吧,这是 Python,所以假设你想坚持使用WindowSDL提供者,你总是可以猴子修补这个mainloop函数,这样它就不会是无限的:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.base import EventLoop
Builder.load_string('''
<MyWidget>:
Button:
text: 'test'
on_press: print('doing my job')
''')
# https://github.com/kivy/kivy/blob/master/kivy/core/window/window_sdl2.py#L630
def mainloop(self):
# replaced while with if
if not EventLoop.quit and EventLoop.status == 'started':
try:
self._mainloop()
except EventLoop.BaseException as inst:
# use exception manager first
r = EventLoop.ExceptionManager.handle_exception(inst)
if r == EventLoop.ExceptionManager.RAISE:
EventLoop.stopTouchApp()
raise
else:
pass

class MyWidget(BoxLayout):
pass

if __name__ == '__main__':
from kivy.base import runTouchApp
runTouchApp(MyWidget(), slave=True)
# monkey patch
EventLoop.window.mainloop = mainloop
while True:
EventLoop.window.mainloop(EventLoop.window)
print('do the other stuff')
if EventLoop.quit:
break

不过,这真的很笨拙,因此我不建议在生产代码中运行这样的东西。

最新更新