ScatterLayout-do_translation不起作用 [英] ScatterLayout - do_translation does not work

查看:61
本文介绍了ScatterLayout-do_translation不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码

import kivy
from kivy.uix.scatterlayout import ScatterLayout
from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button

class MyScatter(ScatterLayout):

    def __init__(self, *args, **kwargs):
        super(MyScatter, self).__init__(*args, **kwargs)
        self.img = Image(source='img.png', keep_ratio=True, center = self.center)
        self.add_widget(self.img)

class MainApp(App):
    def build(self):
        mainbox = FloatLayout()
        mainbox.add_widget(Button(text="Prev",
                                  font_size="17dp",
                                  size_hint=(.15, .15),
                                  pos_hint={"left":1,
                                            "center_y":0.5},
                                  ))
        ms = MyScatter(scale=1, pos_hint={"center_x":0.33, "center_y":0.5}, do_scale = True, do_rotation = False, do_translation = True)
        mainbox.add_widget(ms)
        return mainbox

root = MainApp()
root.run()

按钮和图像的位置根据我的需要.但是,我为MyScatter定义了do_translation = True,它不起作用.

The positions of button and image is according to my needs. But, I defined do_translation = True for MyScatter and it does not work.

推荐答案

我启发了

I inspired here and written this code:

import kivy
from kivy.uix.scatterlayout import ScatterLayout
from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button

class MyScatter(ScatterLayout):

    def __init__(self, *args, **kwargs):
        super(MyScatter, self).__init__(*args, **kwargs)
        self.img = Image(source='img.png', keep_ratio=True, pos_hint={"center_x":0.33, "center_y":0.5})
        self.add_widget(self.img)

    def on_touch_move(self, touch): #magic time!!!!
       res =  super(MyScatter, self).on_touch_move(touch)
       if res: #Yay do something!
           self.img.pos = (self.center_x, self.center_y)
       return res

class MainApp(App):
    def build(self):
        mainbox = FloatLayout()
        mainbox.add_widget(Button(text="Prev",
                                  font_size="17dp",
                                  size_hint=(.15, .15),
                                  pos_hint={"left":1,
                                            "center_y":0.5},
                                  ))
        ms = MyScatter(scale=1, do_scale = True, do_rotation = False, do_translation = True)
        mainbox.add_widget(ms)
        return mainbox

root = MainApp()

root.run()

似乎可行.请让我知道这是否是很好的解决方案.

It seems to work. Please, let me know if it is good solution.

这篇关于ScatterLayout-do_translation不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