Hi,
I must be doing something wrong - probably something obvious and simple - but I can't figure it out.
I have successfully made modal custom dialogs in my real application but I cannot get the dir dialog and the font dialog to open modally.
If I understand modal properly, I shouldn't be able to interact with the main form while the dialog is open. Nor should I be able to open multiple dialogs.
I have made a simple demo script which shows the problem.
What is the obvious (but not to me) error?
import wx
class MyApp(wx.App):
def __init__(self): wx.App.__init__(self)
frame = MyFrame(title="Dialog Test", size=(350,200))
frame.Show() self.SetTopWindow(frame)
class MyFrame(wx.Frame):
"""A simple form with one panel, one box sizer, one label, and two buttons.
The buttons open dialogs when clicked"""
def __init__(self, title="Default title", pos=wx.DefaultPosition, size=wx.DefaultSize): wx.Frame.__init__(self, parent=None, id=-1, title=title, pos=pos, size=size)
self.panel = wx.Panel(self) outerSizer = wx.BoxSizer(wx.VERTICAL)
lblQuestion = wx.StaticText(self.panel, -1, "Why are the dialogs not modal when ShowModal() is used?")
btnOpenDirDialog = wx.Button(self.panel, -1, "Select Dir ...")
btnOpenDirDialog.Bind(wx.EVT_BUTTON, self.OnButtonOpenDirDialog)
btnOpenFontDialog = wx.Button(self.panel, -1, "Select Font ...")
btnOpenFontDialog.Bind(wx.EVT_BUTTON, self.OnButtonOpenFontDialog)
outerSizer.Add(lblQuestion,0,wx.ALL, 10)
outerSizer.Add(btnOpenDirDialog,0,wx.ALL, 10)
outerSizer.Add(btnOpenFontDialog,0,wx.ALL, 10) self.panel.SetSizer(outerSizer)
def OnButtonOpenDirDialog(self, event):
"""Open dir dialog"""
dlgGetFolder = wx.DirDialog(None, "Choose a folder:", style=wx.DD_DEFAULT_STYLE|wx.DD_NEW_DIR_BUTTON)
if dlgGetFolder.ShowModal() == wx.ID_OK:
#was it modal?
self.folder = dlgGetFolder.GetPath()
dlgGetFolder.Destroy()
def OnButtonOpenFontDialog(self, event):
"""Open font dialog""" dlgGetFont = wx.FontDialog(None, wx.FontData())
if dlgGetFont.ShowModal() == wx.ID_OK:
#was it modal?
self.font = "foo"
dlgGetFont.Destroy()
if __name__ == '__main__':
app = MyApp()
app.MainLoop()
All the best, Grant
···
--
___________________________________
Dr Grant Paton-Simpson
Director, Paton-Simpson & Associates Ltd
www.p-s.co.nz
16 Summit Drive, Mt Albert, Auckland 1025
(09) 849-6696
(09) 849-6699
___________________________________