#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    4
    Rep Power
    0

    wxPython:Automatic generation of menu


    I am currently working on my first GUI, using wxPython, and would like to implement an idea but I'm not sure how to proceed.

    In addition to the basic functions of the GUI I have a number of little scripts in a subdirectory that do some simple tasks. The idea is that users can add any new scripts to the directory and then run them from the GUI. To do this the GUI needs to automatically build a menu and then associate each entry with running the script in question. I can easily build the menu but I haven't a clue how to then run the scripts. Can anyone help or is this not a practical idea?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    4
    Rep Power
    0

    solved


    It's OK. Problem solved.
  4. #3
  5. I <3 ASCII
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Posts
    2,399
    Rep Power
    1232
    How was it solved?

    (It might be solved for you but it's always nice to post how you got it done incase someone with the same question searches for the same thing)

    -MBirchmeier
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    4
    Rep Power
    0
    Run a loop through the list of macros which generates a new ID for each macro, creates the menu entry, binds it to an event/function then adds the macro name to a dictionary with the ID as a key.

    Code:
    self.MacroDict= {} 
    
    menu5 = wxMenu()	
    for macro in self.MacroList:
         ID = wxNewId()  
         menu5.Append(ID,macro) 
         self.Bind(wx.EVT_MENU, self.OnExecuteMacro,id=ID) 
         self.MacroDict[ID]= macro 		
    menubar.Append(menu5,"UserMacros")
    The function then extracts the ID of the event that triggered it and uses that to obtain the macro name from the dictionary and passes it to the command line.

    Code:
    def OnExecuteMacro(self, event):
         eID = event.GetId()
         MacroName = os.path.join(self.UserMacroDirectory,self.MacroDict[eID])
         os.system('python '+MacroName)

IMN logo majestic logo threadwatch logo seochat tools logo