首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置由tkinter filechooser (图像)选择的文件作为Kivy图像小部件的源?

如何设置由tkinter filechooser (图像)选择的文件作为Kivy图像小部件的源?
EN

Stack Overflow用户
提问于 2018-07-21 17:05:23
回答 1查看 551关注 0票数 1

我目前正在学习Kivy,我正在使用tkinter filechooser浏览存储在我计算机上的图像,并希望将选定的图像设置为图像小部件的源。

下面的代码在单击按钮时启动tkinter文件回波器,如何将获得的文件设置为图像小部件的源。谢谢您抽时间见我。

main.py

代码语言:javascript
复制
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty

from tkinter.filedialog import askopenfilename
from tkinter import Tk

class SampBoxLayout(BoxLayout):
# Defines two FileChoosers.

    def get_image_one(self):
        # Select image file types, returned image should be used as source of Image widget.
        Tk().withdraw() # avoids window accompanying tkinter FileChooser
        img1 = askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
        return img1

    def get_image_two(self):
        Tk().withdraw()
        img2 = askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
        return img2

class SampleApp(App):
    def build(self):
        # Set the background color for the window
        Window.clearcolor = (1, 1, 1, 1)
        return SampBoxLayout()

sample_app = SampleApp()
sample_app.run()

sample.kv

代码语言:javascript
复制
#: import CheckBox kivy.uix.checkbox

<ColoredBackground@Image>:
    font_size: '48sp'
    color: (.9, .5, .4, 1)
    canvas.before:
        Color:
            rgb: (.9, .9, .9)
        Rectangle:
            pos: self.x + sp(2), self.y + sp(2)
            size: self.width - sp(5), self.height - sp(4)

<SampBoxLayout>:
    orientation: "vertical"
    padding: 10
    spacing: 10

    # ---------- Button FileChooser ----------
    BoxLayout:
        orientation: "horizontal"
        height: 30

        Button:
            text: "FileChooser-1"
            on_press: root.get_image_one()

        Button:
            text: "FileChooser-2"
            on_press: root.get_image_two()

    # ---------- Display Images----------
    BoxLayout:
        orientation: "horizontal"
        height: 30

        ColoredBackground:
            source: " " # <----------- should be the image file selected by FileChooser-1
            size: self.parent.width, self.parent.height
            allow_stretch: True
            keep_ratio: False

        ColoredBackground:
            source: " " # <----------- should be the image file selected by FileChooser-2
            size: self.parent.width, self.parent.height
            allow_stretch: True
            keep_ratio: False
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-21 22:33:13

解决方案是,ColoredBackground可以在get_image_oneget_image_two中访问,因为我们做的是属性:

*.kv

代码语言:javascript
复制
...
<SampBoxLayout>:
    first_image: first_image     # <----
    second_image: second_image   # <----
    orientation: "vertical"
    padding: 10
    spacing: 10

    # ---------- Button FileChooser ----------
    BoxLayout:
        ...

    # ---------- Display Images----------
    BoxLayout:
        orientation: "horizontal"
        height: 30

        ColoredBackground:
            id: first_image  # <----
            size: self.parent.width, self.parent.height
            allow_stretch: True
            keep_ratio: False

        ColoredBackground:
            id: second_image  # <----
            size: self.parent.width, self.parent.height
            allow_stretch: True
            keep_ratio: False

*.py

代码语言:javascript
复制
def get_image_one(self):
    # Select image file types, returned image should be used as source of Image widget.
    Tk().withdraw() # avoids window accompanying tkinter FileChooser
    img = askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
    self.first_image.source = img

def get_image_two(self):
    Tk().withdraw()
    img = askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
    self.first_image.source = img
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51458338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档