gpt4 book ai didi

python - kivy python3检测鼠标滚轮

转载 作者:行者123 更新时间:2023-12-01 01:58:52 26 4
gpt4 key购买 nike

您好,我想在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com