wxPython horizontal scrolling with shift+mouse wheel

595 Views Asked by At

The wx.lib.scrolledpanel seems to support mouse wheel vertical scrolling by default, but does not support horizontal scrolling while shift is pressed. I cannot find any way to activate it. Is it even somewhere or should I write an appropriate event handler by myself? How to do that, if so?

3

There are 3 best solutions below

0
Rolf of Saxony On

To scroll horizontally you must position the mouse over the horizontal scroll bar and then spin the mouse wheel.

import  wx
import  wx.lib.scrolledpanel as scrolled
class TestPanel(scrolled.ScrolledPanel):
    def __init__(self, parent):
        scrolled.ScrolledPanel.__init__(self, parent, -1)
        vbox = wx.BoxSizer(wx.VERTICAL)
        desc1 = wx.StaticText(self, -1, "1. These lines are really quite long and should not fit in the window requiring you to use the horizontal scroll bar or position the mouse over the scroll bar and spin the wheel\n\n")
        desc2 = wx.StaticText(self, -1, "2. These lines are really quite long and should not fit in the window requiring you to use the horizontal scroll bar or position the mouse over the scroll bar and spin the wheel\n\n")
        desc3 = wx.StaticText(self, -1, "3. These lines are really quite long and should not fit in the window requiring you to use the horizontal scroll bar or position the mouse over the scroll bar and spin the wheel\n\n")
        desc4 = wx.StaticText(self, -1, "4. These lines are really quite long and should not fit in the window requiring you to use the horizontal scroll bar or position the mouse over the scroll bar and spin the wheel\n\n")
        desc5 = wx.StaticText(self, -1, "5. These lines are really quite long and should not fit in the window requiring you to use the horizontal scroll bar or position the mouse over the scroll bar and spin the wheel\n\n")
        desc6 = wx.StaticText(self, -1, "6. These lines are really quite long and should not fit in the window requiring you to use the horizontal scroll bar or position the mouse over the scroll bar and spin the wheel\n\n")
        vbox.Add(desc1, wx.NewId(), wx.ALIGN_LEFT|wx.ALL, 5)
        vbox.Add(desc2, wx.NewId(), wx.ALIGN_LEFT|wx.ALL, 5)
        vbox.Add(desc3, wx.NewId(), wx.ALIGN_LEFT|wx.ALL, 5)
        vbox.Add(desc4, wx.NewId(), wx.ALIGN_LEFT|wx.ALL, 5)
        vbox.Add(desc5, wx.NewId(), wx.ALIGN_LEFT|wx.ALL, 5)
        vbox.Add(desc6, wx.NewId(), wx.ALIGN_LEFT|wx.ALL, 5)
        self.SetSizer(vbox)
        self.SetAutoLayout(1)
        self.SetupScrolling(scroll_x=True, scroll_y=True, rate_x=20, rate_y=20, scrollToTop=True, scrollIntoView=True)
app = wx.App(0)
frame = wx.Frame(None, wx.ID_ANY,size=(500,200))
tp = TestPanel(frame)
frame.Show()
app.MainLoop()
0
johnabro On

It seems that you can use the wx.EVT_SCROLLWIN event, and make sure it calls a simple method which sets the event's orientation to wx.HORIZONTAL (when you have pressed shift.)

sw = wx.ScrolledWindow(p, style = wx.HSCROLL)
def onScroll(event):
 event.SetOrientation(wx.HORIZONTAL)
 event.Skip()
sw.Bind(wx.EVT_SCROLLWIN, onScroll)

This will make the scroll window scroll in the horizontal direction when you scroll "in it" with the mouse wheel. Here's a quick code you can paste in the console.

app = wx.App()
f = wx.Frame(None)
p = wx.Panel(f)
sw = wx.ScrolledWindow(p)
sw.SetScrollbars(20,20,500,500)
bb = wx.Button(sw, label='big button', pos=(0,0), size=(500,500))
def onScroll(event):
 event.SetOrientation(wx.HORIZONTAL)
 event.Skip()

sw.Bind(wx.EVT_SCROLLWIN, onScroll)
sz = wx.BoxSizer(wx.HORIZONTAL)
sz.Add(sw, 1, wx.EXPAND)
p.SetSizer(sz)
sz.Fit(f)
f.Show(); app.MainLoop()
0
James Brock On

This is a better answer in 2022.

Use Bind(wx.EVT_MOUSEWHEEL) and then in the handler for the event:wx.MouseEvent do

if event.shiftDown:
    event.SetWheelAxis(wx.MOUSE_WHEEL_HORIZONTAL)
event.Skip()