basic wxpython help

V

Vamp4L

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!
 
M

Mike Driscoll

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
 
V

Vamp4L

Thanks Mike,
Simple enough! I was wandering about the close method too, I had to
hack that together from what I knew about python already. I'll be
sure to join that mailing list.
 
M

Mike Driscoll

Thanks Mike,
Simple enough! I was wandering about the close method too, I had to
hack that together from what I knew about python already. I'll be
sure to join that mailing list.

If you get the chance, check out the "wxPython in Action" book by
Robin Dunn. It's a good reference, although it doesn't have some of
the most recent changes, including the widget browser thing.

As for the mailing list, Dunn responds to a lot of questions there.
He's the creator of wxPython, so he's extremely knowledgeable. There
are also a few wxPython developers on the list that have creates their
own widgets and they tend to answer a lot of general and specific
questions there too.

I still recommend this list for non-wxPython questions though.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top