wxPython issues

F

Francesco

Hello,

I have some questions:

1.
this is in Mainthread:
def OnOverwriteDlg(self, event):
print "OnOverwriteDlg"
dlg = COverwriteDlg ()
dlg.m_strInfo = g_strTitle
g_nGuiResult = dlg.ShowModal()
??? g_objGUISyncSemaphore.release ()
(not working)

that is in worker thread:
print "DlgOverwriteThreadsafe"
g_strTitle = _("Overwrite %s ?") % strFile

event = wxCommandEvent ()
event.SetEventType (wxEVT_COMMAND_BUTTON_CLICKED)
event.SetId (19999)
pFrm = wxGetApp().GetFrame()
if(pFrm):
wxPostEvent(pFrm, event)

#wxpython: do not use this
????#wxYield()
#//wait for GUI to end with owerwrite dialog

print "before"
???? g_objGUISyncSemaphore.acquire()
print "after"
print g_nGuiResult
return g_nGuiResult

How can I get, that in worker thread on "before" is waiting, until the
dialog in mainthread is finished?


wxYield, Semaphore, Mutex, ... is all a bit strange for me.
Is there any website dedicated to this issues?

2. I want to get a list icons from the windows shell folders and put
it in the wxPython List control. How can I accomplish this?

3.
menuAdd(self, pMenuFile, _("Exit") +"\tAlt+F4", _("Exit"),
self.OnClose, wxID_CLOSE)
def OnClose(self,event):
#store startup preferences
the onclose is called by ending the program via menu or Alt-F4.
but if i quit the program with the mouse right above, the onclose will
not be called.

Many thanks in advance,
 
J

Josiah Carlson

#wxpython: do not use this
????#wxYield()
#//wait for GUI to end with owerwrite dialog

wxYield only makes sense when called from the main thread. The main
thread should be the only thread that is handling GUI events.


What I would do is:
def OnOverwriteDlg(self, event):
print "OnOverwriteDlg"
dlg = COverwriteDlg ()
dlg.m_strInfo = g_strTitle
g_nGuiResult = dlg.ShowModal()
print g_nGuiResult
return g_nGuiResult

Toss the semaphores, toss the worker threads, etc. If saving takes so
much time, then you can do what I describe farther down.

How can I get, that in worker thread on "before" is waiting, until the
dialog in mainthread is finished?

Why are you trying to use multiple threads to save files?

wxYield, Semaphore, Mutex, ... is all a bit strange for me.
Is there any website dedicated to this issues?

wxYield is normally used when programming a single-threaded application.
If your application can spend a long time on one task without
returning, and you want to allow the GUI to update while it is
processing, calling wxYield() is a good idea.

For example, you can have a status bar that reflects a percent complete,
without requiring a second thread to do the processing.

def LongRunningMethod(self, event):
for i in xrange(101):
self.SetStatusText("%s%% complete"%i)
wxYield()
time.sleep(.1)


wxMutex and wxSemaphore are really just ways of only allowing one thread
(or some certain count) to modify some structure at one time.

def updateglobal_m(count, wxmutex):
global counter
for i in xrange(count):
wxmutex.Lock()
counter += 1
wxmutex.Unlock()

def updateglobal_s(count, wxsemaphore):
global counter
for i in xrange(count):
wxsemaphore.Wait()
counter += 1
wxsemaphore.Post()

The standard Python Semaphore and Mutex calls are very similar...

def updateglobal_pys(count, pysemaphore):
global counter
for i in xrange(count):
pysemaphore.acquire()
counter += 1
pysemaphore.release()

But a Python mutex has a few different options that you can read about
in the standard documentation.

2. I want to get a list icons from the windows shell folders and put
it in the wxPython List control. How can I accomplish this?

I don't know how to get arbitrary icons from executables or dlls.
Perhaps someone else can tell us.

3.
menuAdd(self, pMenuFile, _("Exit") +"\tAlt+F4", _("Exit"),
self.OnClose, wxID_CLOSE)

Change the above to:
menuAdd(self, pMenuFile, _("Exit") +"\tAlt+F4", _("Exit"), self.OnClose,
wxNewId())

Then in the __init__ method use:
EVT_CLOSE(self, self.OnClose)



- Josiah
 
F

Francesco

On Sun, 01 Feb 2004 16:10:37 -0800, Josiah Carlson

Hello Josiah,

thank you for your answer,
wxYield only makes sense when called from the main thread. The main
thread should be the only thread that is handling GUI events.

Aha; the original program used this in a worker thread.
What I would do is:
def OnOverwriteDlg(self, event):
print "OnOverwriteDlg"
dlg = COverwriteDlg ()
dlg.m_strInfo = g_strTitle
g_nGuiResult = dlg.ShowModal()
print g_nGuiResult
return g_nGuiResult

Toss the semaphores, toss the worker threads, etc. If saving takes so
much time, then you can do what I describe farther down.

No, the thing is:

It is a (still primitive) filemanager, where copy should be run in a
worker thread
in background (you can copy 1 GB file), and you should be able to work
with the
program without waiting

Thanks for this clarification

regards,
 
J

Josiah Carlson

No, the thing is:
It is a (still primitive) filemanager, where copy should be run in a
worker thread
in background (you can copy 1 GB file), and you should be able to work
with the
program without waiting

I see. Well then, here is a version that doesn't have all the details,
but it will properly handle working with and shutting down while dealing
with one working thread.


import Queue
import threading

tocopy = Queue.Queue()
working = threading.Lock()


def workerthread():
frame = wxGetApp().GetFrame()
while 1:
g_nGuiResult = tocopy.get()
if working.acquire(0):
#do whatever you were supposed to do
wxPostEvent(frame, <notification of operation complete>)
else:
#we are supposed to shut down
return

threading.Thread(target=workerthread).start()


def OnOverwriteDlg(self, event):
print "OnOverwriteDlg"
dlg = COverwriteDlg ()
dlg.m_strInfo = g_strTitle
g_nGuiResult = dlg.ShowModal()
print g_nGuiResult
if a copy were supposed to happen:
tocopy.put(g_nGuiResult)

def OnClose(self, event):
#save startup preferences
# show a status update stating that we are waiting for the
# worker thread to finish copying
wxYield() #so that the status update is actually shown
working.acquire() #get the working lock so that the worker
#thread is notified that it should quit
tocopy.put(None) #to wake up the worker thread if necessary
 
F

Francesco

On Mon, 02 Feb 2004 15:38:59 -0800, Josiah Carlson


Hello Josiah,
[snip]

thank you again for your valuable Information!

regards
 
J

Josiah Carlson

No, the thing is:
[snip]

thank you again for your valuable Information!

No problem. I've spent more time that I'd like to admit playing with
threads. I do enjoy playing with wxPython though...it is fun.

- Josiah

P.S. If you are looking for a good editor for Python, I wrote one:
http://pype.sourceforge.net
 
J

Josiah Carlson

PS: yes, it is; you know me from sf :)

I suspected it was you, but I can't find any record of you emailing me
directly, and didn't want to jump to conclusions.

Good to see you developing.

- Josiah
 

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
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top