В Kivy как отобразить Image виджеты(и не только) на второй странице(Screen)? Python
Прошу помощи с кодом.
С первой странице через кнопку переходим на вторую, куда на виджет Image с помощью функции video идёт поток изображений с вебкамеры.
У меня не получается "ухватиться" за виджет - выпадает такая ошибка:
Traceback (most recent call last):
File "kivy\properties.pyx", line 961, in kivy.properties.ObservableDict.__getattr__
KeyError: 'vid'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:\kivy\test.py", line 60, in <module>
TestApp().run()
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\app.py", line 956, in run
runTouchApp()
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 574, in runTouchApp
EventLoop.mainloop()
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 339, in mainloop
self.idle()
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 379, in idle
Clock.tick()
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\clock.py", line 733, in tick
self.post_idle(ts, self.idle())
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\clock.py", line 776, in post_idle
self._process_events()
File "kivy\_clock.pyx", line 620, in kivy._clock.CyClockBase._process_events
File "kivy\_clock.pyx", line 653, in kivy._clock.CyClockBase._process_events
File "kivy\_clock.pyx", line 649, in kivy._clock.CyClockBase._process_events
File "kivy\_clock.pyx", line 218, in kivy._clock.ClockEvent.tick
File "F:\kivy\test.py", line 56, in video
self.root.ids.vid.image.texture = img_texture # AttributeError: 'super' object has no attribute '__getattr__'
File "kivy\properties.pyx", line 964, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
или такая:
AttributeError: 'NoneType' object has no attribute 'texture'
Вот код с .py:
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.graphics.texture import Texture
from kivy.config import Config
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.app import App
import cv2
#Config.set('graphics', 'width', 1024)
#Config.set('graphics', 'height', 720)
class ScreenMain(Screen):
pass
class ScreenVideo(Screen):
image = ObjectProperty(None)
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file('test.kv')
class TestApp(App):
def build(self):
sm = WindowManager()
sm.add_widget(ScreenMain(name='main_screen'))
sm.add_widget(ScreenVideo(name='video_screen'))
self.screen_video = ScreenVideo()
return sm
def loop(self):
Clock.schedule_interval(self.video, 1)
def video(self, *args):
capture = cv2.VideoCapture(0)
ret, frame = capture.read()
# capture.set(3, 480)
# capture.set(4, 640)
buf = cv2.flip(frame, 0).tobytes()
img_texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
img_texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
self.root.ids.vid.image.texture = img_texture # AttributeError: 'super' object has no attribute '__getattr__'
# self.screen_video.image.texture = img_texture # AttributeError: 'NoneType' object has no attribute 'texture'
#self.root.ids.vid.image.remove_from_cache()
if __name__ == "__main__":
TestApp().run()
.kv файл:
WindowManager:
ScreenMain:
id: main
ScreenVideo:
id: vid
<ScreenMain>:
name: "main_screen"
BoxLayout:
orientation: "vertical"
Label:
text: "something"
Button:
text: 'Next screen'
on_release:
app.loop()
app.root.current = "video_screen"
<ScreenVideo>:
name: "video_screen"
BoxLayout:
image: image
Image:
id: image