代码之家  ›  专栏  ›  技术社区  ›  Iorek

Kivy:更改屏幕上的图像源

  •  0
  • Iorek  · 技术社区  · 6 年前

    经过大量的头晕目眩,我创建了一个应用程序,打开文件选择器并选择一个图像。然后一个函数将转换图像并在屏幕上更改它。

    <<SecondScreen>是文件选择器。路径可能需要编辑。<ThirdScreen>应该显示图像以及最终将引导到其他地方的按钮。

    我想问题在于

    sm.current = "_third_screen_"
    

    正在创建一个新实例,该实例与我传递的其他项分开。如何显示所选图像?

    from kivy.lang import Builder
    from kivy.app import App
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.properties import ObjectProperty, StringProperty
    
    
    Builder.load_string("""
    
    <MyScreenManager>:
        FirstScreen:
        SecondScreen:
        ThirdScreen:
    
    <FirstScreen>:
        name: '_first_screen_'
        BoxLayout:
            orientation: "horizontal"
            Label:
                id: first_screen_label
                text: "Hi, I'm the home page"
            BoxLayout:
                orientation: "vertical"
                Button:
                    text: "Okay!"
                    on_press: root.manager.current = '_second_screen_'
                Button:
                    text: "Cancel!"
                    on_press: app.stop()
    
    <SecondScreen>:
        name: '_second_screen_'
        id: file_chooser
        BoxLayout:
            id: file_chooser_box_layout
            orientation: "horizontal"
            Button
                text: "Open"
                on_press: 
                    root.callback_image_and_other_stuff(file_chooser_list_view.selection)
            FileChooserListView:
                id: file_chooser_list_view
    
    <ThirdScreen>:
        name: '_third_screen_'
        BoxLayout:
            orientation: "vertical"
            id: third_screen
            Label:
                id: main_title
                text: "Upload"
                size_hint: (1, 0.1)
            Image:
                id: main_image
                source: root.img
                size_hint: (1, 0.75)
            BoxLayout:
                orientation: "horizontal"
                padding: 10
                size_hint: (1, 0.15)
                Button:
                    text: "Okay"
                    size_hint: (0.5, 1)
                    on_press: image_viewer.image_accepted_by_user(filechooser.selection)
                Button:
                    text: "Cancel"
                    size_hint: (0.5, 1) 
                    on_press: root.manager.current = '_first_screen_'   
    """)
    
    class FirstScreen(Screen):
        pass
    
    class SecondScreen(Screen):
        def callback_image_and_other_stuff(self, new_image_address):
            # do other stuff here also, then pass new_image_address along
            new_image_address = new_image_address[0].replace("\\", "/")
            third_screen = ThirdScreen()
            third_screen.callback_image(new_image_address)
    
    class ThirdScreen(Screen):
        img = ObjectProperty(None)
    
        def __init__(self, **kwargs):
            super(Screen, self).__init__(**kwargs)
    
        def callback_image(self, new_image_address):
            sm.current = "_third_screen_"
            self.img = new_image_address
            self.ids.main_image.source = self.img
    
            print(self.img)
    
    # Create the screen manager
    sm = ScreenManager() # Problem?
    sm.add_widget(FirstScreen(name='_first_screen_'))
    sm.add_widget(SecondScreen(name='_second_screen_'))
    sm.add_widget(ThirdScreen(name='_third_screen_'))
    
    class MyApp(App):
        def build(self):
            return sm
    
    if __name__ == '__main__':
        MyApp().run()
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   eyllanesc RAHUL KUMAR    6 年前

    每次你使用 some_obj = SomClass() 你正在创建一个新对象,在你的情况下,这就是你正在做的,你正在创建一个新的 ThirdScreen 与所示的不观察图像不同,解决方案是必须使用 ScreenManager 和名字屏幕。

    from kivy.lang import Builder
    from kivy.app import App
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.properties import ObjectProperty, StringProperty
    
    
    Builder.load_string("""
    <FirstScreen>:
        BoxLayout:
            orientation: "horizontal"
            Label:
                id: first_screen_label
                text: "Hi, I'm the home page"
            BoxLayout:
                orientation: "vertical"
                Button:
                    text: "Okay!"
                    on_press: root.manager.current = '_second_screen_'
                Button:
                    text: "Cancel!"
                    on_press: app.stop()
    
    <SecondScreen>:
        id: file_chooser
        BoxLayout:
            id: file_chooser_box_layout
            orientation: "horizontal"
            Button
                text: "Open"
                on_press: 
                    root.callback_image_and_other_stuff(file_chooser_list_view.selection)
            FileChooserListView:
                id: file_chooser_list_view
    
    <ThirdScreen>:
        BoxLayout:
            orientation: "vertical"
            id: third_screen
            Label:
                id: main_title
                text: "Upload"
                size_hint: (1, 0.1)
            Image:
                id: main_image
                source: root.img
                size_hint: (1, 0.75)
            BoxLayout:
                orientation: "horizontal"
                padding: 10
                size_hint: (1, 0.15)
                Button:
                    text: "Okay"
                    size_hint: (0.5, 1)
                    on_press: image_viewer.image_accepted_by_user(filechooser.selection)
                Button:
                    text: "Cancel"
                    size_hint: (0.5, 1) 
                    on_press: root.manager.current = '_first_screen_'   
    """)
    
    class FirstScreen(Screen):
        pass
    
    class SecondScreen(Screen):
        def callback_image_and_other_stuff(self, new_image_address):
            if new_image_address:
                third_screen = self.manager.get_screen("_third_screen_")
                # do other stuff here also, then pass new_image_address along
                new_image_address = new_image_address[0].replace("\\", "/")
                third_screen.callback_image(new_image_address)
    
    class ThirdScreen(Screen):
        img = ObjectProperty(None)
    
        def __init__(self, **kwargs):
            super(Screen, self).__init__(**kwargs)
    
        def callback_image(self, new_image_address):
            sm.current = "_third_screen_"
            self.img = new_image_address
            self.ids.main_image.source = self.img
    
            print(self.img)
    
    # Create the screen manager
    sm = ScreenManager() # Problem?
    sm.add_widget(FirstScreen(name='_first_screen_'))
    sm.add_widget(SecondScreen(name='_second_screen_'))
    sm.add_widget(ThirdScreen(name='_third_screen_'))
    
    class MyApp(App):
        def build(self):
            return sm
    
    if __name__ == '__main__':
        MyApp().run()