Kivy:滚动缩放 [英] Kivy: scroll to zoom

查看:165
本文介绍了Kivy:滚动缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以放大桌面kivy应用程序上的图像(例如,使用鼠标滚轮进行缩放)?它似乎在这里讨论: https://github.com/kivy/kivy/issues/3563 ,但我看不出有没有解决的办法.

Is there a way to zoom into an image on a desktop kivy app (e.g. zoom with a mouse scroll-wheel)? It appears to be discussed here: https://github.com/kivy/kivy/issues/3563 but I do not see if there was a work around given.

我从我的kivy应用程序中开始使用静态图片.我想添加缩放/平移图像的功能.我不希望图像框的实际大小发生变化,仅添加缩放/平移功能即可,例如您可能希望与Google地图进行交互.

I began with a static image in my kivy app. I want to add the ability to zoom / pan into the image. I do not want the actual size of the image frame to change, just add zoom/pan functionality, like you might expect from interacting with google maps for example.

可能的编程说明

从我所读的内容中,我应该使用Scatter(?),并且看到可以手动设置Scatter比例来放大/缩小图像.

From what I've read, I should be using Scatter(?), and I see that I can manually set the Scatter scale to size up/down the image.

我最初的想法是,我将不得不添加一个单独的带有滚动视图的小部件以包含分散的布局,并且这将使图像框架保持一致的大小.然后,我需要添加一个可动态更改比例值的事件.

My initial thoughts are that I will have to add a separate widget with a scrollview to contain the scatter layout and that will keep the image frame a consistent size. Then I will need to add an event that dynamically changes the scale value.

问题

  • 我找不到用于更改比例值的适当事件. on_motion事件似乎很有希望.我的应用程序可以使用滑块更新值,但是当我尝试使用on_motion,的类似方法时,会出现AttributeError: motion错误
  • 我正在努力创建小部件.大多数文档似乎在python文件中使用.add_widget(...).是否可以从kv文件中执行此操作?我想这个过程类似于屏幕和屏幕管理器,但是我正在努力寻找一种可行的方法.

  • I cannot find the appropriate event to use to change the scale value. the on_motion event seemed promising. My app can update values with a slider, but when I try a similar approach using on_motion, I get AttributeError: motion errors
  • I am struggling to create the widget. Most documentation seems to use .add_widget(...) in the python file. Is it possible to do this from the kv file? I imagine this process is similar to screens and the screen manager, but I am struggling to find an approach that works.

是否有更直接的方法来做到这一点?

有没有一种方法可以在我的kv文件中使用on_motion类型的事件来使用鼠标滚轮调整此值?

Is there a way I can use on_motion type event in my kv file to adjust this value using the mouse scroll-wheel?

我给出了一个简单的示例,展示了我的kivy应用程序的结构-以及我为添加Scatter所做的尝试.我想我需要将其放入自己的小部件中,以保持图像大小相同?

I give a watered down example of the structure of my kivy app - along with what I tried to do to add Scatter. I think I will need to put it into it's own widget to keep the image the same size?

玩具示例

import kivy
from kivy.lang import Builder
from kivy.core.window import Window


kivy.require('1.1.0')

from kivy.app import App

presentation = Builder.load_file("scatter.kv")
class TestApp(App):
    def build(self):
        Window.clearcolor = (1, 1, 1, 1)
        return presentation

    # def foo():
    #    print("You've reached foo")    

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

#:kivy 1.10.0
GridLayout:
    cols: 2

    Scatter:
        scale: 5
        # on_motion: root.foo()
        Image :
            source: 'foo.png'
            allow_stretch: True
            keep_ratio: True

    Button:
        text: 'Hello World'

产生:

相关种类:

  • Scatter Covers whole display kivy (Unanswered)
  • https://github.com/kivy/kivy/issues/3563

推荐答案

为实现我的目标,我使用了 https://github.com/kivy/kivy/wiki/Draggable-Scalable-Button

To achieve my goal, I used a combination of the information in kivy python3 detect mousewheel, as pointed out ikolim, and the code given here: https://github.com/kivy/kivy/wiki/Draggable-Scalable-Button

为使我的回答简短,重要的代码归结为:

To keep my answer brief, the important code boiled down to this:

    if touch.is_mouse_scrolling:
        if touch.button == 'scrolldown':
            print('down')
            ## zoom in
            if self.scale < 10:
                self.scale = self.scale * 1.1

        elif touch.button == 'scrollup':
            ## zoom out
            print('up')
            if self.scale > 1:
                self.scale = self.scale * 0.8

布局略有不同,我更改了按钮上的文本,但是代码的功能可以在以下gif中看到:

The layout is slightly different and I changed the text on the button, but the functionality of my code can be seen in the following gif:

对于任何希望看到我的整个玩具项目以适应他们自己的目的的人,整个代码都在我的github上:

For anybody wanting to see my entire toy project to adapt for their own purposes, the entire code is on my github: https://github.com/melissadale/Learning-Kivy/tree/master/ZoomPanning

这篇关于Kivy:滚动缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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