How to trigger an action once on overscroll in Kivy?

2024/10/15 4:18:30

I have a ScrollView that's supposed to have an update feature when you overscroll to the top (like in many apps). I've found a way to trigger it when the overscroll exceeds a certain threshold, but it triggers it a lot of times, as the on_overscroll event is triggered on every movement. So is there a way to limit it?
My code looks like this:

from kivy.app import App
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Buttonfrom kivy.effects.dampedscroll import DampedScrollEffectclass Effect(DampedScrollEffect):def on_overscroll(self, *args):super().on_overscroll(*args)if self.overscroll < -50:print('hey')class TestApp(App):def build(self):sv = ScrollView(effect_cls = Effect,size_hint_y = 0.2)gl = GridLayout(cols = 1,size_hint_y = None)gl.bind(minimum_height = gl.setter('height'))for i in range(5):gl.add_widget(Button(text = str(i),size_hint = (None, None)))sv.add_widget(gl)return svTestApp().run()

So, as you can see, when the overscroll goes beyond 50, it prints a simple message. But when you actually try it, you'll see that it prints it many times. What I want for it is to trigger an event, stay untriggerable for some time (like a second) and update the content. I've tried messing with boolean flags and Clock, but it didn't work. What could be done here?

Answer

I would use a stateful decorator here:

class call_control:def __init__(self, max_call_interval):self._max_call_interval = max_call_intervalself._last_call = time()def __call__(self, function):def wrapped(*args, **kwargs):now = time()if now - self._last_call > self._max_call_interval:self._last_call = nowfunction(*args, **kwargs)return wrappedclass Effect(DampedScrollEffect):def on_overscroll(self, *args):super().on_overscroll(*args)if self.overscroll < -50:self.do_something()@call_control(max_call_interval=1)def do_something(self):print('hey')
https://en.xdnf.cn/q/117874.html

Related Q&A

Python - Print Each Sentence On New Line

Per the subject, Im trying to print each sentence in a string on a new line. With the current code and output shown below, whats the syntax to return "Correct Output" shown below?Codesentenc…

pyinstaller struct.error: unpack requires a bytes object of length 16 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Getting the quarter where recession start and recession ends along with the quarter of minimum gdp

Quarter: GDP: GDP change: change 1999q3 9 -- ------ 1999q4 10 1 increase 2000q1 9 -1 decline 2000q2 8 -1 de…

Inherit view and adding fields

I want to add my 2 fields boatlenght and fuelcapacity under price list in product form view but they are not showing up. What did i miss.<?xml version="1.0" encoding="utf-8"?&g…

Linux and python: Combining multiple wave files to one wave file

I am looking for a way that I can combine multiple wave files into one wave file using python and run it on linux. I dont want to use any add on other than the default shell command line and default py…

How does the in operator determine membership? [duplicate]

This question already has answers here:Set "in" operator: uses equality or identity?(5 answers)Closed 7 years ago.How does the in operator work for Python? In the example below I have two n…

Python Automatically ignore unicode string

Ive been searching to automatically import some files but since Im on Windows i got the unicode error (because of the "C:\Users\..."). Ive been looking to correct this error and found some h…

How to obtain currency rates from this website converter widget python

How can I implement the currency rates on this website and keep the currencies up to date so that i can access them in python from this website and input and output values and currencies types. I need …

Trying to add sums from a csv file in python

I need to add sums of a csv file. The program is a test for a travel reservation system and the file reads like this:availableSTART,reservations,cancellations,availableEND 20,1,0,18I need to subtract r…

Numerical patterns in Python3 [duplicate]

This question already has answers here:How to print without a newline or space(30 answers)Closed 7 years ago.Im fairly new to programming, i have to start learning it for Uni.I have to make a pattern a…