拖放图像并在网格布局上显示图像 - Kivy

Abi*_*oor 2 python python-3.x kivy kivy-language

我是 kivy 的新手,我正在尝试创建一个拖放应用程序,如果将图像拖放到网格布局上,则图像将显示在应用程序上,如下所示。我试图获取图像的文件路径,然后使用文件路径将其显示在网格布局上,但不幸的是,这不起作用。任何帮助将不胜感激!

这是当前图像在此输入图像描述

这就是我拖动图片后想要的效果在此输入图像描述

Kv文件

# Custom button
<CustButton@Button>:
    font_size: 32
    background_normal: 'Colour_yellow.png'
    background_down: 'Colour_blue.png'

<Cust2@Button>:
    font_size: 32
    background_normal: 'Colour_red.png'
    background_down: 'Colour_blue.png'

<Cust3@Button>:
    font_size: 32
    background_normal: 'Colour_white.png'
    background_down: 'Colour_blue.png'

<Cust4@Button>:
    font_size: 32
    background_normal: 'Colour_blue.png'
    background_down: 'Colour_white.png'

<CalcGridLayout>:
    id: calculator
    display: entry
    rows: 5
    padding: 10
    spacing: 10

    BoxLayout:
        spacing: 100
        size_hint: .5, None
        Cust2:
            text: "Whats the intensity you want?"

    BoxLayout:

        size_hint: .5, None
        TextInput:
            id: entry
            font_size: 70
            multiline: False
            hint_text: "Type here"

    BoxLayout:
        spacing: 100
        size_hint: .5, None
        Cust4:
            text: "Drag and Drop picture below:"
            on_release: root.build1()

    #THIS IS WHERE I'M STUCK ON
    BoxLayout:
        Image:
            source: root._on_file_drop(file_path)

    BoxLayout:
        size_hint: 1, .3
        spacing: 10

        CustButton:
            text: "Click here for \n reduced size"

        CustButton:
            text: "Click here for pos \n  and intensity of \n      each pixel"
            on_release: root.reduced_image()

        CustButton:
            text: "Click here \n for graph"

        CustButton:
            text: "Click here \n     for all"

        CustButton:
            text: "Extra"
Run Code Online (Sandbox Code Playgroud)

Python文件

import kivy

kivy.require("1.9.0")

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
Window.clearcolor = (0.5, 0.5, 0.5, 1)


class CalcGridLayout(GridLayout):

    def reduced_image(self):
    #ignore

    def build1(self):
        Window.bind(on_dropfile=self._on_file_drop)
        return

    def _on_file_drop(self, window, file_path):
        print(file_path)
        return file_path



class dropdownApp(App):

    def build(self):
        return CalcGridLayout()


dropApp = dropdownApp()
dropApp.run()
Run Code Online (Sandbox Code Playgroud)

iko*_*lim 5

解决方案

\n\n

请参阅片段和完整示例以了解详细信息。

\n\n

kv文件

\n\n
    \n
  1. 消除on_release: root.build1()
  2. \n
  3. 代替source: root._on_file_drop(file_path)用。。。来id: img
  4. \n
\n\n

片段

\n\n
BoxLayout:\n    spacing: 100\n    size_hint: .5, None\n    Cust4:\n        text: "Drag and Drop picture below:"\n\nBoxLayout:\n    Image:\n        id: img\n
Run Code Online (Sandbox Code Playgroud)\n\n

Python代码

