Destroy Modal Dialog Programmatically

942 views
Skip to first unread message

dubiboy

unread,
Jun 10, 2010, 8:44:32 AM6/10/10
to wxPython-users
I am waiting for an answer on a UDP socket with a specified timeout.

During this time I want a dialog box to appear with something like
"Please wait or else click Cancel".

The user can either wait for the UDP response or timeout, in which
case the dialog
should be programmatically destroyed, or else could press Cancel and
destroy the dialog.

I can't work out how to combine these two tasks.

Thanks

David

Cody Precord

unread,
Jun 10, 2010, 8:49:27 AM6/10/10
to wxpytho...@googlegroups.com
Hi,

When the socket times out just call Dialog.Destroy() to destroy the
dialog. (or are you stuck on something else?)


Cody

Mike Driscoll

unread,
Jun 10, 2010, 9:49:45 AM6/10/10
to wxPython-users


On Jun 10, 7:49 am, Cody Precord <codyprec...@gmail.com> wrote:
> Hi,
>
Maybe he's blocking the dialog's UI so the cancel button won't work
while he waits for the socket to timeout. If so, then he'll need to
look at http://wiki.wxpython.org/LongRunningTasks or
http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Robin Dunn

unread,
Jun 10, 2010, 12:37:21 PM6/10/10
to wxpytho...@googlegroups.com

A bit cleaner way to handle it would be to call
dialog.EndModal(someValue), then the code that called ShowModal can get
someVlaue instead of the button ID and will be able to tell why the
modal dialog was ended.

--
Robin Dunn
Software Craftsman
http://wxPython.org

dubiboy

unread,
Jul 13, 2010, 9:57:15 AM7/13/10
to wxPython-users
Thanks all for the suggestions, however nothing is really working, as
below:

The (major relevant) steps I am doing are:

1. From within my notebook page class I create a MessageDialog (call
it self.dlg)
2. Then create a thread object and pass it self.dlg as a parameter.
3. do "print self.dlg" from within my notebook page class (main gui
app) and also
from within the thread just to confirm that both reference the same
object.
They do!
4. do ShowModal() from within main gui - the dialog is now shown.
5. within thread and after a few seconds do
self.dlg.EndModal(wx.CANCEL) but get error message
to the effect that the dialog is not modal.
6. Alternatively to step 5, from within thread do self.dlg.Destroy()
with NO effect.

Any ideas???

On Jun 10, 7:37 pm, Robin Dunn <ro...@alldunn.com> wrote:
> On 6/10/10 5:49 AM, Cody Precord wrote:
>
>
>
> > Hi,
>

Cody Precord

unread,
Jul 13, 2010, 10:09:48 AM7/13/10
to wxpytho...@googlegroups.com
Hi,

On Tue, Jul 13, 2010 at 8:57 AM, dubiboy <david...@gmail.com> wrote:
> Thanks all for the suggestions, however nothing is really working, as
> below:
>
> The (major relevant) steps I am doing are:
>
> 1. From within my notebook page class I create a MessageDialog (call
> it self.dlg)
> 2. Then create a thread object and pass it self.dlg as a parameter.
> 3. do "print self.dlg" from within my notebook page class (main gui
> app) and also
> from within the thread just to confirm that both reference the same
> object.
> They do!
> 4. do ShowModal() from within main gui - the dialog is now shown.
> 5. within thread and after a few seconds do
> self.dlg.EndModal(wx.CANCEL) but get error message
> to the effect that the dialog is not modal.
> 6. Alternatively to step 5, from within thread do self.dlg.Destroy()
> with NO effect.
>
> Any ideas???

First please read this http://www.caliburn.nl/topposting.html

You can't make direct GUI calls from a background thread. You need to
post a message back to the main GUI thread and handle the UI changes
in the context of the main thread.

