简体   繁体   English

如何在缩略图库中制作可点击的猕猴桃图像

[英]How to make a clickable kivy image in a thumbnail gallery

I am trying to learn kivy by doing fun stuff, but is kind of hard to grasp the kivy way of doing things. 我正在尝试通过做有趣的事情来学习猕猴桃,但是有点难以掌握猕猴桃的做事方式。

In Tkinter I created thumbnail gallery with a forloop and bind each individual image to a callback, It just passes the info (path) of the clicked image to the callback to open the image. 在Tkinter中,我创建了带有forloop的缩略图库,并将每个单独的图像绑定到回调,它只是将单击图像的信息(路径)传递给回调以打开图像。 But I can seem to understand how to do such a simple thing in kivy, so I need some help. 但是我似乎可以理解如何在猕猴桃中做这种简单的事情,所以我需要一些帮助。

Using the Button widget worked; 使用Button小部件有效; I tried creating a gallery with buttons and change their background to images but the images get stretched and distorted (Not what I want). 我尝试创建带有按钮的画廊并将其背景更改为图像,但是图像会被拉伸和扭曲(不是我想要的)。

So I made the thumbnail gallery with the Image widget and the thumbs showed just find, but I can't find a way to pass the clicked thumb info to the callback for each thumb (callback event) to work the way is supposed to. 因此,我使用“图像”小部件制作了缩略图库,并且显示的拇指刚刚找到,但是我找不到一种方法来将单击的拇指信息传递给每个拇指的回调(回调事件)以应有的方式工作。

I bind each thumb with on_touch_down property but when the call back is executed all the thumbs info is passed to the callback in one click witch is not what I want, I want only the info of the individual thumb that was clicked to be passed to the callback. 我将每个拇指与on_touch_down属性绑定,但是当执行回调时,所有拇指信息都通过单击传递给回调,这不是我想要的,我只希望将单击的单个拇指的信息传递给打回来。 I read kivy docs but get more and more confused. 我阅读了kivy文档,但越来越困惑了。 Any way here is my bare bone code, any help will be appreciated thank you so much. 无论如何,这是我的基本代码,非常感谢您的帮助。

from kivy.app import App 
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image 

import glob


class Image_Gallery(GridLayout):


    def __init__(self):
        super(Image_Gallery, self).__init__()
        images = glob.glob('C:\Users\Public\Pictures\Sample Pictures\*.jpg')  # windows 7 sample pictures dir looks great
        self.cols=3
        for img in images:
            thumb = Image(source=img)
            thumb.bind(on_touch_down=self.callback)    # I tried on_touch property but does not work with images only buttons
            self.add_widget(thumb)

    def callback(self, obj, touch):
        # This should print only the clicked image source. 
        # (but instead is printing all images sources at once)
        print obj.source                



class mainApp(App):


    def build(self):
        return Image_Gallery()


if __name__ == '__main__':
    mainApp().run()

The on_touch events dispach the event on all the widgets in your app, you must define your own Image class and redefine the on_touch method: on_touch事件会在您应用中的所有小部件上分配该事件,您必须定义自己的Image类并重新定义on_touch方法:

...
class MyImage(Image):
    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            print self.source

class Image_Gallery(GridLayout):

    def __init__(self, **kwargs):
        super(Image_Gallery, self).__init__(**kwargs)
        images = glob.glob('C:\Users\Public\Pictures\Sample Pictures\*.jpg')
        self.cols = 3
        for img in images:
            thumb = MyImage(source=img)
            self.add_widget(thumb)
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM