Python: Decide which scrollbar is moving in Scroll

2019-08-02 12:08发布

问题:

This question continue the question on the link : wxPython Event for ScrolledPanel. I edited the code a little bit so that OnScroll function get the x position of the content's scrollbar and use that to set x position of the header's scrollbar.

My problem is I cannot decide when the horizontal scrollbar is rolled. So my program now has the header's horizontal scrollbar rolled whenever any of the content's scrollbars is rolled while I want the header's h-scrollbar rolled when the content's h-scrollbar is rolled. (Or if anyone can suggest an event triggering by only when scrolling the horizontal bar it will be very good). Thanks ahead.

import wx
from wx.lib.scrolledpanel import ScrolledPanel
header = """
col1        col2        col3        col4        col5"""
text =   """
1336        733         1336        4732        1217

5968        4477        1217        5748        4477

1217        5635        4372        1217        5634

4369        1217        5633        4371        217"""

class Test(wx.Frame):    
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title, size=(300, 200))
        self.panel=panel = wx.Panel(self, -1)
        vbox = wx.BoxSizer(wx.VERTICAL)
        vboxA = wx.BoxSizer(wx.VERTICAL)

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        self.headerPanel = headerPanel = ScrolledPanel(panel, -1, size=(150,32))
        hboxHeader = wx.BoxSizer(wx.HORIZONTAL)
        self.headertc = headertc = wx.TextCtrl(headerPanel, -1, header,
                             size=(500,32),style= wx.TE_READONLY)
        headertc.Unbind(wx.EVT_SCROLLWIN)

        hboxHeader.Add(headertc,1)
        headerPanel.SetSizer(hboxHeader)
        headerPanel.SetAutoLayout(1)
        headerPanel.SetupScrolling(scroll_y = False)
        hbox.Add(headerPanel,1, wx.EXPAND |  wx.ALL,0)

        vboxA.Add(hbox, 0, wx.EXPAND)

        self.textPanel = textPanel = ScrolledPanel(panel, -1, size = (150,150))
        textPanel.Bind(wx.EVT_SCROLLWIN, self.OnScroll)

        hboxText = wx.BoxSizer(wx.HORIZONTAL)

        self.tc = tc = wx.TextCtrl(textPanel, -1, text, size=(500,500),
                style=wx.TE_MULTILINE|wx.TE_DONTWRAP| wx.TE_READONLY)
        hboxText.Add(tc, 1)
        textPanel.SetSizer(hboxText)
        textPanel.SetAutoLayout(1)
        textPanel.SetupScrolling(scroll_x=True, scroll_y=True)

        vboxA.Add(textPanel,1, wx.EXPAND | wx.ALL,0)

        vbox.Add(vboxA, 1, wx.EXPAND | wx.ALL)
        panel.SetSizer(vbox)
        self.Centre()
        self.Show(True)  


    def OnScroll(self, event):
        event.Skip()
        x= event.GetPosition()
        self.headerPanel.Scroll(x,0)
        print event.GetEventType()



if __name__ == "__main__":
    app = wx.App()
    frame = wx.Frame(None, -1)
    win = Test(frame, "Test scroll bar")
    app.MainLoop()         

回答1:

Use the following condition to check for horizontal scrolling in your scroll handler.

if event.Orientation == wx.SB_HORIZONTAL: