Andrea and Cody-
I've gotten this working. Thanks for the help!!
I have two residual questions:
1. I'm surprised that it takes so much code to create and manage a
dialog box with Yes and No buttons. Is my code unnecessarily verbose,
or is this typical for wx?
2. When the program finishes, instead of shutting down nicely, Python
crashes, which is not very elegant. I suspect that I'm doing
something wrong.
Any pointers will be appreciated.
Phillip
--Start of yes_no_demo.py--
# This Python script demonstrates how wxPython can be used to display
a dialog
# box that presents the user with a yes-or-no choice.
import MyMessageDialog, wx
import wx.lib.agw.genericmessagedialog as GMD
# Store `GenericMessageDialog` or `MyMessageDialog` in
`MessageDialog`:
# MessageDialog= GMD.GenericMessageDialog
MessageDialog= MyMessageDialog.MyMessageDialog
class MyApp(wx.App):
def OnInit(self):
frame= wx.Frame(None, -1, "Yes_no_demo")
# frame.Show(True)
self.SetTopWindow(frame)
return_code= self.get_yes_or_no(
message="Do you want to play 'Guess a number'? ")
return True
def get_yes_or_no(self, **kwargs):
if 'prompt' in kwargs:
kwargs['message']= kwargs['prompt']
del kwargs['prompt']
elif not 'message' in kwargs:
kwargs['message']= 'Make a choice: '
if not 'caption' in kwargs:
kwargs['caption']= 'Waiting for user input'
# The 'message_font' keyword is accepted by `MyMessageDialog`,
but not by
# `GenericMessageDialog`:
if MessageDialog == MyMessageDialog.MyMessageDialog:
if not 'message_font' in kwargs:
kwargs['message_font']= 13
else:
if 'message_font' in kwargs:
del kwargs['message_font']
dialog= MessageDialog(parent=None,
style=wx.YES_NO | wx.CANCEL | wx.ICON_INFORMATION, **kwargs)
return_code= dialog.ShowModal()
dialog.Destroy()
if return_code == wx.ID_YES:
print("You clicked on 'Yes'.")
elif return_code == wx.ID_NO:
print("You clicked on 'No'.")
# Close the GUI session:
self.Destroy()
if __name__ == '__main__':
app= MyApp(0)
app.MainLoop()
--End of yes_no_demo.py--
--Start of MyMessageDialog.py--
import wx
import wx.lib.agw.genericmessagedialog as GMD
class MyMessageDialog(GMD.GenericMessageDialog):
# Constructor:
def __init__(self, parent=None, **kwargs):
# Set 'message_font' attribute of the class. If the `kwargs`
dictionary
# contains the 'message_font' key, we must remove this key
before the
# dictionary is passed to the super class constructor.
if 'message_font' in kwargs:
self.message_font= kwargs['message_font']
del kwargs['message_font']
else:
self.message_font= 12
# If `self.message_font` is an int, replace it by instance of
`wx.Font`:
if isinstance(self.message_font, int):
self.message_font= wx.Font(self.message_font,
wx.SWISS, wx.NORMAL, wx.FONTWEIGHT_BOLD)
self.parent= parent
self.frame= wx.Frame(parent=self.parent)
self.panel= wx.Panel(parent=self.frame)
# Invoke constructor of the super class:
super(MyMessageDialog, self).__init__(parent=self.frame,
**kwargs)
def CreateTextSizer(self, message):
txt= wx.StaticText(parent=self, id=-1, label=message)
txt.SetFont(self.message_font)
sizer= wx.BoxSizer()
sizer.Add(txt)
return sizer
--End of MyMessageDialog.py--