pywrap : getopt and global namespace

This is my first post and I am relatively new to python and wxpython and I use wxdesigner as well. I added the cool Widget Inspection Tool (WIT) to an admin menu to help with sizer/widget issues and that has really helped. Having used the python shell to test various python commands and behavior, I thought it would be very helpful to use pywrap to work with my actual python/wxpython application.
The first issue I ran into was that pywrap does not call my main(), which calls init_main() for most of the non-wx program initialization, but rather goes straight to my Main_App(wx.App) class. The second was that getopt was not processing my command line arguments (pywrap test2.py --verbose --fullxml). I saw that sys.argv was not adjusted by pywrap so sys.argv[1] was the module name, test2.py in this case, and that, of course, was considered a non-option. Not being sure the best way for my app to know it was being run by pywrap, I resolved those 2 issues in the Main_App(wx.App) class’s OnInit() method by testing sys.argv[0] for “pywrap”. If so, the app changes the options argument list from the standard sys.argv[1:] to sys.argv[2:] and then calls the init_main() that would normally have been called from main(). Then it proceeds to the rest of the initialization and creates the frame via Main_Frame(wx.Frame) class.
class Main_App(wx.App):
“”“Create wxWindow main app”""

def OnInit(self):
    global appsys
    global opt_pywrap
   
    wx.InitAllImageHandlers()
···
    #=======================================================================
    # pywrap does not run the main code which calls init_main()
    #=======================================================================
    if __name__ != '__main__':
        try:
            dummy = AppSys.initialized
            del(dummy)
        except (NameError, AttributeError), dummy_err:
            if sys.argv[0].endswith("pywrap"):
                opt_pywrap = True
                options = sys.argv[2:]
            else:
                options = sys.argv[1:]
            if not init_main(options=options):
                print "init_main() failed."           
                return False

. . .

I have not been able to figure out this next problem. I know that in the pycrust shell, app is the wx.App of my application so I can get to its instance variables, but how do I get to the rest of the test2.py module from the pycrust shell’s prompt? In order to interactively test different aspects of the actual application, I need to create new class instances, as well as access current global variables, class variables, class instances, etc. - interact with the actual running application. I tried typing in from test2 import * and that gives me access to the global variable and class names, but not the same objects as the application is using. The global and class variables that were updated in the application have the default values when referring to them from the shell after the import.
The wxPython Py documentation says that “PyWrap is a runtime utility that lets you run an existing wxPython
program with a PyCrust frame at the same time. Inside the PyCrust
shell namespace, the local variable app is assigned to your
application instance. In this way you can introspect your entire
application within the PyCrust shell
, as well as the PyFilling
namespace viewer.”. So how do I get to the “entire application” - I guess somehow the globals() of the test2.py being run by pywrap?

Thank you.

Fatal114QT-2 wrote

I have not been able to figure out this next problem. I know that in the
pycrust shell, app is the wx.App of my application so I can get to its
instance variables, but how do I get to the rest of the test2.py module
from the pycrust shell's prompt? In order to interactively test different
aspects of the actual application, I need to create new class instances,
as
well as access current global variables, class variables, class instances,
etc. - interact with the actual running application. I tried typing in
*from
test2 import ** and that gives me access to the global variable and class
names, but not the same objects as the application is using. The global
and
class variables that were updated in the application have the default
values when referring to them from the shell after the import.

The wxPython Py documentation says that "PyWrap is a runtime utility that
lets you run an existing wxPython program with a PyCrust frame at the same
time. Inside the PyCrust shell namespace, the local variable app is
assigned to your application instance. *In this way you can introspect
your
entire application within the PyCrust shell*, as well as the PyFilling
namespace viewer.". So how do I get to the "entire application" - I guess
somehow the globals() of the test2.py being run by pywrap?

After further testing of globals(), I have reached the following preliminary
conclusions:

*) Putting
    self.app_globals = globals()
in the OnInit() method of my Main_App(wx.App) class allows me to see the
global dictionary of the running test2.py application in PyWrap's PyCrust
shell as app.app_globals.

*) For convenience, in the shell I assign it to a short variable:
    >>> ag = app.app_globals
The Namespace notebook tab in the PyFilling splitter window below now shows
ag in locals() (as well as the original app.app_globals) for full
introspection.

*) You can now access the global variables, functions, and classes known to
the test2.py module. For example, the InfoLine() function in test2.py can be
used from the shell through the ag dictionary:
    >>> ag["InfoLine"](app.main_frame, "This message for informational
purposes only")
or directly by setting local variables in the shell to point to the function
object:
    >>> InfoLine = ag["InfoLine"]
    >>> InfoLine(app.main_frame, "This message for informational purposes
only")
The latter makes it easier to use and copy / paste the commands into your
real code if you like the result.

*) From the interactive PyCrust shell, you can also create instances of
classes to work with and test. Using the QList class in test2.py:
    >>> qlist = ag["QList"]()
    >>> qlist.new_list(("ColA", "ColB", "ColC"))
    >>> qlist.append([(1, 10, 100), (2, 20, 200)])
    >>> qlist.show()

It seems to me at this point that I can more fully utilize pywrap to develop
and test different aspects of my current wxPython project. From actually
using pywrap on my current project to see if I come across any further info
or drawbacks, and from any comments to this post, I will create and attach a
small test program and some examples of the shell commands that can be used
with it.

···

--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/pywrap-getopt-and-global-namespace-tp5718785p5718788.html
Sent from the wxPython-users mailing list archive at Nabble.com.

Fatal114QT-2 wrote:

Fatal114QT-2 wrote
It seems to me at this point that I can more fully utilize pywrap to develop
and test different aspects of my current wxPython project. From actually
using pywrap on my current project to see if I come across any further info
or drawbacks, and from any comments to this post, I will create and attach a
small test program and some examples of the shell commands that can be used
with it.

You should also take a look at the Widget Inspection Tool. Not only does it give you a PyShell to work with, (PyCrust too, but it is turned off by default) but you also have tools for browsing, introspecting and interacting with the live widgets in your application. Very helpful.

http://wiki.wxpython.org/Widget_Inspection_Tool

···

--
Robin Dunn
Software Craftsman

Yes, Robin. the WIT is a wonderful tool. I was not able to get it working properly from keyboard shortcut so I just added it to my Admin menu and call it from there. I use wxDesigner but needed to create parts of it manually because I could not figure out how to do it directly with wxDesigner. Of course the manual parts outside of it were not sizing properly and I read about your WIT. For those with sizing issues, it is invaluable to figure out where to properly place the sizers and who the parents are to refresh(), etc. if necessary. After finding one of your posts regarding using custom classes with wxDesigner, I was able to once again use it for most of my layout. I highly recommend that product as well.

···

On Fri, Oct 4, 2013 at 1:55 PM, Robin Dunn [via wxPython-users] <[hidden email]> wrote:

You should also take a look at the Widget Inspection Tool. Not only
does it give you a PyShell to work with, (PyCrust too, but it is turned
off by default) but you also have tools for browsing, introspecting and
interacting with the live widgets in your application. Very helpful.

http://wiki.wxpython.org/Widget_Inspection_Tool


Robin Dunn

Software Craftsman

http://wxPython.org


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 [hidden email].

For more options, visit https://groups.google.com/groups/opt_out.


If you reply to this email, your message will be added to the discussion below:

http://wxpython-users.1045709.n5.nabble.com/pywrap-getopt-and-global-namespace-tp5718785p5718803.html

To unsubscribe from pywrap : getopt and global namespace, click here.

  [NAML](http://wxpython-users.1045709.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml)