windows 在kivy屏幕之间更新图像

k7fdbhmy  于 2023-01-10  发布在  Windows
关注(0)|答案(1)|浏览(97)

我遇到了一个问题,在一个更大的应用程序即时通讯,似乎愚蠢,但我不能弄清楚。我想让用户选择一个图像路径在屏幕1,然后有路径加载到屏幕2。这提出了2个问题。
1.如果路径第一次是空的,kivy无论如何都会运行第二个窗口,并且cv2读取一个空路径。我希望保持路径为空,并防止窗口运行,直到从前一个窗口加载路径为止
1.我的代码甚至没有从上一个窗口加载数据。我知道我可以直接更新图像源,但我正在尝试更新路径变量
下面是测试代码和测试图像

from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
import cv2

class WindowManager(ScreenManager):
    pass

class Window1(Screen, RelativeLayout):
    button_size = (100, 100)
    button_pos = (400, 400)

class Window2(Screen,RelativeLayout):
    path=''
    print(cv2.imread(path).shape)

KV = Builder.load_string("""

WindowManager:
    Window1:
    Window2:

<Window1>:
    name: 'one'
    RelativeLayout:
        Button:
            allow_stretch: True
            keep_ratio: True
            size_hint: None,None
            size: root.button_size
            pos: root.button_pos
            on_press: 
                root.manager.screens[1].path='/MRI-scan.jpg'
    
            on_release:
                app.root.current = "two" 
                root.manager.transition.direction = "left"  
<Window2>: 
    name: 'two'
    
    RelativeLayout:  
        Image:
            id: my_image
            size_hint: 0.5,0.5
            pos: (0,0)
            source: root.path

""")

class TApp(App):

    def build(self):
        return KV

if __name__ == '__main__':
    TApp().run()
xjreopfe

xjreopfe1#

我能够解决这个问题,但有一个缺点。任何人谁找出这个缺点,我会检查标记你的答案。我可以切换图像之间使用路径,我甚至不需要使用StringProperty。但是,我不能开始与路径=''。我需要开始与一个文件开始。这里是解决方案。任何人谁可以提供一个答案,开始与一个空字符串,我给你给予个勾,我用on_pre_enter解决了这个问题。

from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
import cv2

class WindowManager(ScreenManager):
    pass

class Window1(Screen, RelativeLayout):
    button_size = (100, 100)
    button_pos = (400, 400)

    btn2_size=(100,100)
    btn2_pos=(400,300)

    def on_touch_up(self, touch):
        if self.ids.btn1.collide_point(*touch.pos):
            self.manager.get_screen('two').path='img1'

        if self.ids.btn2.collide_point(*touch.pos):
            self.manager.get_screen('two').path = 'img2'

class Window2(Screen,RelativeLayout):
    btn3_size = (100, 100)
    btn3_pos = (400, 200)
    path='img start' #===> this is where I want to start with a blank string 
    print(cv2.imread(path).shape)
    def on_pre_enter(self, *args):
        self.manager.get_screen('two').ids['my_image'].source=self.path
        print(print(cv2.imread(self.path).shape)
)
KV = Builder.load_string("""

WindowManager:
    Window1:
    Window2:

<Window1>:
    name: 'one'
    RelativeLayout:
        Button:
            id: btn1
            text: 'one'
            allow_stretch: True
            keep_ratio: True
            size_hint: None,None
            size: root.button_size
            pos: root.button_pos 
            on_release:
                app.root.current = "two" 
                root.manager.transition.direction = "left" 
                
        Button:
            id: btn2
            text: 'two'
            allow_stretch: True
            keep_ratio: True
            size_hint: None,None
            size: root.btn2_size
            pos: root.btn2_pos
            on_release:
                app.root.current = "two" 
                root.manager.transition.direction = "left" 
               
<Window2>: 
    name: 'two'
    
    RelativeLayout:  
        Image:
            id: my_image
            size_hint: 0.5,0.5
            pos: (0,0)
            source: root.path
    
        Button:
            id: btn3
            text: 'previous'
            allow_stretch: True
            keep_ratio: True
            size_hint: None,None
            size: root.btn3_size
            pos: root.btn3_pos
          
    
            on_release:
                app.root.current = "one" 
                root.manager.transition.direction = "right"     
      
""")

class TApp(App):

    def build(self):
        return KV

if __name__ == '__main__':
    TApp().run()

相关问题