当我试图通过单击图像按钮打开文件选择器时发生错误

问题描述 投票:1回答:1

im试图在按下图像按钮时加载Filechooser弹出窗口,然后选择一个图像来替换我的kivy / python文件中的图像,但是我在代码中出现错误,这是我的错误。任何帮助,将不胜感激。

mainfile.py

import kivy
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
from kivy.uix.button import ButtonBehavior
from kivy.uix.image import Image
from kivy.lang import Builder
from kivy.app import App



class ImageButton(ButtonBehavior, Image):
    pass


class LoadDialog(FloatLayout):
    load = ObjectProperty(None)
    save = ObjectProperty(None)

class ProfileWindow(Screen):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)


    def __init__(self, **kwargs):
        super(ProfileWindow, self).__init__(**kwargs)
        self.profileimage = ImageButton(source= "icons/profilepic.png", pos_hint={"center_x": 0.5, "center_y": 0.65})
        self.profileimage.bind(on_release= self.imagechange)
        self.add_widget(self.profileimage)

    def imagechange(self, instance):
        content = LoadDialog(load=self.load_list, cancel=self.dismiss_popup)
        self._popup = Popup(title="Load a file list", content=content, size_hint=(1, 1))
        self._popup.open()

    def load_list(self, path, filename):
        pass

    def dismiss_popup(self):
        self._popup.dismiss()



kv = Builder.load_file("kivy.kv")

class WindowManager(ScreenManager):
    pass

sm = WindowManager()

sm.current = "page"

class MyApp(App):
    def build(self):
        return sm

if __name__ == "__main__":
    MyApp().run()

kivy.kv

<WindowManager>:
    id: window_manager
    ProfileWindow:
        id: page
        name: "page"

<ProfileWindow>:

    full_name: fullname
    jobs: jobs

    canvas.before:
        Color:
            rgba: 1, 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size

<LoadDialog>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser
            path: './'
        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: "Cancel"
                on_release: root.cancel()
            Button:
                text: "Load"
                on_release: root.load(filechooser.path, filechooser.selection)

任何可以借的帮助将不胜感激。我是kivy和python的新手

python kivy
1个回答
0
投票

您在cancel类中缺少LoadDialog属性。另外,您在load类中不需要cancelProfileWindow属性>

这里是您的.py文件已固定:

class ImageButton(ButtonBehavior, Image):
    pass


class LoadDialog(FloatLayout):
    load = ObjectProperty(None)
    save = ObjectProperty(None)
    cancel = ObjectProperty(None)


class ProfileWindow(Screen):

    def __init__(self, **kwargs):
        super(ProfileWindow, self).__init__(**kwargs)
        self.profileimage = ImageButton(source="icons/profilepic.png",
                                        pos_hint={"center_x": 0.5, "center_y": 0.65})
        self.profileimage.bind(on_release=self.imagechange)
        self.add_widget(self.profileimage)

    def imagechange(self, instance):
        content = LoadDialog(load=self.load_list, cancel=self.dismiss_popup)
        self._popup = Popup(title="Load a file list", content=content, size_hint=(1, 1))
        self._popup.open()

    def load_list(self, path, filename):
        self.profileimage.source = filename[0]
        print(path, filename)
        self.dismiss_popup()

    def dismiss_popup(self):
        self._popup.dismiss()


kv = Builder.load_file("kivy.kv")


class WindowManager(ScreenManager):
    pass


sm = WindowManager()

sm.current = "page"


class MyApp(App):
    def build(self):
        return sm


if __name__ == "__main__":
    MyApp().run()
© www.soinside.com 2019 - 2024. All rights reserved.