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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Salerno

    #1

    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.Fr ame):

    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.SetTopWind ow(frame)
    frame.Show()
    return True


    app = MyApp(redirect= False)
    app.MainLoop()
  • jean-michel bain-cornu

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

    Hi John
    John Salerno a écrit :[color=blue]
    > 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.[/color]

    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(se lf,wx.ID_ANY,"e tc...")

    Regards,
    jm

    Comment

    • Scott David Daniels

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

      John Salerno wrote:[color=blue]
      > 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.Fr ame):
      > 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.SetTopWind ow(frame)
      > frame.Show()
      > return True
      >
      > app = MyApp(redirect= False)
      > app.MainLoop()[/color]

      import wx
      __version__ = '0.0'

      class InputForm(wx.Fr ame):
      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.Bu tton(panel, label='OK'), 0, wx.ALL, 10)
      sizer.Add(wx.Bu tton(panel, label='Cancel') , 0, wx.ALL, 10)

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

      if __name__ == '__main__':
      MyApp(redirect= False).MainLoop ()


      --Scott David Daniels
      scott.daniels@a cm.org

      Comment

      • John Salerno

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

        Scott David Daniels wrote:

        [color=blue]
        > class InputForm(wx.Fr ame):
        > 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__))[/color]
        [color=blue]
        > class MyApp(wx.App):
        > def OnInit(self):
        > frame = InputForm(title ='Data Entry Form')
        > self.SetTopWind ow(frame)
        > frame.Show()
        > return True[/color]

        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?

        Comment

        • John Salerno

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

          Scott David Daniels wrote:
          [color=blue]
          > class InputForm(wx.Fr ame):
          > def __init__(self, parent=None, id=-1, title=__file__) :[/color]

          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?

          Comment

          • John Salerno

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

            Scott David Daniels wrote:
            [color=blue]
            > def __init__(self, parent=None, id=-1, title=__file__) :[/color]

            I get that __file__ is not defined.

            Comment

            • Scott David Daniels

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

              John Salerno wrote:[color=blue]
              > Scott David Daniels wrote:
              >[color=green]
              >> class InputForm(wx.Fr ame):
              >> def __init__(self, parent=None, id=-1, title=__file__) :[/color]
              >
              > 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?[/color]
              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.Fr ame):
              def __init__(self, parent=None, id=-1, title='Input Form"):

              --Scott David Daniels
              scott.daniels@a cm.org

              Comment

              • Frank Niessink

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

                Scott David Daniels:[color=blue]
                > John Salerno wrote:[color=green]
                >> 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.Fr ame):
                >> def __init__(self, parent, id, title):
                >> wx.Frame.__init __(self, parent, id, title)[/color][/color]

                I usually do it like this:

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

                Cheers, Frank

                Comment

                Working...