Properly invoking __main__

The main frame is defined as:

class MainFrame(wx.Frame):
     def __init__(self, parent, title):
         wx.Frame.__init__(self, parent, id=wx.ID_ANY, title='openEDMS',
                  size=wx.Size(700,400), style=wx.DEFAULT_FRAME_STYLE, name='main_frame')

   At the bottom of the module is:

if __name__ == '__main__':
     edms = wx.App()
     top = main_frame('openEDMS')
     top = Show()
     edms.MainLoop()

and Python3 tells me there's a error on the first 'top = ' line:

Traceback (most recent call last):
   File "./openEDMS.py", line 297, in <module>
     top = main_frame('openEDMS')
NameError: name 'main_frame' is not defined

   When I read about wx.Frame I see that the name parameter is the internal
reference name for the class so I thought the above was correct. When I
replace the name with the class name I get a different error:

Traceback (most recent call last):
   File "./openEDMS.py", line 296, in <module>
     top = MainFrame('openEDMS')
TypeError: __init__() missing 1 required positional argument: 'title'

   I've looked at the 4.0.1 demo, a couple of tutorials, and the 4.0.2a API
at <https://wxpython.org/Phoenix/docs/html/wx.Frame.html> yet still do not
see my error. I need to learn what I've done incorrectly between the
wx.Frame initialization and referencing it so the module runs.

Rich

The main frame is defined as:

class MainFrame(wx.Frame):
   def __init__(self, parent, title):
       wx.Frame.__init__(self, parent, id=wx.ID_ANY, title='openEDMS',
                size=wx.Size(700,400), style=wx.DEFAULT_FRAME_STYLE, name='main_frame')

At the bottom of the module is:

if __name__ == '__main__':
   edms = wx.App()
   top = main_frame('openEDMS')
   top = Show()
   edms.MainLoop()

and Python3 tells me there's a error on the first 'top = ' line:

Traceback (most recent call last):
File "./openEDMS.py", line 297, in <module>
   top = main_frame('openEDMS')
NameError: name 'main_frame' is not defined

Your class is named MainFrame not main_frame.

When I read about wx.Frame I see that the name parameter is the internal
reference name for the class so I thought the above was correct. When I
replace the name with the class name I get a different error:

Traceback (most recent call last):
File "./openEDMS.py", line 296, in <module>
   top = MainFrame('openEDMS')
TypeError: __init__() missing 1 required positional argument: 'title'

I've looked at the 4.0.1 demo, a couple of tutorials, and the 4.0.2a API
at <https://wxpython.org/Phoenix/docs/html/wx.Frame.html&gt; yet still do not
see my error. I need to learn what I've done incorrectly between the
wx.Frame initialization and referencing it so the module runs.

The TypeError is telling you the problem. Your class MainFrame takes two arguments in its __init__ method, parent and title. You've only passed one argument.

···

On Wed, 25 Apr 2018, Rich Shepard wrote:

Your class is named MainFrame not main_frame.

Scott,

   I thought the reference should be to the class name, not the internal
name. Thanks for confirming this.

The TypeError is telling you the problem. Your class MainFrame takes two
arguments in its __init__ method, parent and title. You've only passed one
argument.

   Then I'm not seeing the type error. The class __init__ method specifies
the title:

def __init__(self, parent, title):
         wx.Frame.__init__(self, parent, id=wx.ID_ANY, title='openEDMS',
                  size=wx.Size(700,400), style=wx.DEFAULT_FRAME_STYLE)

but the type error is at the end of the module where there is no __init__
method, but a reference to the class name with the frame's title.

Thanks,

Rich

···

On Wed, 25 Apr 2018, Scott Talbert wrote:

When you are calling MainFrame() you are instantiating a class, which will call the __init__ method. You are only passing a single parameter to MainFrame() but you have defined MainFrame's __init__ to have two parameters, parent and title. You have to pass both.

You might want to brush up on Python classes here:

See the documentation about the __init__ method.

···

On Wed, 25 Apr 2018, Rich Shepard wrote:

The TypeError is telling you the problem. Your class MainFrame takes two
arguments in its __init__ method, parent and title. You've only passed one
argument.

Then I'm not seeing the type error. The class __init__ method specifies
the title:

def __init__(self, parent, title):
       wx.Frame.__init__(self, parent, id=wx.ID_ANY, title='openEDMS',
                size=wx.Size(700,400), style=wx.DEFAULT_FRAME_STYLE)

but the type error is at the end of the module where there is no __init__
method, but a reference to the class name with the frame's title.

if name == ‘main’:

 edms = wx.App()

 top = main_frame('openEDMS')

 top = Show()

In addition to the class name and init notes, you likely want to use top.Show() here if you actually want to show the frame instead of calling some non-existent top-level Show function.

···

On Wednesday, April 25, 2018 at 3:12:01 PM UTC-7, Rich wrote:

Robin

When you are calling MainFrame() you are instantiating a class, which will
call the __init__ method. You are only passing a single parameter to
MainFrame() but you have defined MainFrame's __init__ to have two
parameters, parent and title. You have to pass both.

Scott,

   Got it, thanks.

You might want to brush up on Python classes here:
9. Classes — Python 3.6.15 documentation
See the documentation about the __init__ method.

   I am doing this. It didn't occur to me to review the class as I was
focused on the wxPython wx.Frame() object.

Much appreciated,

Rich

···

On Wed, 25 Apr 2018, Scott Talbert wrote:

Robin,

   Thank you. It's been a long time since I used wxPython and I've forgotten
much more than I realized.

Best regards,

Rich

···

On Wed, 25 Apr 2018, Robin Dunn wrote:

