[英]Passing values between the screens Kivy
我有一个有 2 个屏幕的 Open CV + Kivy 应用程序。 屏幕 1 (ProblemWindow) 将获取用户输入,来自网络摄像头的实时视频将显示在屏幕 2 (StepsWindow) 上。 但是,我需要将屏幕 1 (ProblemWindow) 中的一个值 (Spinner id: problem_id) 传递到屏幕 2 (StepsWindow),并且还需要使用 python 文件中的值来获取附加逻辑。
谁能指导我如何实现这一目标? 我被困在这里。
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()
Kivy 文件:
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"
您可以让 Kivy 为您处理,通过使用Properties
将文本分配给Label
problem_name
首先,为ProblemWindow
Screen
分配一个id
:
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 不会重复方法调用来更新值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.