kivy python3检测鼠标滚轮

2024-02-09

你好,我想在 kivy 的图表中创建缩放效果(我在 Windows 上的 python 3.6 64 位中使用 kivy 1.10)

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

我的代码:

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 https://github.com/kivy-garden/garden.graph/blob/master/__init__.py用于使用 kivy 创建图表,但使用此代码我无法放大图表。

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


你必须实现 on_touch_down 事件,并检查是否有滚动以及它是什么类型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")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

kivy python3检测鼠标滚轮 的相关文章

随机推荐