首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ScatterLayout - do_translation不工作

ScatterLayout - do_translation不工作
EN

Stack Overflow用户
提问于 2020-11-02 19:04:13
回答 1查看 44关注 0票数 0

我的代码

代码语言:javascript
复制
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()

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-02 20:24:39

我启发了here并编写了以下代码:

代码语言:javascript
复制
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()

好像很管用。请告诉我这是个好办法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64651740

复制
相关文章

相似问题

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