我正在為應用程式制作加載螢屏,但在同時運行影片和演算法(需要很長時間才能運行)時遇到了一些問題。但是,當我運行那段代碼時,當我嘗試呼叫“self.ids.loading_anim”時,出現錯誤 AttributeError: 'super' object has no attribute ' getattr '。有人可以推薦一種更好的方法來在 kivy 中多執行緒影片嗎?請記住,當我多執行緒另一個演算法時,運行時間太長。
蟒蛇代碼:
def animate_image(self):
anim = Animation()
anim.start(self.ids.loading_anim)
class LoadingWindow(Screen):
def on_enter(self):
t = Thread(target=animate_image, args=(self))
t.deamon = True
t.start()
for x in range(5):
print(x) # this is just a test algorithm which takes 5 seconds to run
time.sleep(1) # in the real file there is another algorithm which takes time to run
基維代碼
<LoadingWindow>
FloatLayout:
Image:
id: animation
source: 'loading.gif'
size_hint_x:0.6
size_hint_y:0.6
pos_hint: {'x':0.19, 'y':0.2}
allow_stretch: True
anim_delay: 0
anim_reset: True
Label:
text: 'Searching the internet for recipes....'
pos_hint: {'x':0, 'y':0.3}
font_size: 28
uj5u.com熱心網友回復:
首先,我認為函式 animate_image() 可以放在類里面嗎?
你得到的錯誤是因為self.ids沒有一個名為 的鍵loading_anim,我認為應該是animation通過參考 .kv 檔案?
另一方面,我認為您也可以執行緒化演算法?
我還將MyApp帶有 Inherit的類添加到 kivy.app.App 并將 LoadingWindow 類放入 kivy ScreenManager。看看這是不是你想要的。
from threading import Thread
import time
import kivy
from kivy.clock import Clock
from kivy.app import App
from kivy.animation import Animation
from kivy.uix.screenmanager import Screen, ScreenManager
kivy.lang.Builder.load_string("""
#:kivy 2.0.0
<LoadingWindow>
FloatLayout:
Image:
id: animation
source: 'loading.gif'
size_hint_x:0.6
size_hint_y:0.6
pos_hint: {'x':0.19, 'y':0.2}
allow_stretch: True
anim_delay: 0
anim_reset: True
Label:
text: 'Searching the internet for recipes....'
pos_hint: {'x':0, 'y':0.3}
font_size: 28
""")
class LoadingWindow(Screen):
def animate_image(self): # can you put the animate_image() function to here ?
anim = Animation()
# anim.start(self.ids.loading_anim) # I think it should be animation ref to the .kv file ?
anim.start(self.ids.animation)
def your_algorithm(self):
for x in range(5):
print(x) # this is just a test algorithm which takes 5 seconds to run
time.sleep(1) # in the real file there is another algorithm which takes time to run
def on_enter(self):
t = Thread(target = self.animate_image)
t.deamon = True
t.start()
# instead of threading the animation , I think you can also thread the algorithm ?
algorithm = Thread(target = self.your_algorithm)
algorithm.start()
class MyApp(App):
screen_manager = ScreenManager()
def build(self):
# add screen to screen manager
self.screen_manager.add_widget(LoadingWindow(name = "LoadingWindow"))
return self.screen_manager
MyApp().run()
歡迎任何其他建議/建議/更好的方法/等:)
(sry 英語不好)
uj5u.com熱心網友回復:
我有類似的問題on_pre_enter&on_enter
從我了解的功能on_pre_enter和on_enter螢屏IDS加載之前,但我找到了一種方法,你可以使用被稱為
試試這個代碼:
from kivy.clock import Clock
def animate_image(self):
anim = Animation()
anim.start(self.ids.loading_anim)
class LoadingWindow(Screen):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Clock.schedule_once(self.init_completed)
def init_completed(self, dt):
t = Thread(target=animate_image, args=(self))
t.deamon = True
t.start()
for x in range(5):
print(x) # this is just a test algorithm which takes 5 seconds to run
time.sleep(1) # in the real file there is another algorithm which takes time to run
正如我在Screen on_pre_enter和on_enter中所說的那樣,在加載 ids 之前被呼叫,但是通過這種方式加載了 ids 然后你的函式 init_completed 被呼叫
為我作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/399888.html
