calling wxPython from the shell

Angel Asencio asencio at mitre.org
Fri Jul 5 19:37:45 EDT 2002


Ok, found the flag of "MainLoop", and works like a charm using BOA.
(Where I developed it first)

But when I tried it on PythonWin, as soon I closed it, it bomb out.

I tryied on IDLEfork also,
If I close first the window (in selectOneHandler.py) and then exit the main
loop,
works fine (gives the result) but does not close the window.
Viceversa closes the window but frezzes IDLEfork.

The code relative to this is on  selectOneHandler.py near the bottom at the
button
events code.

This is the command that I am putting on the shell:

print(selectorHandler.getOneStr( ["alpha","beta","chi","delta"] ))

Ok, now for the source code:

selectorHandler.py ***********************

#!/usr/bin/env python
#Boa:App:SelectorApp

from wxPython.wx import *

import selectOneHandler
import selectMultipleHandler

modules ={'selectMultipleHandler': [0, '', 'selectMultipleHandler.py'],
 'selectOneHandler': [0, 'Main frame of Application',
'selectOneHandler.py']}

class SelectorApp(wxApp):
    def OnInit(self):
        self.slctOne = selectOneHandler.create(None)
        self.slctMult = selectMultipleHandler.create(None)
        self.toReturn = None
        self.selection = -1
        self.slctOne.setOwner( self )
        self.slctMult.setOwner( self )
        return true

    def setOneAsMain(self):
        self.main = self.slctOne

    def setMultAsMain(self):
        self.main = self.slctMult

    def setSelectionList( self, lstString ):
        self.main.setSelectionList(lstString)

    def setSelection( self, anIntegerOrLstOf ):
        self.selection = anIntegerOrLstOf

    def getSelection( self ):
        return self.selection

    def getObjectStrings(self, lstObjects ):
        toReturn = []

        toReturn = [ obj.__str__() \
                        for obj in lstObjects ]

        return toReturn #colletion of strings

    def getObject(self, intIndex, lstObjects ):
        toReturn = None

        if (not (intIndex == -1)):
            toReturn = lstObjects[intIndex]

        return toReturn #an Object

    def getObjects(self, lstIntegers, lstObjects ): # PENDING
        toReturn = []

        toReturn = [ lstObjects[ anIndex ] \
                        for anIndex in lstIntegers ]

        return toReturn #Zero or more objects

    def getSelctStr( self, intIndx, lstStrings ):
        toReturn = None

        if ( not (intIndx == -1 ) ):
            toReturn = lstStrings[intIndx]
        return toReturn

    def getSelctStrs( self, lstIntIndxs, lstStrings ):
        toReturn = None

        if ( not (len(lstIntIndxs) == 0 ) ):
            toReturn = [ self.getSelctStr(anIndx, lstStrings) \
                        for anIndx in lstIntIndxs ]
        return toReturn

class TestObject:

    def __init__(self, strID = "noID"):
        self.id = strID

    def __str__(self):
        return self.id


def getOneStr( lstString ):
    return mainOneStr( lstString )

def getOneObj( lstObject ):
    return mainOneObj( lstObject )

def getMultStr( lstString ):
    return mainMultStr( lstString )

def getMultObj( lstObject ):
    return mainMultObj( lstObject )

def mainOneStr(lstString ):

    application = SelectorApp(0)
    application.setOneAsMain()
    application.setSelectionList(lstString)
    application.main.Show(true)
    application.SetTopWindow(application.main)
    application.MainLoop()

    return application.getSelctStr(application.getSelection(), lstString)

def mainOneObj( lstObject ):
    lstString = []
    toReturn = None #should be an object

    application = SelectorApp(0)
    application.setOneAsMain()

    lstString = application.getObjectStrings(lstObject )
    application.setSelectionList(lstString)
    application.main.Show(true)
    application.SetTopWindow(application.main)
    application.MainLoop() #stops when GUI closes
    toReturn = application.getObject( application.getSelection() , \
                 lstObject )
    return toReturn

def mainMultStr(lstString ):

    application = SelectorApp(0)
    application.setMultAsMain()
    application.setSelectionList(lstString)
    application.main.Show(true)
    application.SetTopWindow(application.main)
    application.MainLoop()

    return application.getSelctStrs(application.getSelection(), lstString)


