wxpython custom dialog

Solutions on MaxInterview for wxpython custom dialog by the best coders in the world

showing results for - "wxpython custom dialog"
Kenji
01 Apr 2020
1#!/usr/bin/env python
2
3'''
4ZetCode wxPython tutorial
5
6In this code example, we create a
7custom dialog.
8
9author: Jan Bodnar
10website: www.zetcode.com
11last modified: July 2020
12'''
13
14import wx
15
16class ChangeDepthDialog(wx.Dialog):
17
18    def __init__(self, *args, **kw):
19        super(ChangeDepthDialog, self).__init__(*args, **kw)
20
21        self.InitUI()
22        self.SetSize((250, 200))
23        self.SetTitle("Change Color Depth")
24
25
26    def InitUI(self):
27
28        pnl = wx.Panel(self)
29        vbox = wx.BoxSizer(wx.VERTICAL)
30
31        sb = wx.StaticBox(pnl, label='Colors')
32        sbs = wx.StaticBoxSizer(sb, orient=wx.VERTICAL)
33        sbs.Add(wx.RadioButton(pnl, label='256 Colors',
34            style=wx.RB_GROUP))
35        sbs.Add(wx.RadioButton(pnl, label='16 Colors'))
36        sbs.Add(wx.RadioButton(pnl, label='2 Colors'))
37
38        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
39        hbox1.Add(wx.RadioButton(pnl, label='Custom'))
40        hbox1.Add(wx.TextCtrl(pnl), flag=wx.LEFT, border=5)
41        sbs.Add(hbox1)
42
43        pnl.SetSizer(sbs)
44
45        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
46        okButton = wx.Button(self, label='Ok')
47        closeButton = wx.Button(self, label='Close')
48        hbox2.Add(okButton)
49        hbox2.Add(closeButton, flag=wx.LEFT, border=5)
50
51        vbox.Add(pnl, proportion=1,
52            flag=wx.ALL|wx.EXPAND, border=5)
53        vbox.Add(hbox2, flag=wx.ALIGN_CENTER|wx.TOP|wx.BOTTOM, border=10)
54
55        self.SetSizer(vbox)
56
57        okButton.Bind(wx.EVT_BUTTON, self.OnClose)
58        closeButton.Bind(wx.EVT_BUTTON, self.OnClose)
59
60
61    def OnClose(self, e):
62
63        self.Destroy()
64
65
66class Example(wx.Frame):
67
68    def __init__(self, *args, **kw):
69        super(Example, self).__init__(*args, **kw)
70
71        self.InitUI()
72
73
74    def InitUI(self):
75
76        tb = self.CreateToolBar()
77        tb.AddTool(toolId=wx.ID_ANY, label='', bitmap=wx.Bitmap('color.png'))
78
79        tb.Realize()
80
81        tb.Bind(wx.EVT_TOOL, self.OnChangeDepth)
82
83        self.SetSize((350, 250))
84        self.SetTitle('Custom dialog')
85        self.Centre()
86
87    def OnChangeDepth(self, e):
88
89        cdDialog = ChangeDepthDialog(None,
90            title='Change Color Depth')
91        cdDialog.ShowModal()
92        cdDialog.Destroy()
93
94
95def main():
96
97    app = wx.App()
98    ex = Example(None)
99    ex.Show()
100    app.MainLoop()
101
102
103if __name__ == '__main__':
104    main()
105