467,906 Members | 1,734 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,906 developers. It's quick & easy.

wxpython: how do i write this without the id parameter?

I was reading in the wxPython wiki that most of the time you don't have
to include the id parameter at all, and you can just use keyword
arguments for other parameters. But I'm having trouble converting this
code into that method (i.e., without the id parameter). I keep getting
errors that involve wrong parameters, or that they are out of order,
etc. So I'm hoping someone can show me how to re-write the constructors
for InputForm and wx.Frame, as well as the __init__ method, so that they
will just deal with parent and title.

Also, the two buttons have an id parameter, but leaving them out doesn't
seem to create any problems.

Thanks.

-----------

import wx
class InputForm(wx.Frame):

def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)

panel = wx.Panel(self)
self.btnOK = wx.Button(panel, label='OK')
self.btnCancel = wx.Button(panel, label='Cancel')

sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.btnOK, 0, wx.ALL, 10)
sizer.Add(self.btnCancel, 0, wx.ALL, 10)
panel.SetSizer(sizer)
class MyApp(wx.App):

def OnInit(self):
frame = InputForm(None, -1, title='Data Entry Form')
self.SetTopWindow(frame)
frame.Show()
return True
app = MyApp(redirect=False)
app.MainLoop()
Jun 12 '06 #1
  • viewed: 1548
Share:
7 Replies
Hi John
John Salerno a écrit :
I was reading in the wxPython wiki that most of the time you don't have
to include the id parameter at all, and you can just use keyword
arguments for other parameters. But I'm having trouble converting this
code into that method (i.e., without the id parameter). I keep getting
errors that involve wrong parameters, or that they are out of order,
etc. So I'm hoping someone can show me how to re-write the constructors
for InputForm and wx.Frame, as well as the __init__ method, so that they
will just deal with parent and title.


May I suggest you to use wx.ID_ANY each time you need an id.
It's a very clean way to give it even if you don't really care.
For instance :
wx.StaticBox(self,wx.ID_ANY,"etc...")

Regards,
jm
Jun 12 '06 #2
John Salerno wrote:
I was reading in the wxPython wiki that most of the time you don't have
to include the id parameter at all, and you can just use keyword
arguments for other parameters. But I'm having trouble converting this
code into that method (i.e., without the id parameter)....

import wx

class InputForm(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
panel = wx.Panel(self)
self.btnOK = wx.Button(panel, label='OK')
self.btnCancel = wx.Button(panel, label='Cancel')
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.btnOK, 0, wx.ALL, 10)
sizer.Add(self.btnCancel, 0, wx.ALL, 10)
panel.SetSizer(sizer)

class MyApp(wx.App):
def OnInit(self):
frame = InputForm(None, -1, title='Data Entry Form')
self.SetTopWindow(frame)
frame.Show()
return True

app = MyApp(redirect=False)
app.MainLoop()


import wx
__version__ = '0.0'

class InputForm(wx.Frame):
def __init__(self, parent=None, id=-1, title=__file__):
# or, if you prefer: ..., id=wx.ID_ANY, ...
wx.Frame.__init__(self, parent=parent, id=id,
title='%s v%s' % (title, __version__))
panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.HORIZONTAL)
panel.SetSizer(sizer)
sizer.Add(wx.Button(panel, label='OK'), 0, wx.ALL, 10)
sizer.Add(wx.Button(panel, label='Cancel'), 0, wx.ALL, 10)

class MyApp(wx.App):
def OnInit(self):
frame = InputForm(title='Data Entry Form')
self.SetTopWindow(frame)
frame.Show()
return True

if __name__ == '__main__':
MyApp(redirect=False).MainLoop()
--Scott David Daniels
sc***********@acm.org
Jun 13 '06 #3
Scott David Daniels wrote:

class InputForm(wx.Frame):
def __init__(self, parent=None, id=-1, title=__file__):
# or, if you prefer: ..., id=wx.ID_ANY, ...
wx.Frame.__init__(self, parent=parent, id=id,
title='%s v%s' % (title, __version__)) class MyApp(wx.App):
def OnInit(self):
frame = InputForm(title='Data Entry Form')
self.SetTopWindow(frame)
frame.Show()
return True


Thanks, but there was an example (which I can't find now) somewhere in
the wxPython wiki that showed a call to wx.Frame without the id
parameter at all, like wx.Frame(parent, title). How is that possible?

Is the issue with my code just that I'm passing the parameters around
and so I can't be as concise as that?
Jun 13 '06 #4
Scott David Daniels wrote:
class InputForm(wx.Frame):
def __init__(self, parent=None, id=-1, title=__file__):


Also, is there a way to define parent and id with defaults, but not
title? Is it good to change the order around to do this?
Jun 13 '06 #5
Scott David Daniels wrote:
def __init__(self, parent=None, id=-1, title=__file__):


I get that __file__ is not defined.
Jun 13 '06 #6
John Salerno wrote:
Scott David Daniels wrote:
class InputForm(wx.Frame):
def __init__(self, parent=None, id=-1, title=__file__):


Also, is there a way to define parent and id with defaults, but not
title? Is it good to change the order around to do this?

No, too many things know the first couple of args are parent and id.
If __file__ doesn't work for you, just stick in something for title
and remember to replace it.

class InputForm(wx.Frame):
def __init__(self, parent=None, id=-1, title='Input Form"):

--Scott David Daniels
sc***********@acm.org
Jun 14 '06 #7
Scott David Daniels:
John Salerno wrote:
I was reading in the wxPython wiki that most of the time you don't have
to include the id parameter at all, and you can just use keyword
arguments for other parameters. But I'm having trouble converting this
code into that method (i.e., without the id parameter)....

import wx

class InputForm(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)


I usually do it like this:

class InputForm(wx.Frame):
def __init__(self, *args, **kwargs):
super(InputForm, self).__init__(*args, **kwargs)
...

Cheers, Frank
Jun 17 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Tom Plunket | last post: by
15 posts views Thread by Grant Edwards | last post: by
6 posts views Thread by Batista, Facundo | last post: by
22 posts views Thread by dcrespo | last post: by
14 posts views Thread by Rod W | last post: by
12 posts views Thread by vivainio | last post: by
44 posts views Thread by bg_ie | last post: by
1 post views Thread by [david] | last post: by
16 posts views Thread by Andrea Gavana | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.