Die*_*rez 2 python bind image kivy
我试图通过做有趣的事情来学习 kivy,但有点难以掌握 kivy 做事的方式。
在 Tkinter 中,我使用 forloop 创建了缩略图库并将每个单独的图像绑定到回调,它只是将单击图像的信息(路径)传递给回调以打开图像。但是我似乎可以理解如何在 kivy 中做这么简单的事情,所以我需要一些帮助。
使用 Button 小部件有效;我尝试创建一个带有按钮的画廊并将它们的背景更改为图像,但图像被拉伸和扭曲(不是我想要的)。
所以我用图像小部件制作了缩略图库,拇指显示只是找到,但我找不到一种方法将点击的拇指信息传递给每个拇指的回调(回调事件),以按预期的方式工作。
我将每个拇指与 on_touch_down 属性绑定,但是当执行回调时,所有拇指信息都通过一键单击传递给回调,这不是我想要的,我只想将单击的单个拇指的信息传递给打回来。我阅读了 kivy 文档,但越来越困惑。任何方式这里都是我的基本代码,任何帮助将不胜感激,非常感谢。
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()
Run Code Online (Sandbox Code Playgroud)
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)
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2325 次 |
| 最近记录: |