If that doesn't solve the issue then will probably need to see some
sample code that is giving you the problem
(http://wiki.wxpython.org/MakingSampleApps)


Cody

dubiboy

unread,
Jul 13, 2010, 11:38:07 AM7/13/10
to wxPython-users
Hi,
Bottom posting point taken ...

On Jul 13, 5:09 pm, Cody Precord <codyprec...@gmail.com> wrote:
> Hi,
>
>
>
> On Tue, Jul 13, 2010 at 8:57 AM, dubiboy <david.we...@gmail.com> wrote:
> > Thanks all for the suggestions, however nothing is really working, as
> > below:
>
> > The (major relevant) steps I am doing are:
>
> > 1. From within my notebook page class I create a MessageDialog (call
> > it self.dlg)
> > 2. Then create a thread object and pass it self.dlg as a parameter.
> > 3. do "print self.dlg" from within my notebook page class (main gui
> > app) and also
> > from within the thread just to confirm that both reference the same
> > object.
> > They do!
> > 4. do ShowModal() from within main gui - the dialog is now shown.
> > 5. within thread and after a few seconds do
> > self.dlg.EndModal(wx.CANCEL) but get error message
> > to the effect that the dialog is not modal.
> > 6. Alternatively to step 5, from within thread do self.dlg.Destroy()
> > with NO effect.
>
> > Any ideas???
>
> First please read thishttp://www.caliburn.nl/topposting.html
>
> You can't make direct GUI calls from a background thread. You need to
> post a message back to the main GUI thread and handle the UI changes
> in the context of the main thread.
>
> If that doesn't solve the issue then will probably need to see some
> sample code that is giving you the problem
> (http://wiki.wxpython.org/MakingSampleApps)
>
> Cody

I would like to attach a sample file, but after having spent 1/2 hour
trying (including viewing google groups help)
I have almost given up. Any easy way to attach file? I cannot find any
buttons with Link or Attach File, in addition
the PAGES pages does not seem to have a way to make a new page.
Thanks for any help.

Cody Precord

unread,
Jul 13, 2010, 11:42:14 AM7/13/10
to wxpytho...@googlegroups.com
Hi,

On Tue, Jul 13, 2010 at 10:38 AM, dubiboy <david...@gmail.com> wrote:
> Hi,


>>
>> You can't make direct GUI calls from a background thread. You need to
>> post a message back to the main GUI thread and handle the UI changes
>> in the context of the main thread.
>>
>> If that doesn't solve the issue then will probably need to see some
>> sample code that is giving you the problem
>> (http://wiki.wxpython.org/MakingSampleApps)
>>
>> Cody
>
> I would like to attach a sample file, but after having spent 1/2 hour
> trying (including viewing google groups help)
> I have almost given up. Any easy way to attach file? I cannot find any
> buttons with Link or Attach File, in addition
> the PAGES pages does not seem to have a way to make a new page.
> Thanks for any help.

This is an mailing list you can send email to it, no need to use the
web interface.

wxpytho...@googlegroups.com


Cody

david wende

unread,
Jul 13, 2010, 12:44:33 PM7/13/10
to wxpytho...@googlegroups.com
Hi,

Here is the sample code.
Just run the gui5.py

My comments in pagebucket.py explain where the dialog was supposed to be destroyed.

Thanks for the help


pagebucket.py
gui5.py

WinCrazy

unread,
Jul 13, 2010, 12:50:54 PM7/13/10
to wxPython-users
To attach any file you must send a regular email with your file(s)
attached.

Set the subject line to be the exact title of the particular thread
you want it to show up in, e.g.:

Destroy Modal Dialog Programmatically

The recipient of the email is:

wxpytho...@googlegroups.com

It's a shame that these instructions aren't shown on every single
thread page in an obvious manner.

Ray Pasco

Robin Dunn

unread,
Jul 13, 2010, 1:41:03 PM7/13/10
to wxpytho...@googlegroups.com
On 7/13/10 9:50 AM, WinCrazy wrote:
>
> It's a shame that these instructions aren't shown on every single
> thread page in an obvious manner.

Probably not obvious enough but it is mentioned on the main group page:
http://groups.google.com/group/wxpython-users

"""However if you would like to send sample source code along with your
questions please use email and attach the code to the message instead of
just pasting the code into the message box in the web interface."""

Robin Dunn

unread,
Jul 13, 2010, 1:41:58 PM7/13/10
to wxpytho...@googlegroups.com
On 7/13/10 9:44 AM, david wende wrote:
> Hi,
>
> Here is the sample code.
> Just run the gui5.py
>
> My comments in pagebucket.py explain where the dialog was supposed to be
> destroyed.

Two problems:

1. When you use pubsub to send messages from a worker thread the message
is received in the context of the worker thread, not the GUI thread. So
you'll still need to do something to pass control over to the GUI thread
in order to operate on the dialog. wx.CallAfter works well.

2. Since wx.MessageDialog is not a real wx.Dialog (it's just a wwrapper
around the system's stock message dialog APIs) it doesn't respond to
EndZModal or Destroy like you would expect. If you want to
programatically close it you'll need to derive your own class from
wx.Dialog to do it.

3. Once you do #2 then using EndModal will be the correct method to use.

dubiboy

unread,
Jul 14, 2010, 6:16:41 AM7/14/10
to wxPython-users
Hi,
Robin - I subclassed wx.Dialog and also used CallAfter. It's mostly
working now.
One (hopefully last) issue.
When I close the dialog NON-programmatically, i.e. with the Cancel
button that I
created manually in my class, I get a return code of 5101. Where does
this number come from?

Thanks a lot

Steven Sproat

unread,
Jul 14, 2010, 8:38:41 AM7/14/10
to wxpytho...@googlegroups.com
> --

steven@dm-steven:~$ python
Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
>>> wx.ID_CANCEL
5101
>>>

dubiboy

unread,
Jul 14, 2010, 9:22:19 AM7/14/10
to wxPython-users
PSB

On Jul 14, 3:38 pm, Steven Sproat <spro...@gmail.com> wrote:
Thanks Steven
Am I embarrassed! This should have been obvious to me.

Robin Dunn

unread,
Jul 14, 2010, 12:15:25 PM7/14/10
to wxpytho...@googlegroups.com

It's the value passed to EndModal, and by common convention the ID of
the button used to close the dialog is the value that is used. Support
for handling wx.ID_OK and wx.ID_CANCEL buttons in dialogs is built-in.

Reply all
Reply to author
Forward
0 new messages