Importing event ID's

Hi all,

I've got an app that runs the MainApp, builds the MainFrame, and
instantiates a wxSplitterWindow all inside one script (say "one.py").

I then build additional scripts ("two.py") that have a two.Panel,
two.Menubar, two.Toolbar and two.EventHandler in each.

I've written a method in my MainFrame class called "Swap" that has the
function of reading in the new classes, setting the menubar and toolbar,
calling wxSplitterWindow.ReplaceWindow (to swap out the panel being
displayed), and finally calling PopEventHandler and PushEventHandler. The
code for this method is at the end of the post.

Everything is pretty-much working so far, except for a couple of little
quirks. The real problem, though, is that I don't think importing the module
inside a method of MainFrame is making the event IDs (i.e.
ID_Whatever=wxNewId() ) available for processing. So, everything displays -
but nothing responds. I can't put all the id's inside the main script
because I'm shooting for add-on modules - so I won't know which modules the
user has installed or even which ones I might decide to make in the future.

Any guidance on how to read in those id's from the called script, into a
method (i.e. "local") and still make the event handler work?

Code for the swap method (don't laugh, Robin, I'm just a beginner). Import
statements are replicated in multiple locations because of the possibility
of calling swap to only replace a single item - the menu for instance - and
leaving all the others alone. Only Pop&PushEventHandler is always executed.

    def Swap(self, package='package', pmod='module', pclass='class',
menubar='menubar', toolbar='toolbar', events='events'):
        global PANEL, REVERT
        global frame_1, window_1, pane_2
        if package != 'package':
            REVERT['package'], PANEL['package'] = PANEL['package'], package
        if pmod != 'module':
            REVERT['module'], PANEL['module'] = PANEL['module'], pmod
        if pclass != 'class':
            self.oldpanel, REVERT['class'], PANEL['class'] =
PANEL['oldpanel'], PANEL['class'], pclass
            exec 'import ' + PANEL['package'] + "." + PANEL['module']
            self.newtarget = eval(PANEL['package'] + "." + PANEL['module'] +
"." + PANEL['class'])
            self.newpanel = self.newtarget(PANEL, window_1, -1)
            PANEL['oldpanel']= self.newpanel
            window_1.ReplaceWindow(self.oldpanel, self.newpanel)
            self.oldpanel.Destroy()
        if menubar != 'menubar':
            REVERT['menubar'], PANEL['menubar'] = PANEL['menubar'], menubar
            exec 'import ' + PANEL['package'] + "." + PANEL['module']
            self.newmenutarget = eval(PANEL['package'] + "." +
PANEL['module'] + "." + PANEL['menubar'])
            self.SetMenuBar(self.newmenutarget())
        if toolbar != 'toolbar':
            REVERT['toolbar'], PANEL['toolbar'] = PANEL['toolbar'], toolbar
            exec 'import ' + PANEL['package'] + "." + PANEL['module']
            self.newtooltarget = eval(PANEL['package'] + "." +
PANEL['module'] + "." + PANEL['toolbar'])
            self.SetToolBar(self.newtooltarget(self, -1))
        if events != 'events':
            REVERT['events'], PANEL['events'] = PANEL['events'], events
        frame_1.PopEventHandler()
        try:
            frame_1.PushEventHandler(PANEL['events'](frame_1, window_1,
pane_2, PANEL['menubar'], PANEL['toolbar']))
        except:
            frame_1.PushEventHandler(REVERT['events'](frame_1, window_1,
pane_2, PANEL['menubar'], PANEL['toolbar']))
        self.Layout()

I should add, BTW, that I'm running Python 2.3 on WinXP and wxPython
2.4.2.4u. The called files are configured in a pretty straight-forward
manner:

two.py

ID_One = wxNewId()
ID_Two = wxNewId()
...
class Panel
    ....
class Menubar
    ....
class Toolbar
    ....
class EventHandler
    ....

and that's about it.

···

----- Original Message -----
From: "Rick Zoerner" <rick@zoerner.com>
To: "WxPython-users" <wxPython-users@lists.wxwindows.org>
Sent: Friday, January 16, 2004 8:24 PM
Subject: [wxPython-users] Importing event ID's

Hi all,

I've got an app that runs the MainApp, builds the MainFrame, and
instantiates a wxSplitterWindow all inside one script (say "one.py").

I then build additional scripts ("two.py") that have a two.Panel,
two.Menubar, two.Toolbar and two.EventHandler in each.

I've written a method in my MainFrame class called "Swap" that has the
function of reading in the new classes, setting the menubar and toolbar,
calling wxSplitterWindow.ReplaceWindow (to swap out the panel being
displayed), and finally calling PopEventHandler and PushEventHandler. The
code for this method is at the end of the post.

Everything is pretty-much working so far, except for a couple of little
quirks. The real problem, though, is that I don't think importing the

module

inside a method of MainFrame is making the event IDs (i.e.
ID_Whatever=wxNewId() ) available for processing. So, everything

displays -

but nothing responds. I can't put all the id's inside the main script
because I'm shooting for add-on modules - so I won't know which modules

