在kivy python中显示来自不同功能的图像

在kivy python中显示来自不同功能的图像,python,opencv,kivy,kivy-language,Python,Opencv,Kivy,Kivy Language,嗨,我正在尝试建立一个扫描仪,它采取了一个图像,并点击提交按钮,它应该返回结果图像在新的屏幕上,这是我到现在为止,帮助是非常感谢。提前谢谢 from kivy.app import App from kivy.lang import Builder import time from kivy.uix.boxlayout import BoxLayout from kivy.uix.image import Image Builder.load_string( ''' <CameraCli

嗨,我正在尝试建立一个扫描仪,它采取了一个图像,并点击提交按钮,它应该返回结果图像在新的屏幕上,这是我到现在为止,帮助是非常感谢。提前谢谢

from kivy.app import App
from kivy.lang import Builder
import time
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image

Builder.load_string( '''
<CameraClick>:
orientation: 'vertical'
Camera:
    id: camera
    resolution: 500,500
BoxLayout:
    orientation: 'horizontal'
    size_hint_y: None
    height: '48dp'
    Button:
        text: 'Click'
        on_press: root.capture()
        on_release: camera.play = False
    Button:
        text: 'Submit'
        on_press: root.capture()
        on_release: camera.play = False
''')

class CameraClick(BoxLayout):
def capture(self):
    '''
    Function to capture the images and give them the names
    according to their captured time and date.
    '''
    camera = self.ids['camera']
    print("camera down")
    print(type(camera))
    timestr = time.strftime("%Y%m%d_%H%M%S")
    camera.export_to_png("IMG_{}.png".format(timestr))
    print("Captured")
    return Image(source='hey.png')

#  def release(self):
    

class CameraApp(App):
    def build(self):
        return CameraClick()


if __name__ == '__main__':
    CameraApp().run()
从kivy.app导入应用
从kivy.lang导入生成器
导入时间
从kivy.uix.boxlayout导入boxlayout
从kivy.uix.image导入图像
Builder.load_字符串(“”)
:
方向:“垂直”
摄像机:
id:照相机
决议:500500
盒子布局:
方向:“水平”
尺寸提示:无
高度:“48dp”
按钮:
文本:“单击”
on_press:root.capture()
发布时:camera.play=False
按钮:
文本:“提交”
on_press:root.capture()
发布时:camera.play=False
''')
类CameraClick(框布局):
def捕获(自我):
'''
函数捕获图像并为其命名
根据他们捕获的时间和日期。
'''
camera=self.ids['camera']
打印(“相机向下”)
打印(打印(照相机))
timestr=time.strftime(“%Y%m%d\u%H%m%S”)
camera.export_to_png(“IMG_{}.png.format(timestr))
打印(“捕获”)
返回图像(source='hey.png')
#def释放(自):
类CameraApp(应用程序):
def生成(自):
返回CameraClick()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
CameraApp().run()

当您单击按钮时,
Kivy
运行分配的函数,但它不会得到返回值-它不知道如何处理返回值

您必须将值分配给全局变量或类(使用
self.
),其他函数必须从该变量获取值

def capture(self):
    self.image = Image(source='hey.png')

def other_function(self):
    do_something(self.image)
如果
other_函数
可以在
capture
之前执行,那么最好在开始时使用一些默认值创建此变量-即
None

class CameraClick(BoxLayout):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.image = None
然后您可以检查是否执行了
capture

def other_function(self):

    if self.image:
        do_something(self.image)
    else:
        print('Image not captured yet')

如果您想在不同的类中使用该值,那么您可能必须将其分配给全局变量-最终如果可能,您可以将一个类的实例作为参数发送给另一个类

camera_click =  CameraClick()
OtherClass(camera_click)
其他类可以保留它,以后可以使用它来获取图像

class OtherClass():

    def __init__(self, camera, **kwargs):
        super().__init__(**kwargs)

        self.camera = camera


    def some_function(self):
       
        if self.camera.image:
            do_something(self.camera.image)
        else:
            print('Image not captured yet')

顺便说一句:有时类可能有相同的
父类
,然后
其他类
可以使用

 self.parent.camera_click.image
分配给按钮(或菜单)的函数无法返回值,因为没有对象可以获取此值。您应该将其分配给全局变量或类变量(即,
self.image
),其他类/函数应该从该变量获取它。