I've come to a problem in a larger app im writing that seems dumb but I can't figure it out. I want to have the user select an image path on screen1 and then have the path load onto screen2. This presents 2 problems.
if the path is first empty, kivy runs the second window anyway, and cv2 reads an empty path. I would like to keep the path empty and prevent the window from running until the path is loaded from the previous window
my code isn't even loading the data from the previous window. I know i could update the image source directly, but I am seeking to update the path variable
Here is a test code and test image
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()
I was able to solve the problem with one shortcoming. Anyone who figures out this shortcoming, I'll check mark your answer. I can switch between images using the path and I did not even need to use StringProperty. However, I cannot start with path=''. I need to start with a file initially. Here's the solution. Anyone who can provide an answer by starting with an empty string, I'll give a check mark to you. I solved the problem with 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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.