the

user has installed or even which ones I might decide to make in the

future.

Any guidance on how to read in those id's from the called script, into a
method (i.e. "local") and still make the event handler work?

Code for the swap method (don't laugh, Robin, I'm just a beginner). Import
statements are replicated in multiple locations because of the possibility
of calling swap to only replace a single item - the menu for instance -

and

leaving all the others alone. Only Pop&PushEventHandler is always

executed.

    def Swap(self, package='package', pmod='module', pclass='class',
menubar='menubar', toolbar='toolbar', events='events'):
        global PANEL, REVERT
        global frame_1, window_1, pane_2
        if package != 'package':
            REVERT['package'], PANEL['package'] = PANEL['package'],

package

        if pmod != 'module':
            REVERT['module'], PANEL['module'] = PANEL['module'], pmod
        if pclass != 'class':
            self.oldpanel, REVERT['class'], PANEL['class'] =
PANEL['oldpanel'], PANEL['class'], pclass
            exec 'import ' + PANEL['package'] + "." + PANEL['module']
            self.newtarget = eval(PANEL['package'] + "." + PANEL['module']

+

"." + PANEL['class'])
            self.newpanel = self.newtarget(PANEL, window_1, -1)
            PANEL['oldpanel']= self.newpanel
            window_1.ReplaceWindow(self.oldpanel, self.newpanel)
            self.oldpanel.Destroy()
        if menubar != 'menubar':
            REVERT['menubar'], PANEL['menubar'] = PANEL['menubar'],

menubar

            exec 'import ' + PANEL['package'] + "." + PANEL['module']
            self.newmenutarget = eval(PANEL['package'] + "." +
PANEL['module'] + "." + PANEL['menubar'])
            self.SetMenuBar(self.newmenutarget())
        if toolbar != 'toolbar':
            REVERT['toolbar'], PANEL['toolbar'] = PANEL['toolbar'],

toolbar

            exec 'import ' + PANEL['package'] + "." + PANEL['module']
            self.newtooltarget = eval(PANEL['package'] + "." +
PANEL['module'] + "." + PANEL['toolbar'])
            self.SetToolBar(self.newtooltarget(self, -1))
        if events != 'events':
            REVERT['events'], PANEL['events'] = PANEL['events'], events
        frame_1.PopEventHandler()
        try:
            frame_1.PushEventHandler(PANEL['events'](frame_1, window_1,
pane_2, PANEL['menubar'], PANEL['toolbar']))
        except:
            frame_1.PushEventHandler(REVERT['events'](frame_1, window_1,
pane_2, PANEL['menubar'], PANEL['toolbar']))
        self.Layout()

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

Have you considered using XML ressources ?
You can define the GUI in an xrc file and the load the file in the module.
This would require you to have 2 files for the module (the module itself and
the according ressource file), but that shouldn't be an issue.
With XRC you can address the controls by using the macros

xrc.XRCCTRL(dialog,'ID_WHATEVER_YOU_NAMED_IT')
which returns a control as object or

xrc.XRCID('ID_WHATEVER....')
which returns the numeric id that has been assigned automatically when loading
the dialog from the xrc file.