\n\n
    \n
  1. 添加导入语句,from kivy.properties import StringProperty
  2. \n
  3. 申报财产,filePath = StringProperty(\'\')class CalcGridLayout().
  4. \n
  5. 将构造函数添加到方法并从方法中class CalcGridLayout()移动Window.bind(on_dropfile=self._on_file_drop)build1()
  6. \n
  7. 去除build1()方法
  8. \n
  9. _on_file_drop()方法中,替换return file_pathself.filePath = file_path.decode("utf-8") self.ids.img.source = self.filePathandself.ids.img.reload()
  10. \n
\n\n

片段

\n\n
from kivy.properties import StringProperty\n...\n\nclass CalcGridLayout(GridLayout):\n    filePath = StringProperty(\'\')\n\n    def __init__(self, **kwargs):\n        super(CalcGridLayout, self).__init__(**kwargs)\n        Window.bind(on_dropfile=self._on_file_drop)\n\n    def reduced_image(self):\n        print(self.filePath)\n\n    def _on_file_drop(self, window, file_path):\n        print(file_path)\n        self.filePath = file_path.decode("utf-8")     # convert byte to string\n        self.ids.img.source = self.filePath\n        self.ids.img.reload()   # reload image\n
Run Code Online (Sandbox Code Playgroud)\n\n

窗口 \xc2\xbb on_dropfile 事件

\n\n
\n
on_dropfile(filename)\n
Run Code Online (Sandbox Code Playgroud)\n\n

将文件拖放到应用程序上时调用的事件。

\n\n

警告

\n\n

此事件当前适用于 sdl2 窗口提供程序、pygame 窗口\n 提供程序和带有 pygame 修补版本的 OS X。此事件保留\n以供进一步发展(ios、android 等)

\n
\n\n

例子

\n\n

主要.py

\n\n
import kivy\nkivy.require("1.11.0")\n\nfrom kivy.app import App\nfrom kivy.uix.gridlayout import GridLayout\nfrom kivy.core.window import Window\nfrom kivy.properties import StringProperty\n\nWindow.clearcolor = (0.5, 0.5, 0.5, 1)\n\n\nclass CalcGridLayout(GridLayout):\n    filePath = StringProperty(\'\')\n\n    def __init__(self, **kwargs):\n        super(CalcGridLayout, self).__init__(**kwargs)\n        Window.bind(on_dropfile=self._on_file_drop)\n\n    def reduced_image(self):\n        print(self.filePath)\n\n    def _on_file_drop(self, window, file_path):\n        print(file_path)\n        self.filePath = file_path.decode("utf-8")     # convert byte to string\n        self.ids.img.source = self.filePath\n        self.ids.img.reload()   # reload image\n\n\nclass DragDropApp(App):\n\n    def build(self):\n        return CalcGridLayout()\n\n\nif __name__ == "__main__":\n    DragDropApp().run()\n
Run Code Online (Sandbox Code Playgroud)\n\n

拖放.kv

\n\n
#:kivy 1.11.0\n\n# Custom button\n<CustButton@Button>:\n    background_normal: "/home/iam/Pictures/AppImages/Colors/yellow.png"   # \'Colour_yellow.png\'\n    background_down: "/home/iam/Pictures/AppImages/Colors/blue.png"       # \'Colour_blue.png\'\n    text_size: self.size\n    halign: \'center\'\n    valign: \'middle\'\n\n<Cust2@Button>:\n    font_size: 32\n    background_normal: "/home/iam/Pictures/AppImages/Colors/red.png"   # \'Colour_red.png\'\n    background_down: "/home/iam/Pictures/AppImages/Colors/blue.png"     # \'Colour_blue.png\'\n\n<Cust3@Button>:\n    font_size: 32\n    background_normal: "/home/iam/Pictures/AppImages/Colors/white.png"     # \'Colour_white.png\'\n    background_down: "/home/iam/Pictures/AppImages/Colors/blue.png"       # \'Colour_blue.png\'\n\n<Cust4@Button>:\n    font_size: 32\n    background_normal: "/home/iam/Pictures/AppImages/Colors/blue.png"     # \'Colour_blue.png\'\n    background_down: "/home/iam/Pictures/AppImages/Colors/white.png"       # \'Colour_white.png\'\n\n<CalcGridLayout>:\n    id: calculator\n    display: entry\n    rows: 5\n    padding: 10\n    spacing: 10\n\n    BoxLayout:\n        spacing: 100\n        size_hint: .5, None\n        Cust2:\n            text: "Whats the intensity you want?"\n\n    BoxLayout:\n        size_hint: .5, None\n        TextInput:\n            id: entry\n            font_size: 70\n            multiline: False\n            hint_text: "Type here"\n\n    BoxLayout:\n        spacing: 100\n        size_hint: .5, None\n        Cust4:\n            text: "Drag and Drop picture below:"\n\n    BoxLayout:\n        Image:\n            id: img\n\n    BoxLayout:\n        size_hint: 1, .3\n        spacing: 10\n\n        CustButton:\n            text: "Click here for \\n reduced size"\n\n        CustButton:\n            text: "Click here for pos \\n  and intensity of \\n      each pixel"\n            on_release: root.reduced_image()\n\n        CustButton:\n            text: "Click here \\n for graph"\n\n        CustButton:\n            text: "Click here \\n     for all"\n\n        CustButton:\n            text: "Extra"\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出

\n\n

图像01\n图像02

\n