def mainMultObj( lstObject ):
    lstString = []
    toReturn = None #should be a collection of zero or more objects

    application = SelectorApp(0)
    application.setMultAsMain()

    lstString = application.getObjectStrings(lstObject )
    application.setSelectionList(lstString)
    application.main.Show(true)
    application.SetTopWindow(application.main)
    application.MainLoop() #stops when GUI closes
    toReturn = application.getObjects( application.getSelection() , \
                 lstObject )
    return toReturn

def main():
    application = SelectorApp(0)
    application.main.Show(true)
    application.SetTopWindow(self.main)
    application.MainLoop()
    return application.getToReturn()

if __name__ == '__main__':
    main()

selectOneHandler.py *************************************

#Boa:Frame:SelectOneHandler
from wxPython.wx import *
from wxPython.lib.buttons import *

def create(parent):
    return SelectOneHandler(parent)

[wxID_SELECTONEHANDLER, wxID_SELECTONEHANDLERBTNACCEPT,
wxID_SELECTONEHANDLERBTNCANCEL, wxID_SELECTONEHANDLERLISTBOX1] = map(lambda
_init_ctrls: wxNewId(), range(4))

class SelectOneHandler(wxFrame):

    def _init_utils(self):
        pass

    def _init_ctrls(self, prnt):
        wxFrame.__init__(self, id = wxID_SELECTONEHANDLER, name = '', parent
= prnt, pos = wxPoint(555, 60), size = wxSize(225, 455), style =
wxDEFAULT_FRAME_STYLE, title = 'SelectOneHandler')
        self._init_utils()
        self.SetClientSize(wxSize(217, 428))

        self.listBox1 = wxListBox(choices = [], id =
wxID_SELECTONEHANDLERLISTBOX1, name = 'listBox1', parent = self, pos =
wxPoint(56, 24), size = wxSize(115, 296), style = 0, validator =
wxDefaultValidator)

        self.btnCancel = wxButton(id = wxID_SELECTONEHANDLERBTNCANCEL, label
= 'Cancel', name = 'btnCancel', parent = self, pos = wxPoint(144, 344), size
= wxSize(51, 51), style = 0)
        EVT_BUTTON(self.btnCancel, wxID_SELECTONEHANDLERBTNCANCEL,
self.OnBtncancelButton)

        self.btnAccept = wxButton(id = wxID_SELECTONEHANDLERBTNACCEPT, label
= 'Accept', name = 'btnAccept', parent = self, pos = wxPoint(32, 344), size
= wxSize(51, 51), style = 0)
        EVT_BUTTON(self.btnAccept, wxID_SELECTONEHANDLERBTNACCEPT,
self.OnBtnacceptButton)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.owner = None

    def setSelectionList(self, lstString ):
        self.listBox1.InsertItems( lstString, 0 )

    def setOwner( self, someOwner ):
        self.owner = someOwner

    def getOwner():
        return self.owner

    def OnBtncancelButton(self, event):
        self.owner.setSelection(-1)

        try:
            self.Close(true)
            print( "cancel closed")
            self.owner.ExitMainLoop()
            print("cancel loop")
        except:
            print( "cancel except")

    def OnBtnacceptButton(self, event):
        selection = self.listBox1.GetSelection()
        self.owner.setSelection(selection)

        try:
            self.Close(true)
            print( "accept closed" )
            self.owner.ExitMainLoop()
            print("accept loop")
        except:
            print( "accept except" )


Angel Asencio wrote:

> Greetings:
>
>   I have been requested to write a function that will take a list, open
> a selection window, and then when the window is closed the function
> would return a list with the selected items.
>
>   Looks like wxPython is quite favored, and I decided to use the Boa
> Constructor, finished the tutorial. Everything fine and dandy, until I
> wanted to create the function that you can call from the shell.
>
>   The scenario that I have tried is for the function to call the MainApp
> (that has the
> MainLoop) with the Frame that will do the selection. I understand about
> the events, but what ever I do with the event, 'myModule.myFunction( [
> "a","collection","to","select"])' return nothing on the combinations I
> have tried.
>
>   Bottom line is that I want a function call that I can call in the
> shell, like as part of a  print, that I do the selection, and when I
> press ok on the window, the window closes and the print completes.
>
>   Thanks to any pointer,
>
> --
> -Angel Asencio
> The MITRE corp.
> asencio at mitre.org




More information about the Python-list mailing list