basic wxpython help

Mike Driscoll kyosohma at gmail.com
Sat Feb 16 12:52:05 EST 2008


On Feb 16, 10:04 am, Vamp4L <vampir... at gmail.com> wrote:
> Hello,
>   I'm brand new to wxpython, trying to implement some code to add rows
> in the GUI dynamically.  What I want to do is when you click on the
> Add Row button, a new row gets added after the first row, before the
> button.  This code adds a row in the right place, but it overlaps the
> button because it doesn't shift the button down.  Here's what I have
> so far:
>
> class TestPanel(wx.Panel):
>     def __init__(self, parent, log):
>         self.log = log
>         wx.Panel.__init__(self, parent, -1)
>
>         fgs = self.fgs = wx.FlexGridSizer(cols=1, hgap=10, vgap=10)
>
>         fgs.Add(wx.StaticText(self, -1, "Label 1"))
>         a1a = self.a1a = wx.FlexGridSizer(cols=5, hgap=10, vgap=10)
>         fgs.Add(a1a)
>         a1a.Add(wx.StaticText(self, -1, "User:"))
>         cc = wx.Choice(self, -1, choices=["A", "B"])
>         a1a.Add(cc)
>
>         cc = FileSelectorCombo(self, size=(250, -1))
>         a1a.Add((10,10))
>         a1a.Add(wx.StaticText(self, -1, "Path:"))
>         a1a.Add(cc)
>
>         b = wx.Button(self, -1, "Add Row", (50,50))
>         self.Bind(wx.EVT_BUTTON, self.OnButtonIE7, b)
>         fgs.Add(b)
>
>         box = wx.BoxSizer()
>         box.Add(fgs, 1, wx.EXPAND|wx.ALL, 20)
>         self.SetSizer(box)
>
>         a1 = wx.FlexGridSizer(cols=5, hgap=10, vgap=10)
>         fgs.Add(a1)
>         b = wx.Button(self, -1, "Run Report", (50,50))
>         self.Bind(wx.EVT_BUTTON, self.OnButtonRun, b)
>         a1.Add(b)
>
>         b = wx.Button(self, -1, "Close", (50,50))
>         self.Bind(wx.EVT_BUTTON, self.OnButtonClose, b)
>         a1.Add(b)
>
>     def OnButtonIE7(self, evt):
>         self.a1a.Add(wx.StaticText(self, -1, "Label 1"))
>         self.a1a.Layout()
>
>     def OnButtonRun(self, evt):
>         wx.StaticText(self, -1, "User:")
>
>     def OnButtonClose(self, evt):
>         dlg = wx.MessageDialog(self, "Are you sure you want to exit?",
> "Exit", wx.YES_NO | wx.ICON_QUESTION)
>         if dlg.ShowModal() == wx.ID_YES:
>             self.Destroy() # frame
>             sys.exit(1)
>         dlg.Destroy()
>
> Any suggestions?
> Thanks!

Welcome to wxPython. It's a lot of fun! Your problem stems from
calling the Layout() method on the sizer object. You need to call it
on the panel object instead. So just change "self.a1a.Layout()" to
"self.Layout()" and it should work.

Also, I would recommend closing your application differently. Since
the parent of this application is likely to be a frame, put a property
at the beginning of your __init__ that reads something this this:

self.frame = parent

Then change your close event handler to this:

<code>

def OnButtonClose(self, evt):
        dlg = wx.MessageDialog(self, "Are you sure you want to exit?",
"Exit", wx.YES_NO | wx.ICON_QUESTION)
        if dlg.ShowModal() == wx.ID_YES:
            self.frame.Close()
        dlg.Destroy()

</code>

Now you won't need to import the sys module and this is the standard
way to close a frame in most cases. Finally, I recommend that you join
the wxPython list as you'll learn a lot from them and receive good
advice there...probably faster than here. Their list can be found
here: http://wxpython.org/maillist.php

HTH

Mike



More information about the Python-list mailing list