wxpython custom dialog

#!/usr/bin/env python

ZetCode wxPython tutorial

In this code example, we create a
custom dialog.

author: Jan Bodnar
website: www.zetcode.com
last modified: July 2020

import wx

class ChangeDepthDialog(wx.Dialog):

    def __init__(self, *args, **kw):
        super(ChangeDepthDialog, self).__init__(*args, **kw)

        self.SetSize((250, 200))
        self.SetTitle("Change Color Depth")

    def InitUI(self):

        pnl = wx.Panel(self)
        vbox = wx.BoxSizer(wx.VERTICAL)

        sb = wx.StaticBox(pnl, label='Colors')
        sbs = wx.StaticBoxSizer(sb, orient=wx.VERTICAL)
        sbs.Add(wx.RadioButton(pnl, label='256 Colors',
        sbs.Add(wx.RadioButton(pnl, label='16 Colors'))
        sbs.Add(wx.RadioButton(pnl, label='2 Colors'))

        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        hbox1.Add(wx.RadioButton(pnl, label='Custom'))
        hbox1.Add(wx.TextCtrl(pnl), flag=wx.LEFT, border=5)


        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        okButton = wx.Button(self, label='Ok')
        closeButton = wx.Button(self, label='Close')
        hbox2.Add(closeButton, flag=wx.LEFT, border=5)

        vbox.Add(pnl, proportion=1,
            flag=wx.ALL|wx.EXPAND, border=5)
        vbox.Add(hbox2, flag=wx.ALIGN_CENTER|wx.TOP|wx.BOTTOM, border=10)


        okButton.Bind(wx.EVT_BUTTON, self.OnClose)
        closeButton.Bind(wx.EVT_BUTTON, self.OnClose)

    def OnClose(self, e):


class Example(wx.Frame):

    def __init__(self, *args, **kw):
        super(Example, self).__init__(*args, **kw)


    def InitUI(self):

        tb = self.CreateToolBar()
        tb.AddTool(toolId=wx.ID_ANY, label='', bitmap=wx.Bitmap('color.png'))


        tb.Bind(wx.EVT_TOOL, self.OnChangeDepth)

        self.SetSize((350, 250))
        self.SetTitle('Custom dialog')

    def OnChangeDepth(self, e):

        cdDialog = ChangeDepthDialog(None,
            title='Change Color Depth')

def main():

    app = wx.App()
    ex = Example(None)

if __name__ == '__main__':

Here is what the above code is Doing:
1. We create a custom dialog class ChangeDepthDialog.
2. We create a panel and a vertical box sizer.
3. We create a static box and a static box sizer.
4. We add three radio buttons to the static box sizer.
5. We create a horizontal box sizer and add a radio button and a text control to it.
6. We add the horizontal box sizer to the static box sizer.
7. We set the panel sizer.
8. We create another horizontal box sizer and add two buttons to it.
9. We add the panel and the horizontal box sizer to the vertical box sizer.
10. We set the dialog sizer.
11. We bind the OnClose method to the button events.
12. We create a toolbar and add a tool to it.
13. We bind the OnChangeDepth method to the tool event.
14. We set the frame size and title.
15. We centre the frame on the screen.
16. We show the frame.
17. We bind the OnChangeDepth method to the tool event.
18. We set the frame size and title.
19. We centre the frame on the screen.
20. We show the frame.
21. We create an instance of the ChangeDepthDialog class.
22. We show the dialog and wait for the user input.
23. We destroy the dialog.