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

J

John Salerno

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()
 
J

jean-michel bain-cornu

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
 
S

Scott David Daniels

John said:
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
(e-mail address removed)
 
J

John Salerno

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?
 
J

John Salerno

Scott said:
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?
 
S

Scott David Daniels

John said:
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
(e-mail address removed)
 
F

Frank Niessink

Scott David Daniels:
I usually do it like this:

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

Cheers, Frank
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top