if __name__ == '__main__':
     edms = wx.App()
     top = main_frame('openEDMS')
     top = Show()

In addition to the class name and __init__ notes, you likely want to use
top.Show() here if you actually want to show the frame instead of calling
some non-existent top-level Show function.

I know your questions were answered, but I’m confused enough by this
statement that I’d like to explore it. You had this:
MainFrame IS the class name. That’s all Python knows. “main_frame”
is just a string that happens to be passed as a parameter to another
method call. The language doesn’t know anything about that. More,
that string won’t be seen until the class is instantiated, which can
only be done by using the Python class name.
The type error happens at the line where the error occurs:
top = MainFrame(‘openEDMS’)
This is just like calling a constructor in C++. It creates an
object, and calls the init method to initialize the object.
This is exactly the same as:
top = MainFrame.init( , ‘openEDMS’ )
Given that, you can see that you are missing the “parent” parameter
to your constructor.

···

Rich Shepard wrote:

  On

Wed, 25 Apr 2018, Scott Talbert wrote:

    Your class is named MainFrame not

main_frame.

    I thought the reference should be to the class name, not the

internal

  name. Thanks for confirming this.

class MainFrame(wx.Frame):

  `          def __init__(self, parent, title):

`

  `                wx.Frame.__init__(self, parent, id=wx.ID_ANY,

title=‘openEDMS’,
`

  `                         size=wx.Size(700,400),

style=wx.DEFAULT_FRAME_STYLE, name=‘main_frame’)
`

    The TypeError is telling you the problem.

Your class MainFrame takes two

    arguments in its __init__ method, parent and title. You've only

passed one

    argument.
    Then I'm not seeing the type error. The class __init__ method

specifies

  the title:


  ..

  but the type error is at the end of the module where there is no

init

  method, but a reference to the class name with the frame's title.
-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

class MainFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, id=wx.ID_ANY, title='openEDMS',
                 size=wx.Size(700,400), style=wx.DEFAULT_FRAME_STYLE,
name='main_frame')

and I just noticed that your __init__ takes 'title' as a (required)
parameter, and then you set the title to a hard-coded string later:

title='openEDMS',

you sure don't want to require a parameter your not going to use :wink:

-CHB

···

On Wed, Apr 25, 2018 at 5:51 PM, Tim Roberts <timr@probo.com> wrote:

MainFrame IS the class name. That's all Python knows. "main_frame" is
just a string that happens to be passed as a parameter to another method
call. The language doesn't know anything about that. More, that string
won't be seen until the class is instantiated, which can only be done by
using the Python class name.

The TypeError is telling you the problem. Your class MainFrame takes two
arguments in its __init__ method, parent and title. You've only passed one
argument.

  Then I'm not seeing the type error. The class __init__ method specifies
the title:
..
but the type error is at the end of the module where there is no __init__
method, but a reference to the class name with the frame's title.

The type error happens at the line where the error occurs:
    top = MainFrame('openEDMS')

This is just like calling a constructor in C++. It creates an object, and
calls the __init__ method to initialize the object. This is exactly the
same as:
    top = MainFrame.__init__( <empty object>, 'openEDMS' )

Given that, you can see that you are missing the "parent" parameter to
your constructor.

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

--
You received this message because you are subscribed to the Google Groups
"wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Chris,

   Yep. Got this fixed yesterday:

     def __init__(self, parent, title='openEDMS'):
         wx.Frame.__init__(self, parent, id=wx.ID_ANY, size=wx.Size(800,600),
                           style=wx.DEFAULT_FRAME_STYLE)

Thanks,

Rich

···

On Thu, 26 Apr 2018, Chris Barker wrote:

and I just noticed that your __init__ takes 'title' as a (required)
parameter, and then you set the title to a hard-coded string later:
title='openEDMS',
you sure don't want to require a parameter your not going to use :wink:

Robin,

   Made that change.

Thanks,

Rich

···

On Wed, 25 Apr 2018, Robin Dunn wrote:

In addition to the class name and __init__ notes, you likely want to use
top.Show() here if you actually want to show the frame instead of calling
some non-existent top-level Show function.

Rich Shepard wrote:

···

On Thu, 26 Apr 2018, Chris Barker wrote:

and I just noticed that your __init__ takes 'title' as a (required)
parameter, and then you set the title to a hard-coded string later:
title='openEDMS',
you sure don't want to require a parameter your not going to use :wink:

Chris,

Yep. Got this fixed yesterday:

def \_\_init\_\_\(self, parent, title=&#39;openEDMS&#39;\):
    wx\.Frame\.\_\_init\_\_\(self, parent, id=wx\.ID\_ANY,

size=wx.Size(800,600),
style=wx.DEFAULT_FRAME_STYLE)

Almost. You still need to pass the title to the wx.Frame.

def \_\_init\_\_\(self, parent, title=&#39;openEDMS&#39;\):
    wx\.Frame\.\_\_init\_\_\(self, parent, id=wx\.ID\_ANY, title=title,

size=wx.Size(800,600),
style=wx.DEFAULT_FRAME_STYLE)

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Tim,

   That explains why I saw 'title' in both the class and frame inits. The
last bits of fog have cleared away with the lights you and Robin shown on
it.

Much appreciated,

Rich

···

On Thu, 26 Apr 2018, Tim Roberts wrote:

Almost. You still need to pass the title to the wx.Frame.

def \_\_init\_\_\(self, parent, title=&#39;openEDMS&#39;\):
    wx\.Frame\.\_\_init\_\_\(self, parent, id=wx\.ID\_ANY, title=title,

size=wx.Size(800,600),
style=wx.DEFAULT_FRAME_STYLE)