【问题标题】:How to place the image selected in a BoxLayout with Kivy如何使用 Kivy 将所选图像放置在 BoxLayout 中
【发布时间】:2021-04-30 00:55:33
【问题描述】:

我正在尝试使用 KivyMD FileManager 选择图像并将其放置在 boxlayout 中,但它会产生很多错误,例如(AttributeError: 'super' object has no attribute 'getattr')当我使用self.root.ids.image.source = path 时,另一个尝试self.root.ids["image"].source = path 给了我(KeyError: 'image') 和self.ids["image"].source = path 给了AttributeError: ('MyMainApp' 对象没有属性'ids')。这是python代码:


from kivymd.app import MDApp
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.filemanager import MDFileManager
from kivy.uix.image import Image
from kivymd.toast import toast
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty


class MainWindow(Screen):
    pass

class SecondWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass

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

class MyMainApp(MDApp):
    def __init__(self, **kwargs):
        super(MyMainApp, self).__init__(**kwargs)
        self.manager_open = False
        self.file_manager = MDFileManager(
            exit_manager=self.exit_manager,  # function called when the user reaches directory tree root
            select_path=self.select_path,  # function called when selecting a file/directory
            #preview=True,
        )


    def file_manager_open(self):
        self.file_manager.show("/")  # output manager to the screen
        self.manager_open = True

    def select_path(self, path):
        """It will be called when you click on the file name
        or the catalog selection button.

        :type path: str;
        :param path: path to the selected directory or file;
        """

        self.exit_manager()

        toast(path)

    def exit_manager(self, *args):
        """Called when the user reaches the root of the directory tree."""

        self.manager_open = False
        self.file_manager.close()

    def build(self):
        return kv

if __name__ == "__main__":
    MyMainApp(title="Noyse Remove").run()

.kv 文件

WindowManager:
    MainWindow:
    SecondWindow:

<MainWindow>:
    name: "main"
    id: pri
    canvas:
        Rectangle:
            source: 'img/entrada.jpg'
            size: self.width, self.height

    Button:
        text: "Open Archives"
        size_hint: 0.166, 0.075
        pos_hint: {"center_x": .5, "center_y": .3}
        on_release:
            app.root.current = "second"
            root.manager.transition.direction = "left"
            app.file_manager_open()

<SecondWindow>:
    name: "second"
    id: sec
    canvas.before:
        Color:
            rgba: .01, .01, .01, 1
        Rectangle:
            pos: self.pos
            size: self.size


    BoxLayout:
        id: box
        orientation: "horizontal"
        size: root.width, root.height
        padding: 50
        spacing: 50
        Image:
            id: image
            source: "" # I want to place here the path of image selected
            size:root.width, root.height
            pos_hint: {'x': .1, 'y': .1}



    Slider:
        size_hint: 0.6, 0.1
        min: 0
        max: 100
        on_value: label.text = str(self.value)
        pos_hint:{'center_x': .5, 'center_y': .1}
    Label:
        id: label
        text: "0.0"
        pos_hint:{'center_x': .5, 'center_y': .05}

【问题讨论】:

    标签: python android kivy kivymd


    【解决方案1】:

    image id&lt;SecondWindow&gt; 规则中定义,因此id 可从ids 实例中的ids 获得。所以你必须得到SecondWindow 的实例,它在你的MyMainApp 中。您可以将Image source 设置为:

    self.root.get_screen('second').ids.image.source = path
    

    假设这段代码在MyMainApp的方法中执行。 self.rootApp 的根小部件,即WindowManagerget_screen('second') 方法获取对SecondWindow 实例的引用,ids.image 获取对Image 实例的引用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      相关资源
      最近更新 更多