473,387 Members | 3,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Creating custom event in WxPython

Hello,

I'm faced with the following problem: I have a (secondary) thread that
monitors a socket for incoming message traffic using the
select.select() function.

Besides that I also have the main thread of my WxPython application. So
far so good.

Now when my socket thread detects an incoming message, I need my main
thread to interpret the message and react to it by updating the GUI.
IMO the best way to achieve this is by having my socket thread send a
custom event to my application's event loop for the main thread to
process. However, I'm at a total loss as far as creating custom events
is concerned. The WxWindows documentation isn't very helpful on this
either.
I think I need to derive my own event class from the wxEvent class, but
I have no idea HOW (i.e. what methods do I need to override, etc.) Can
anyone help me?

Sep 1 '05 #1
4 6524
<Nu****@gmx.net> wrote:
Now when my socket thread detects an incoming message, I need my main
thread to interpret the message and react to it by updating the GUI.
IMO the best way to achieve this is by having my socket thread send a
custom event to my application's event loop for the main thread to
process. However, I'm at a total loss as far as creating custom events
is concerned. The WxWindows documentation isn't very helpful on this
either.
I think I need to derive my own event class from the wxEvent class, but
I have no idea HOW (i.e. what methods do I need to override, etc.) Can
anyone help me?


(a) Consider whether you can do what you want with CallAfter().

(b) If you do have to go with a custom event, the idiom I've got here
is:

EVT_CUSTOM_WITH_DATA_ID = wxNewId()

def EVT_CUSTOM_WITH_DATA(win, func):
win.Connect(-1, -1, EVT_CUSTOM_WITH_DATA_ID, func)

class CustomWithDataEvent(wxPyEvent):
def __init__(self, data=None):
wxPyEvent.__init__(self)
self.data = data
self.SetEventType(EVT_CUSTOM_WITH_DATA_ID)

def Clone(self):
return CustomWithDataEvent(self.data)

but do note that this is for wxPython2.4 -- amongst other differences,
EVT_CUSTOM_WITH_DATA() should be unnecessary in 2.6 (used Bind()
instead).

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Sep 1 '05 #2
Nu****@gmx.net a écrit :
Now when my socket thread detects an incoming message, I need my main
thread to interpret the message and react to it by updating the GUI.
IMO the best way to achieve this is by having my socket thread send a
custom event to my application's event loop for the main thread to
process. However, I'm at a total loss as far as creating custom events
is concerned. The WxWindows documentation isn't very helpful on this
either.


I think this is what you're looking for:

# begin code

import wx

wxEVT_INVOKE = wx.NewEventType()

class InvokeEvent(wx.PyEvent):
def __init__(self, func, args, kwargs):
wx.PyEvent.__init__(self)
self.SetEventType(wxEVT_INVOKE)
self.__func = func
self.__args = args
self.__kwargs = kwargs

def invoke(self):
self.__func(*self.__args, **self.__kwargs)

class MyFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.Connect(-1, -1, wxEVT_INVOKE, self.onInvoke)

def onInvoke(self, evt):
evt.invoke()

def invokeLater(self, func, *args, **kwargs):
self.GetEventHandler().AddPendingEvent(InvokeEvent (func, args,
kwargs))

# end code

This way, if frm is an instance of MyFrame, invoking
frm.invokeLater(somecallable, arguments...) will invoke 'somecallable'
with the specified arguments in the main GUI thread.

I found this idiom somewhere on the Web and can't remember where.

HTH
Sep 1 '05 #3
Cool, thanks a bunch!

Sep 8 '05 #4
You should be derriving from PyCommandEvent since only CommandEvents are set
to propegate, which is probably what you want (see the wxwidgets Event
handling overview).

In order to use Bind you will need an instance of PyEventBinder. For the
example below that would be something like:

EVT_INVOKE = PyEventBinder(wxEVT_INVOKE)

-Chris
On Thu, Sep 01, 2005 at 04:25:05PM +0200, fraca7 wrote:
Nu****@gmx.net a ?crit :
Now when my socket thread detects an incoming message, I need my main
thread to interpret the message and react to it by updating the GUI.
IMO the best way to achieve this is by having my socket thread send a
custom event to my application's event loop for the main thread to
process. However, I'm at a total loss as far as creating custom events
is concerned. The WxWindows documentation isn't very helpful on this
either.


I think this is what you're looking for:

# begin code

import wx

wxEVT_INVOKE = wx.NewEventType()

class InvokeEvent(wx.PyEvent):
def __init__(self, func, args, kwargs):
wx.PyEvent.__init__(self)
self.SetEventType(wxEVT_INVOKE)
self.__func = func
self.__args = args
self.__kwargs = kwargs

def invoke(self):
self.__func(*self.__args, **self.__kwargs)

class MyFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.Connect(-1, -1, wxEVT_INVOKE, self.onInvoke)

def onInvoke(self, evt):
evt.invoke()

def invokeLater(self, func, *args, **kwargs):
self.GetEventHandler().AddPendingEvent(InvokeEvent (func, args,
kwargs))

# end code

This way, if frm is an instance of MyFrame, invoking
frm.invokeLater(somecallable, arguments...) will invoke 'somecallable'
with the specified arguments in the main GUI thread.

I found this idiom somewhere on the Web and can't remember where.

HTH
--
http://mail.python.org/mailman/listinfo/python-list

Sep 8 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: wang xiaoyu | last post by:
Hello: i want use activex in wxpython program,but when i use MakeActiveXClass an exception occurs. this is my source code dealing the DICOM ocx.I must note that in this program "hwtxcontrol" is...
3
by: Robert | last post by:
Hello list, could somebody point me to a good reference about wxPython event handling? I have seen many examples but which one is the best. Waht are the advantages and disadvantages? Can you...
3
by: Lo?c Mah? | last post by:
Hello I try to use a Validator for a TxtCtrl placed in a Panel with a Button in order to trigger the Validator and test the content of TxtCtrl. I have looked into wxPython documentation and...
1
by: Ivan Voras | last post by:
Does wxPython (actually, the wx toolkit) support setting up ListCtrls or similar to allow custom element drawing? Something like generating an 'OnPaint()' event in which I could paint whatever I...
1
by: T | last post by:
I have a web site i want to deploy using a VS.NEt set up project. I have no problems with that, that is fine and works no problem. But the web application uses a custom event log to log...
1
by: John F. | last post by:
I want to write a client app in Python using wxWindows that connects to my FreeBSD server via SSH (using my machine account credentials) and runs a python or shell script when requested (by...
3
by: clsmith66 | last post by:
I hope this is just something stupid I'm missing and someone can easily point out my error. I'm trying to do a few turorials on windows services, and the couple I found so far just create an event...
2
by: Kevin Walzer | last post by:
I'm porting a Tkinter application to wxPython and had a question about wxPython's event loop. The Tkinter app provides a GUI to a command-line tool. It gathers user input, and opens an...
5
by: Joe P. Cool | last post by:
So far I have a little experience with Tkinter and wxPython. I wonder which of the numerous Python GUI kits would be the best choice for a multi platform application that makes heavy use of custom...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.