woopwoop399
woopwoop399

Reputation: 133

How to disable smooth scrolling in wxPython?

When I scroll with my mousewheel, it draws many intermediate steps, and I want it to only draw the immediate destination. It's a bit laggy otherwise.

I tried intercepting wx.EVT_MOUSEWHEEL with event.StopPropagation() and doing .Scroll() manually, but it still scrolls by itself, StopPropagation() doesn't work.

The version of wxPython is 2.9. The widget is wx.ScrolledWindow.

Upvotes: 1

Views: 168

Answers (2)

devtk
devtk

Reputation: 2179

In Python v3.11, within a TextCtrl, this can be done by capturing and handling the MouseWheel event. No doubt things have changed since v2.9.

The following code will scroll the appropriate number of lines according to the user's settings in Windows, either a full page or X number of lines.

def event_text_on_mouse_wheel(self, event) -> None:
    control = event.EventObject
    if event.GetWheelAxis() != 0 or not isinstance(control, wx.TextCtrl):
        event.Skip()
        return
    control.Freeze()
    wheel_count = -round(event.GetWheelRotation() / max(event.GetWheelDelta(), 1))
    line_count = event.GetLinesPerAction()
    if line_count == -1:
        control.ScrollPages(wheel_count)
    else:
        control.ScrollLines(line_count * wheel_count)
    control.Thaw()

Upvotes: 0

Rolf of Saxony
Rolf of Saxony

Reputation: 22448

You can use the following to adjust the size of a scroll event:

SetScrollRate(self, xstep, ystep)
Set the horizontal and vertical scrolling increment only.

See the pixelsPerUnit parameter in SetScrollbars .

Parameters:
xstep (int) – ystep (int) –

Coupled with the font size and the number of items in the window, this can of course be variable.

Note:

StopAutoScrolling(self)
Stop generating the scroll events when mouse is held outside the window

and:

StopPropagation(self)
Stop the event from propagating to its parent window.

Not stop the event itself

Upvotes: 0

Related Questions