python - kivy python3检测鼠标滚轮

标签 python kivy

您好,我想在 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 用于使用 kivy 创建图表,但使用此代码我无法放大图表。

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

最佳答案

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

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")

关于python - kivy python3检测鼠标滚轮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49896086/

相关文章:

python - 如何从 Python 中的 csv 文件创建 SQL 数据库

python - Kivy TypeError 不支持的操作数类型

kivy - 如何在 Kivy 中设置屏幕背景图片

python - 如何使用 kivy screenmanager 引用其他屏幕中的对象

python - Flask-SQLAlchemy 数据类型

python - 如何在 python 之外使用 Vowpal Wabbit 模型

python - 如何在 Kivy 中压缩 GridLayout 垂直空间?

python - 尝试向下滚动带有水平 ScrollView 的垂直 ScrollView 时,Kivy 出现问题

python - 等价于 =+ 的字符串,但将现有字符串附加到新字符串而不是添加到新字符串之前

python - XPath 选择第一个包含表的表