【发布时间】:2021-07-29 11:58:32
【问题描述】:
我正在使用 Kivy 创建一个简单的两屏界面,用于执行图像分类任务。在第一个屏幕中,我使用文件选择器选择图像并显示它。在第二个屏幕中,我想显示相同的图像和分类任务的结果。屏幕之间的转换是通过第一个屏幕上的按钮完成的。
我的问题是:当我按下按钮时,如何触发第二屏上图像源属性的更新,以便将所选图像显示在第二屏上? 分类部分只是为了了解我的问题的背景,我没有在代码中包含它。
这是main.py 文件
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty
class WindowManager(ScreenManager):
image_source = StringProperty()
def selected(self,filename):
try:
self.image_source = filename[0]
except:
pass
# Screen where the image is selected
class ImageSelector(Screen):
pass
# Display image & classification results
class ClassificationResultWindow(Screen):
pass
class MainApp(App):
def build(self):
self.image_selector = ImageSelector()
self.scan_result_window = ClassificationResultWindow()
if __name__ == "__main__":
MainApp().run()
这是main.kv 文件
#:kivy 2.0.0
WindowManager:
ImageSelector:
ClassificationResultWindow:
<ImageSelector>:
name: "image_selector"
id: image_selector
BoxLayout:
orientation: 'vertical'
id: image_box
FileChooserListView:
id: filechooser
on_selection:
root.manager.selected(filechooser.selection)
print(root.manager.image_source)
size_hint: 1, 10
Image:
id: image
source: root.manager.image_source
size_hint: 1, 4
Button:
id: diagnose
text: "Classify"
on_release:
print(root.manager.image_source)
app.root.current = "classification_result"
<ClassificationResultWindow>:
name: "classification_result"
BoxLayout:
orientation: 'vertical'
id: box
Image:
id: scan
source: root.manager.image_source
size_hint: 1, 10
Label:
text: "Here comes the classification result"
font_size: 30
size_hint: 1, 2
id: label
我尝试以不同的方式绑定属性但没有成功,但由于我是 kivy 的新手,我不知道这是否有意义,所以我没有在此处包含它们。
【问题讨论】:
标签: image kivy kivy-language