Search code examples
pythonpython-2.7wxpython

How to get MessageDialog on exiting frame?


I need to know how to get the MessageDialog onto my existing frame, not a pop up frame. I tried just making a buttons for 'ok' and 'cancel' but I couldn't figure out how to get value of the textboxes I had before the ok and cancel. I got the error of the TextCtrlInstance.GetValue() is not defined. Here is the simplified code, which I don't it will help but I will post it anyways. In this code, a window pops up with ok and cancel but I want it in the 300 by 300 frame.

import wx
class oranges(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Stuff',size=(300,300))
        panel = wx.Panel(self)
        box = wx.MessageDialog(self, 'Cool','title',wx.OK|wx.CANCEL)
        result=box.ShowModal()
        if result==wx.ID_OK:
            print 'ok'
if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=oranges(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

Thanks so much in advance! Looking forward to the answers!


Solution

  • Sounds like you might want an MDI window:
    https://github.com/crankycoder/wxPython-2.9.2.4/blob/master/wxPython/demo/MDIWindows.py http://wxpython.org/Phoenix/docs/html/MDIChildFrame.html http://www.java2s.com/Tutorial/Python/0380__wxPython/MDIframe.htm

    It would be easier to just use a custom panel with those buttons and textboxes, like this:

    import wx
    
    class modalPanel(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent, -1)
            sizer = wx.BoxSizer(wx.HORIZONTAL)
            self.okBtn = wx.Button(self, -1, "OK")
            self.okBtn.Bind(wx.EVT_BUTTON, self.OnOkClick)
            sizer.Add(self.okBtn, 0)
            self.SetSizer(sizer)
    
        def OnOkClick(self, event):
            #do stuff
            pass
            self.Hide()
            self.GetParent().GetParent().stuffPanel.Show()
    
    class normalPanel(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent, -1)
            sizer = wx.BoxSizer(wx.HORIZONTAL)
            self.btn = wx.Button(self, -1, "Show Modal")
    
            sizer.Add(self.btn, 0)
            self.SetSizer(sizer)
    
    class oranges(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, -1,'Stuff',size=(300,300))
    
            self.panel = wx.Panel(self)
            self.sizer = wx.BoxSizer(wx.HORIZONTAL)
    
            self.stuffPanel = normalPanel(self.panel)
            self.stuffPanel.btn.Bind(wx.EVT_BUTTON, self.OnBtn)
    
            self.panel2 = modalPanel(self.panel)
            self.panel2.Hide()
    
            self.sizer.Add(self.stuffPanel, 0, wx.EXPAND)
            self.sizer.Add(self.panel2, 0, wx.EXPAND)
            self.panel.SetSizer(self.sizer)
    
        def OnBtn(self, event):
            self.stuffPanel.Hide()
            self.panel2.Show()
    
            self.sizer.Layout()      
    
    
    
    
    
    if __name__=='__main__':
        app=wx.App(False)
        frame=oranges()
        frame.Show()
        app.MainLoop()