Added wxPython.lib.newevent from Miki Tebeka

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_4_BRANCH@23872 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2003-09-24 00:37:40 +00:00
parent 5e899a81d9
commit 3a78892d31
2 changed files with 127 additions and 2 deletions

View File

@@ -2,13 +2,16 @@ CHANGES.txt for wxPython
----------------------------------------------------------------------
2.4.1.x
2.4.2.1
-------
Use wxSTC in the demo for displaying the soucre code of the samples.
Updated to Scintilla 1.53.
Updated to Scintilla 1.54.
Lots of bug fixes and such from the wxWindows folks.
Added wxPython.lib.newevent from Miki Tebeka

View File

@@ -0,0 +1,122 @@
#!/usr/bin/env python
"""Easy generation of new events classes and binder functions"""
__author__ = "Miki Tebeka <tebeka@cs.bgu.ac.il>"
import wx
#---------------------------------------------------------------------------
def NewEvent():
"""Generate new (Event, Binder) tuple
e.g. MooEvent, EVT_MOO = NewEvent()
"""
evttype = wx.NewEventType()
class _Event(wx.PyEvent):
def __init__(self, **kw):
wx.PyEvent.__init__(self)
self.SetEventType(evttype)
self.__dict__.update(kw)
def Binder(win, func):
win.Connect(-1, -1, evttype, func)
return _Event, Binder
def NewCommandEvent():
"""Generate new (CmdEvent, Binder) tuple
e.g. MooCmdEvent, EVT_MOO = NewCommandEvent()
"""
evttype = wx.NewEventType()
class _Event(wx.PyCommandEvent):
def __init__(self, id, **kw):
wx.PyCommandEvent.__init__(self, evttype, id)
self.__dict__.update(kw)
def Binder(win, id, func):
win.Connect(id, -1, evttype, func)
return _Event, Binder
#---------------------------------------------------------------------------
def _test():
"""A little smoke test"""
from threading import Thread
from time import sleep
MooEvent, EVT_MOO = NewEvent()
GooEvent, EVT_GOO = NewCommandEvent()
DELAY = 0.7
def evt_thr(win):
sleep(DELAY)
wx.PostEvent(win, MooEvent(moo=1))
def cmd_thr(win, id):
sleep(DELAY)
wx.PostEvent(win, GooEvent(id, goo=id))
ID_CMD1 = wx.NewId()
ID_CMD2 = wx.NewId()
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "MOO")
sizer = wx.BoxSizer(wx.VERTICAL)
EVT_MOO(self, self.on_moo)
b = wx.Button(self, -1, "Generate MOO")
sizer.Add(b, 1, wx.EXPAND)
wx.EVT_BUTTON(self, b.GetId(), self.on_evt_click)
b = wx.Button(self, ID_CMD1, "Generate GOO with %d" % ID_CMD1)
sizer.Add(b, 1, wx.EXPAND)
wx.EVT_BUTTON(self, ID_CMD1, self.on_cmd_click)
b = wx.Button(self, ID_CMD2, "Generate GOO with %d" % ID_CMD2)
sizer.Add(b, 1, wx.EXPAND)
wx.EVT_BUTTON(self, ID_CMD2, self.on_cmd_click)
EVT_GOO(self, ID_CMD1, self.on_cmd1)
EVT_GOO(self, ID_CMD2, self.on_cmd2)
self.SetSizer(sizer)
self.SetAutoLayout(True)
sizer.Fit(self)
def on_evt_click(self, e):
t = Thread(target=evt_thr, args=(self, ))
t.setDaemon(True)
t.start()
def on_cmd_click(self, e):
t = Thread(target=cmd_thr, args=(self, e.GetId()))
t.setDaemon(True)
t.start()
def show(self, msg, title):
dlg = wx.MessageDialog(self, msg, title, wx.OK)
dlg.ShowModal()
dlg.Destroy()
def on_moo(self, e):
self.show("MOO = %s" % e.moo, "Got Moo")
def on_cmd1(self, e):
self.show("goo = %s" % e.goo, "Got Goo (cmd1)")
def on_cmd2(self, e):
self.show("goo = %s" % e.goo, "Got Goo (cmd2)")
app = wx.PySimpleApp()
f = Frame()
f.Show(True)
app.MainLoop()
if __name__ == "__main__":
_test()