Search code examples
wxpython

Basic questions about wx.Dialog


I would like to create a dialog with wx.Dialog.

I have two questions on that.

Do I have to set a wx.Frame as a parent window or can I use my wx.Dialog as the main window?

Are the Sizers usables in a wx.Dialog without parent?

Thank you for your answers.


Solution

  • In order No, you don't have to set a wx.Frame
    Yes, you can use a Dialog on its own
    Yes, sizers can be used in the dialog
    Here's an example:

    #!/usr/bin/env python
    import wx
    class TestDialog(wx.Dialog):
        def __init__(self, parent, msg, title):
            wx.Dialog.__init__(self, parent, id=-1, title=title)
            Buttons = []
            Buttons.append(wx.Button(self,1, "Approve Location"))
            Buttons.append(wx.Button(self,2, "Approve Item"))
            Buttons.append(wx.Button(self,3, "Change Qty"))
            Buttons.append(wx.Button(self,4, "Approve"))
            sizer = wx.GridBagSizer(5,3)
            sizer.Add(Buttons[0], (0, 5), (1,1), wx.EXPAND)
            sizer.Add(Buttons[1], (1, 4), (1,1), wx.EXPAND)
            sizer.Add(Buttons[2], (1, 5), (1,1), wx.EXPAND)
            sizer.Add(Buttons[3], (2, 5), (1,1), wx.EXPAND)
            self.Bind(wx.EVT_BUTTON, self.OnLocation, id=1)
            self.Bind(wx.EVT_BUTTON, self.OnItem, id=2)
            self.Bind(wx.EVT_BUTTON, self.OnQty, id=3)
            self.Bind(wx.EVT_BUTTON, self.OnApprove, id=4)
            self.buttonpressed = None
            self.SetSizerAndFit(sizer)
            self.Centre()
        def OnLocation(self,event):
            self.EndModal(1)
            self.buttonpressed="Location"
        def OnItem(self,event):
            self.EndModal(2)
            self.buttonpressed="Item"
        def OnQty(self,event):
            self.EndModal(3)
            self.buttonpressed="Qty"
        def OnApprove(self,event):
            self.EndModal(4)
            self.buttonpressed="Approve"
    
    if __name__ == "__main__":
        app = wx.App()
        dlg = TestDialog(None, "test my dialog", "Test Title")
        val = dlg.ShowModal()
        print "Dialog numeric result: " + str(val)
        print "Dialog text: " + str(dlg.buttonpressed)