wxwindows event function arguments

L

lotmr

I have a windows launch bar application that sits in the system tray.
What I want to do is when i click on a button in the launch menu, it
calls the event which then calls 'OnLaunch('path')' this does not seem
possible. When I change 'OnLaunch(self, event)' to 'OnLaunch(self,
event, path)' it says I dont have enough arguments. When I remove
'event', the OnLaunch runs as soon as the program loads. Using Google I
can absolutly not find a way to pass arguments to a function called by
a event. If indeed it is absolutly impossible, what could be the best
alternate possibility?

Any help would be appreciated,
Nils Lundquist

CODE:

from wxPython.wx import *
import os, sys
class MainWindow(wxFrame):
def OnAbout (self, event):
m = wxMessageDialog( self, " LUNCH Bar 0.1 \n"
" A launch bar written in Python using
wxWindows","About LUNCH", wxOK)
m.ShowModal()
m.Destroy
def OnExit (self, event):
self.tbi.Destroy()
sys.exit()
def OnTrayRight (self, event):
self.PopupMenu(self.rhtmenu)
def OnLaunch (self, event):
print event.GetString()
path = 'D:\Program Files\Mozilla Firefox\Firefox.exe'
os.startfile(path)
def __init__(self,parent,id,title):
wxID_FIREFOX = 1117
wxFrame.__init__(self,parent,wxID_ANY, title, size = (
200,100),
style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE)
self.tbi = wxTaskBarIcon()
icon = wxIcon('package.ico',wxBITMAP_TYPE_ICO)
self.tbi.SetIcon(icon, '')
self.rhtmenu = wxMenu()
icon = wxBitmap(name = 'ffico.png', type = wxBITMAP_TYPE_PNG)
item = wxMenuItem(parentMenu = self.rhtmenu, id = wxID_FIREFOX,
text='Firefox')
item.SetBitmap(icon)
self.rhtmenu.Append(wxID_EXIT, "&Exit"," Terminate this program")
self.rhtmenu.Append(wxID_ABOUT, "&About"," Information about this
program")
self.rhtmenu.AppendSeparator()
self.rhtmenu.AppendItem(item)
EVT_TASKBAR_RIGHT_UP(self.tbi, self.OnTrayRight)
EVT_MENU(self, wxID_ABOUT, self.OnAbout)
EVT_MENU(self, wxID_EXIT, self.OnExit)
EVT_MENU(self, wxID_FIREFOX, self.OnLaunch)


appwin = wxPySimpleApp()
frame = MainWindow(None, -1, "Small editor")
appwin.MainLoop()
 
G

Greg Krohn

lotmr said:
I have a windows launch bar application that sits in the system tray.
What I want to do is when i click on a button in the launch menu, it
calls the event which then calls 'OnLaunch('path')' this does not seem
possible. When I change 'OnLaunch(self, event)' to 'OnLaunch(self,
event, path)' it says I dont have enough arguments. When I remove
'event', the OnLaunch runs as soon as the program loads. Using Google I
can absolutly not find a way to pass arguments to a function called by
a event. If indeed it is absolutly impossible, what could be the best
alternate possibility?

One possible solution is to curry your event handler. This means that you
already supply the third argument (i.e. path) so that when wxWidgets
call the handler it only needs to supply the first two (i.e self and
event). If that's confusing see
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52549 for more
info. So basically this is what you do. Put the following code before
your MainWindow class:

class curry:
def __init__(self, fun, *args, **kwargs):
self.fun = fun
self.pending = args[:]
self.kwargs = kwargs.copy()

def __call__(self, *args, **kwargs):
if kwargs and self.kwargs:
kw = self.kwargs.copy()
kw.update(kwargs)
else:
kw = kwargs or self.kwargs

return self.fun(*(self.pending + args), **kw)

Then, when you bind your event, do it like this:

EVT_MENU(self, wxID_FIREFOX, curry(self.OnLaunch, path='D:\Program Files\Mozilla Firefox\Firefox.exe'))

Then change your OnLaunch handler back to the way you originally wanted it:

def OnLaunch (self, event, path):
print event.GetString()
os.startfile(path)


Hope this helps,
greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top