HuongOrchid
HuongOrchid

Reputation: 332

wxPython Event for ScrolledPanel

I want to make 2 TextCtrls, one is the header, one is the content in which when I scroll vertically, only the content is rolled, but when I scroll horizontally , both header and content are rolled. So I intended to put each of them in a separate ScrolledPanel. When I scroll content TextCtrls, the wx.EVT_SCROLLWIN is trigger and call OnScroll function. In this function I will get the x position of the content TextCtrl and set it to header TextCtrl.

However I've got problem when trying to use wx.EVT_SCROLLWIN. This is my code:

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=(200, 200))
        self.panel=panel = wx.Panel(self, -1)
        vbox = wx.BoxSizer(wx.VERTICAL)
        vboxA = wx.BoxSizer(wx.VERTICAL)

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

        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)

        textPanel = ScrolledPanel(panel, -1, size = (150,150))

        #self.Bind(wx.EVT_SCROLLWIN, self.OnScroll, id=textPanel.GetId())
        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):
        print "OnScroll"


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

If I use code line 38: OnScroll is called, but the content TextCtrl is not moved. If I use code line 37 (or not use either 37 or 38) the content TextCtrl is moved but OnScroll is not called. Can anyone help me to find what's wrong here?

Upvotes: 0

Views: 862

Answers (1)

Yoriz
Yoriz

Reputation: 3625

Unless you call event.Skip() in the EVT_SCROLLWIN handler the default event action will not happen, if you don't want to call event.Skip() you will need to handle the scrolling yourself.

Upvotes: 2

Related Questions