Simple message dialog GUI

Message
All,

I am new to wxPython so please bear with me for asking perhaps dumb questions…

At present I only need simple message box type dialogs and assumed these would be readily available, but after several hours searching the web… it seems not ?

I did find a module called easygui.py, and this is almost perfect… well except it does not look like windows, but it’s close.

From what I have found thus far, it does seem there is not much to it, though to be honest I still don’t fully understand what everything does. But at this point I don’t need to know, so that’s OK.

···

this works

from wxPython.wx import *

application = wxPySimpleApp() # Why is this needed ?

def Info(Message="", Title=“Information”):
“”“A simple information message”""
dlg = wxMessageDialog(None, Message, Title,
wxOK | wxICON_INFORMATION
)
dlg.ShowModal()
dlg.Destroy() # This appears NOT to be needed ?

function test

Info(“Look, matey, I know a dead parrot when I see one…”)

So I could develop a small module with the needed functions pretty quickly. But I ask myself, am I reinventing the wheel, someone out there must have “Been there, done that”. Have I just been looking in the wrong places or does such a module or class really not exist ?

Thanks,

Robert

Robert Adams wrote:

The function you're looking for is "wx.MessageBox":

    import wx

    def Info(message = "", title = "Information"):
      """A simple information message"""
      wx.MessageBox(message, title, wx.OK | wx.ICON_INFORMATION)

    # function test
    Info("Look, matey, I know a dead parrot when I see one...")

(You could just use wx.MessageBox directly, except it defaults to a
title of "Message" and no ICON_INFORMATION.)

Here's some other comments on the code you posted:

#
# this works
#
from wxPython.wx import *

It's preferred to just "import wx", and then use "wx.MessageDialog" and
"wx.OK" instead of "wxMessageDialog" and "wxOK", etc. The wxPython
syntax is older, and being gradually phased out.

application = wxPySimpleApp() # Why is this needed ?

It's needed because every wxWidgets application needs an application
object, and it has to be created before doing much else. There's really
not much more you need to know than that.

def Info(Message="", Title="Information"):
    """A simple information message"""
    dlg = wxMessageDialog(None, Message, Title,
                          wxOK | wxICON_INFORMATION
                          )
    dlg.ShowModal()
    dlg.Destroy() # This appears NOT to be needed ?

The destroy function will basically get rid of the underlying Windows
(or Gtk, or whatever) objects that the wxMessageDialog is using. Try this:

   print "Test 1"
   dlg.ShowModal()
   print "Test 2"
   dlg.ShowModal()
   dlg.Destroy()
   print "Test 3"
   dlg.ShowModal()

It should show the dialog twice (because after it comes back from the
first ShowModal, the dialog still exists, it just isn't being shown) and
then do nothing after printing "Test 3", because the dialog has been
deleted.

You mainly only have to worry about this if you're writing a complicated
application where leaving the dialog objects in existance when they're
not being used would use up too much memory. This pretty much never
happens in Python (it's more of a factor in C++), so you're safe just
ignoring it until it's time to optimize your app.

# function test
Info("Look, matey, I know a dead parrot when I see one...")

So I could develop a small module with the needed functions pretty quickly.
But I ask myself, am I reinventing the wheel, someone out there must have
"Been there, done that". Have I just been looking in the wrong places or
does such a module or class really not exist ?

wx.MessageBox is documented at
http://www.wxwidgets.org/manuals/2.6.3/wx_dialogfunctions.html#wxmessagebox

That page has a lot of other convenience functions for dialogs, too.

Joe

Joe Mason wrote:

Robert Adams wrote:

    dlg.Destroy() # This appears NOT to be needed ?

The destroy function will basically get rid of the underlying Windows
(or Gtk, or whatever) objects that the wxMessageDialog is using. Try this:

   print "Test 1"
   dlg.ShowModal()
   print "Test 2"
   dlg.ShowModal()
   dlg.Destroy()
   print "Test 3"
   dlg.ShowModal()

It should show the dialog twice (because after it comes back from the
first ShowModal, the dialog still exists, it just isn't being shown) and
then do nothing after printing "Test 3", because the dialog has been
deleted.

You mainly only have to worry about this if you're writing a complicated
application where leaving the dialog objects in existance when they're
not being used would use up too much memory. This pretty much never
happens in Python (it's more of a factor in C++), so you're safe just
ignoring it until it's time to optimize your app.

You should always destroy your dialogs. Not necessarily immediately, but at least before you want the app to exit. This is because MainLoop doesn't exit until there are no more top-level windows in existence, so keeping some undestroyed dialogs around could lead to your app not terminating when the main frame(s) is closed. Standard common dialogs (wx.MessageDialog, wx.FileDialog, etc.) can be an exception to this since they are usually implemented as wrappers around a platform API and not a real wx.Dialog, but it is still a good idea to destroy them as well since the native API usage could be different on various platforms and can change over time.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!