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

2024-10-01 05:05:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图加载一个文件选择器弹出窗口时,图像按钮被按下,并选择一个图像,以取代我的kivy/python文件中的图像,但我招致了一个错误的代码,这里是我的mre。任何帮助都将不胜感激

主文件.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()

千伏

<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的新手


Tags: fromposimportselfnonesizedefload
1条回答
网友
1楼 · 发布于 2024-10-01 05:05:39

LoadDialog类中缺少cancel属性。
另外,在ProfileWindow类中不需要loadcancel属性

这是您的.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()

相关问题 更多 >