Using XRC is quite simple and has a bunch of benefits (you could change the
look of the screen without even touching your code and you can reuse dialogs
in other places.

See the demo and the docs.

UC

I should add, BTW, that I'm running Python 2.3 on WinXP and wxPython
2.4.2.4u. The called files are configured in a pretty straight-forward
manner:

two.py

ID_One = wxNewId()
ID_Two = wxNewId()
...
class Panel
    ....
class Menubar
    ....
class Toolbar
    ....
class EventHandler
    ....

and that's about it.

From: "Rick Zoerner" <rick@zoerner.com>
To: "WxPython-users" <wxPython-users@lists.wxwindows.org>
Sent: Friday, January 16, 2004 8:24 PM
Subject: [wxPython-users] Importing event ID's

> Hi all,
>
> I've got an app that runs the MainApp, builds the MainFrame, and
> instantiates a wxSplitterWindow all inside one script (say "one.py").
>
> I then build additional scripts ("two.py") that have a two.Panel,
> two.Menubar, two.Toolbar and two.EventHandler in each.
>
> I've written a method in my MainFrame class called "Swap" that has the
> function of reading in the new classes, setting the menubar and toolbar,
> calling wxSplitterWindow.ReplaceWindow (to swap out the panel being
> displayed), and finally calling PopEventHandler and PushEventHandler. The
> code for this method is at the end of the post.
>
> Everything is pretty-much working so far, except for a couple of little
> quirks. The real problem, though, is that I don't think importing the

module

> inside a method of MainFrame is making the event IDs (i.e.
> ID_Whatever=wxNewId() ) available for processing. So, everything

displays -

> but nothing responds. I can't put all the id's inside the main script
> because I'm shooting for add-on modules - so I won't know which modules

the

> user has installed or even which ones I might decide to make in the

future.

> Any guidance on how to read in those id's from the called script, into a
> method (i.e. "local") and still make the event handler work?
>
> Code for the swap method (don't laugh, Robin, I'm just a beginner).
> Import statements are replicated in multiple locations because of the
> possibility of calling swap to only replace a single item - the menu for
> instance -

and

> leaving all the others alone. Only Pop&PushEventHandler is always

executed.

> def Swap(self, package='package', pmod='module', pclass='class',
> menubar='menubar', toolbar='toolbar', events='events'):
> global PANEL, REVERT
> global frame_1, window_1, pane_2
> if package != 'package':
> REVERT['package'], PANEL['package'] = PANEL['package'],

package

> if pmod != 'module':
> REVERT['module'], PANEL['module'] = PANEL['module'], pmod
> if pclass != 'class':
> self.oldpanel, REVERT['class'], PANEL['class'] =
> PANEL['oldpanel'], PANEL['class'], pclass
> exec 'import ' + PANEL['package'] + "." + PANEL['module']
> self.newtarget = eval(PANEL['package'] + "." +
> PANEL['module']

+

> "." + PANEL['class'])
> self.newpanel = self.newtarget(PANEL, window_1, -1)
> PANEL['oldpanel']= self.newpanel
> window_1.ReplaceWindow(self.oldpanel, self.newpanel)
> self.oldpanel.Destroy()
> if menubar != 'menubar':
> REVERT['menubar'], PANEL['menubar'] = PANEL['menubar'],

menubar

> exec 'import ' + PANEL['package'] + "." + PANEL['module']
> self.newmenutarget = eval(PANEL['package'] + "." +
> PANEL['module'] + "." + PANEL['menubar'])
> self.SetMenuBar(self.newmenutarget())
> if toolbar != 'toolbar':
> REVERT['toolbar'], PANEL['toolbar'] = PANEL['toolbar'],

toolbar

> exec 'import ' + PANEL['package'] + "." + PANEL['module']
> self.newtooltarget = eval(PANEL['package'] + "." +
> PANEL['module'] + "." + PANEL['toolbar'])
> self.SetToolBar(self.newtooltarget(self, -1))
> if events != 'events':
> REVERT['events'], PANEL['events'] = PANEL['events'], events
> frame_1.PopEventHandler()
> try:
> frame_1.PushEventHandler(PANEL['events'](frame_1, window_1,
> pane_2, PANEL['menubar'], PANEL['toolbar']))
> except:
> frame_1.PushEventHandler(REVERT['events'](frame_1, window_1,
> pane_2, PANEL['menubar'], PANEL['toolbar']))
> self.Layout()
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
> For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

- --
  UC

- --
Open Source Solutions 4U, LLC 2570 Fleetwood Drive
Phone: +1 650 872 2425 San Bruno, CA 94066
Cell: +1 650 302 2405 United States
Fax: +1 650 872 2417

···

On Friday 16 January 2004 08:38 pm, Rick Zoerner wrote:

----- Original Message -----

Uwe C. Schroeder wrote:

Have you considered using XML ressources ?

No, I haven't. Python is my fist programming language (unless you count
BASIC c.1980 - think 1Mhz 6502 and Z80 processors) and I've only been
teaching myself that for about a month, so far. Actually, I haven't really
made it all the way to Python, yet, since 90% of what I've done so far is
laying out the opening gui in wxPython. 'KISS' is the paramount principle
here. One language at a time.

What I'm hoping for is some way to either:

a) put the requisite ID_Names in a list to pass in and assign wxNewId() in a
loop (which doesn't seem to work for me so far), or

b) same as above - but in a Metakit table. I think the table is a better
solution because I want to be able to install add-on modules and it would be
easy enough to have the installation routine add in any ID_Names associated
with that module. Then the MainApp can query that table at program init to
extract a distinct set (i.e. no duplicates) and again, via loop, assign
wxNewId() "en masse". But again, I can't see how I could make a loop
actually be assigning a whole bunch of distinct ID_Name = wxNewId() instead
of a whole bunch of x=wxIdNew() -- resulting in one assignment at the end of
the loop called x instead of the 50-100 I need.

Rick Zoerner wrote:

Everything is pretty-much working so far, except for a couple of little
quirks. The real problem, though, is that I don't think importing the module
inside a method of MainFrame is making the event IDs (i.e.
ID_Whatever=wxNewId() ) available for processing.

When the module is imported for the first time then the code within it is executed and assignments like the above will make objects in the module's namespace. It doesn't matter where the import is done from.

So, everything displays -
but nothing responds. I can't put all the id's inside the main script
because I'm shooting for add-on modules - so I won't know which modules the
user has installed or even which ones I might decide to make in the future.

I'm not quite sure what you are trying to do but perhaps if you think about things a little differently it will help. Instead of trying to make your framework figure everything out, have the framework call a function/method in your plugin modules that does the work iteself to setup or teardown the plugin. For example:

  def Swap(self, newPluginModuleName):
    self.current.Teardown()
    module = __import__(newPluginModuleName)
    module.Setup()
    self.current = module

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!