wx.Python: Passing control between multiple panels

后端 未结 2 1903
太阳男子
太阳男子 2021-01-28 22:42

I\'m a newbie to wxPython, and have researched similar questions, but can\'t specifically find an answer to my question. I\'m creating two panels with a splitter. Each panel has

2条回答
  •  清歌不尽
    2021-01-28 23:09

    In your code you have

    def on_button_pressed(Panel,event):
    
        RightPanel().SetBackgroundColour("light blue")
    

    In the definition 'Panel' should be 'self' as 'on_button_pressed' is a instance method

    Then you are creating a new RightPanel instead of acces the already created instance.

    I moved the bind to the parent frame so it can call methods on the other child panel. See the modified code below.

    import wx
    import wx.grid as gridlib
    # import  pyodbc
    
    
    class RightPanel(wx.Panel):
        """"""
    
        def __init__(self, parent):
            """Constructor"""
            wx.Panel.__init__(self, parent=parent)
    
            grid = gridlib.Grid(self)
            grid.CreateGrid(5, 5)
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(grid, 0, wx.EXPAND)
            self.SetSizer(sizer)
    
    
    class LeftPanel(wx.Panel):
        """"""
    
        def __init__(self, parent):
            """Constructor"""
            wx.Panel.__init__(self, parent=parent)
    
            self.create_controls()
            self.SetBackgroundColour("light green")
    
        def create_controls(self):
    
            self.h_sizer = wx.BoxSizer(wx.HORIZONTAL)
            self.v_sizer = wx.BoxSizer(wx.VERTICAL)
    
            self.button = wx.Button(self, label="Press me!")
    
            self.v_sizer.Add(self.button, 0)
    
            self.v_sizer.Add(self.h_sizer, 0, wx.EXPAND)
            self.SetSizer(self.v_sizer)
    
    
    class MyForm(wx.Frame):
    
        def __init__(self):
            wx.Frame.__init__(self, None, wx.ID_ANY, "DB Viewer", size=(350, 250))
    
            splitter = wx.SplitterWindow(self)
            leftP = LeftPanel(splitter)
            self.rightP = RightPanel(splitter)
    
            splitter.SplitVertically(leftP, self.rightP)
            splitter.SetMinimumPaneSize(20)
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(splitter, 1, wx.EXPAND)
            self.SetSizer(sizer)
    
            leftP.button.Bind(wx.EVT_BUTTON, self.on_button_pressed)
    
            self.Layout()
    
        def on_button_pressed(self, event):
                self.rightP.SetBackgroundColour("light blue")
                self.Refresh()
    
    if __name__ == "__main__":
        app = wx.App(False)
        frame = MyForm()
        frame.Show()
        app.MainLoop()
    

提交回复
热议问题