我有一個有 2 個螢屏的 Open CV Kivy 應用程式。螢屏 1 (ProblemWindow) 將獲取用戶輸入,來自網路攝像頭的實時視頻將顯示在螢屏 2 (StepsWindow) 上。但是,我需要將螢屏 1 (ProblemWindow) 中的一個值 (Spinner id: problem_id) 傳遞到螢屏 2 (StepsWindow) 中,并且還需要使用 python 檔案中的值來獲取附加邏輯。
誰能指導我如何實作這一目標?我被困在這里。
蟒蛇檔案:
# importing kivy dependencies
from kivy.app import App
from kivy.clock import Clock
from kivy.graphics.texture import Texture
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.widget import Widget
# import other dependencies
import cv2 as cv
import numpy as np
from cv2 import aruco
# defining different screens
class WindowManager(ScreenManager):
pass
class ProblemWindow(Screen):
def selected_problem(self, value):
self.ids.click_label.text = f'selected problem: {value}'
return value
class StepsWindow(Screen):
def __init__(self, **kwargs):
print('Steps Window')
super().__init__(**kwargs)
# Capture the frames from the webcamera
self.capture = cv.VideoCapture(0)
Clock.schedule_interval(self.run_webcam, 1.0/30.0)
def run_webcam(self, *args):
"""Run continuously to get webcam feed"""
ret, frame = self.capture.read()
# Flip Horizontal and convert image into texture
buf = cv.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.ids.web_cam.texture = img_texture # id is the id of the image from the kv file.
kv = Builder.load_file('window_manager.kv')
class main(App):
def build(self):
return kv
if __name__ == '__main__':
main().run()
cv.destroyAllWindows()
基維檔案:
WindowManager:
ProblemWindow:
StepsWindow:
<ProblemWindow>:
name: "problem_window"
GridLayout:
rows: 3
size: root.width, root.height
Label:
id: click_label
text: "Select a Problem"
font_size: 32
height: 20
Spinner:
id: problem_id
text: "selected problem"
values: ["Problem_1", "Problem_2"]
on_text: root.selected_problem(problem_id.text)
size_hint: 0.5, 0.5
Button:
text: "Show Steps"
font_size: 32
size_hint: 0.3, 0.3
on_release:
app.root.current = "steps_window"
root.manager.transition.direction = "left"
<StepsWindow>:
name: "steps_window"
web_cam: web_cam
GridLayout:
rows: 3
size: root.width, root.height
Label:
id: problem_name
text: "Selected Problem Name should come here"
font_size: '42'
size_hint_y: None
height: 50
Image:
id: web_cam
Button:
text: "Exit"
font_size: 32
size_hint: 0.1, 0.1
on_release:
app.root.current = "problem_window"
root.manager.transition.direction = "right"
uj5u.com熱心網友回復:
problem_name Label您可以通過將文本分配給using來讓 Kivy 為您處理Properties。首先,分配id一個ProblemWindow Screen:
WindowManager:
ProblemWindow:
id: pw
StepsWindow:
然后使用它id來分配文本:
Label:
id: problem_name
text: root.manager.ids.pw.ids.problem_id.text
font_size: '42'
size_hint_y: None
height: 50
因為文本是基于ids和分配的Properties,Kivy 會為您保持更新。如果您嘗試使用root.manager.get_screen('problem_window'),它將無法正常作業,因為 Kivy 不會重復方法呼叫來更新值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/510784.html
