kivy python3检测鼠标滚轮 [英] kivy python3 detect mousewheel

查看:240
本文介绍了kivy python3检测鼠标滚轮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想在kivy的图形图中创建缩放效果(我在Windows的python 3.6 64bits中使用kivy 1.10)

Hello I want to create zoom effect in graph plot in kivy (i use kivy 1.10 in python 3.6 64bits on windows)

我想在我的图形小部件中检测鼠标滚轮事件,但是找不到如何执行此操作.

I want to detect mousewheel event in my graph widget, but I couldn't find how to do this.

我的代码:

import itertools
from math import sin, cos, pi
from random import randrange
from kivy.utils import get_color_from_hex as rgb
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
from graph import Graph,MeshLinePlot

from kivy.uix.popup import Popup
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.stacklayout import StackLayout




class Visu(GridLayout):
    def __init__(self, **kwargs):
        super(Visu, self).__init__(**kwargs)
        self.cols = 2
        self.row = 2

        b = BoxLayout(orientation='vertical', on_press=self.zoom)


        graph = Graph(xlabel='X', ylabel='Y', x_ticks_minor=5,
        x_ticks_major=25, y_ticks_major=1,
        y_grid_label=True, x_grid_label=True, padding=5,
        x_grid=True, y_grid=True, xmin=-0, xmax=50, ymin=-1, ymax=1)


        #graph.add_x_axis(0,10)

        plot1 = MeshLinePlot(color=[1, 0, 0, 1])
        plot1.points = [(x, sin(x / 10.)) for x in range(0, 65000)]
        graph.add_plot(plot1)
        plot2 = MeshLinePlot(color=[1, 0, 0, 1])
        plot2.points = [(x, sin(x / 10.)) for x in range(65000, 120000)]
        graph.add_plot(plot2)

        b.add_widget(graph)
        graph.xmax=1000
        graph.xmax=40

        self.add_widget(b)

    def zoom(self):
        print("if mousewheel i change graph.xmin and graph.xmax")



class MyApp(App):
    def build(self):
        return Visu()


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

我使用此代码 https://github.com/kivy-garden/garden. graph/blob/master/ init .py 用于使用kivy创建图形,但是使用此代码,我无法放大图形.

I use this code https://github.com/kivy-garden/garden.graph/blob/master/init.py for creating the graph with kivy, but with this code I can't zoom in my graph.

我想检测鼠标滚轮并运行我的函数self.zoom

I want to detect mousewheel and run my function self.zoom

推荐答案

您必须实现on_touch_down事件,并通过is_mouse_scrollingbutton检查是否有滚动以及滚动的类型.

You have to implement the on_touch_down event, and check if there is a scroll and what type it is through is_mouse_scrolling and button.

class Visu(GridLayout):
    def __init__(self, **kwargs):
        ...

    def on_touch_down(self, touch):
        if touch.is_mouse_scrolling:
            if touch.button == 'scrolldown':
                print('down')
            elif touch.button == 'scrollup':
                print('up')
         GridLayout.on_touch_down(self, touch)

    def zoom(self):
        print("if mousewheel i change graph.xmin and graph.xmax")

这篇关于kivy python3检测鼠标滚轮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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