1

I want to ask is it possible to add wx.Panel with event button in wxpython? There are plenty examples how to switch panels Hide first one and show second, but they are useless for me. I want to create panel with add button. For example I have panel something like this

import wx
import wx.grid as grid

class MainPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent = parent)


class SecondPanel(wx.Panel):
    def __init__(self, parent,a,b):
        wx.Panel.__init__(self, parent=parent)
        MyGrid=grid.Grid(self)
        MyGrid.CreateGrid(a, b)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(MyGrid, 0, wx.EXPAND)
        self.SetSizer(sizer)

class MainFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="test",
                          size=(800,600))
        self.splitter = wx.SplitterWindow(self)

        self.panelOne = MainPanel(self.splitter)


        self.panelTwo = SecondPanel(self.splitter, 1, 1)
        txtOne = wx.StaticText(self.panelOne, -1, label = "piradoba", pos = (20,10))
        self.txtTwo = wx.StaticText(self.panelOne, -1, label = "", pos = (40,80))
        self.txtPlace = wx.TextCtrl(self.panelOne, pos = (20,30))
        button = wx.Button(self.panelOne, label = "search", pos = (40,100))
        button.Bind(wx.EVT_BUTTON, self.Onbutton)
        self.splitter.SplitHorizontally(self.panelOne, self.panelTwo)
        self.splitter.SetMinimumPaneSize(20)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.splitter, 1, wx.EXPAND)
        self.SetSizer(sizer)
   def Onbutton(self, event):
        var=self.txtPlace.GetValue()
        if len(var) == 9 or len(var) == 11:
           ???????????????????????????????????????????????

if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()

for example now I want to add new panel with this event what can I do? and I want to create this panel with event.

3
  • Your informations are useless for us (so others downvotes you). What did you try to do ? Show some code. Commented Oct 29, 2013 at 10:58
  • I edited so now you can see Commented Oct 29, 2013 at 11:00
  • Where do you want to add new panel - in another panel, another splitter, etc. ? Do you have place for new panel ? Maybe try to add new panel in traditional way - in __init__ and then you can try to move that code to onbutton Commented Oct 29, 2013 at 13:03

1 Answer 1

2

I don't know if it is what you need but in this example you have:

  • panel with button and event
  • button call function in mainframe
  • mainframe add next panel (with grid) to boxsizer

Tested on Linux Mint + Python 2.7.4

import wx
import wx.grid as grid

class MainPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent = parent)

        self.txtOne = wx.StaticText(self, -1, label = "piradoba", pos = (20,10))
        self.txtPlace = wx.TextCtrl(self, pos = (20,30))
        self.txtTwo = wx.StaticText(self, -1, label = "", pos = (20,40))

        button = wx.Button(self, label = "search", pos = (20,70))
        button.Bind(wx.EVT_BUTTON, self.onButton)

    def onButton(self, event):
        var=self.txtPlace.GetValue()
        if len(var) == 9 or len(var) == 11:
            print "???"
        # MainPanel->SplitterWindow->MainFrame ( 2x GetParent() )
        self.GetParent().GetParent().AddPanel()

class SecondPanel(wx.Panel):

    def __init__(self, parent,a,b):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)

        MyGrid=grid.Grid(self)
        MyGrid.CreateGrid(a, b)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(MyGrid, 0, wx.EXPAND)
        self.SetSizer(sizer)

class MainFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="test", size=(800,600))

        self.splitter = wx.SplitterWindow(self)

        self.panelOne = MainPanel(self.splitter)
        self.panelTwo = SecondPanel(self.splitter, 1, 1)

        self.splitter.SplitHorizontally(self.panelOne, self.panelTwo)
        self.splitter.SetMinimumPaneSize(20)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.splitter, 2, wx.EXPAND)

        self.SetSizer(self.sizer)

    def AddPanel(self):
        self.newPanel = SecondPanel(self, 1, 1)
        self.sizer.Add(self.newPanel, 1, wx.EXPAND)
        self.sizer.Layout()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    frame.Show()
    app.MainLoop()
Sign up to request clarification or add additional context in comments.

1 Comment

I like your example. I also solved that problem yesterday, it was easier than i expected. But anyway I'll accept your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.