├── .gitignore
├── LICENSE
├── README.md
├── advanced
├── actresses.py
├── autowidth.py
├── closebutton.png
├── exit.png
├── help.html
├── help.png
├── helpwindow.py
├── html_window.py
├── listbox.py
├── newt.png
├── page.html
├── repository.py
└── sorted.py
├── customwidgets
├── burning.py
├── cpu.py
└── hyperlink.py
├── dialogs
├── about_dialog.py
├── color.png
├── custom_dialog.py
├── hunter.png
├── message_box.py
└── message_dialogs.py
├── dragdrop
├── dragdrop_file.py
└── dragdrop_text.py
├── events
├── custom_ids.py
├── default_ids.py
├── event_propagation.py
├── event_veto.py
├── focus_event.py
├── key_event.py
├── paint_event.py
├── simple_event.py
└── standard_ids.py
├── graphics
├── brushes.py
├── colours.py
├── custom_patterns.py
├── draw_line.py
├── draw_line2.py
├── gradients.py
├── granite.png
├── joins_caps.py
├── lines.py
├── pattern1.png
├── pattern2.png
├── pattern3.png
├── pens.py
├── points.py
├── region_operations.py
├── ruler.py
├── shapes.py
└── star.py
├── intro
├── centering.py
├── moving.py
├── no_minimize.py
├── set_size.py
└── simple.py
├── layout
├── absolute.py
├── bardejov.jpg
├── border.py
├── calculator.py
├── exec.png
├── goto_class.py
├── mincol.jpg
├── newclass.py
├── rename.py
├── review.py
└── rotunda.jpg
├── menus
├── checkmenu_item.py
├── context_menu.py
├── exit.png
├── icons_shortcuts.py
├── simple_menu.py
├── submenus.py
├── texit.png
├── tnew.png
├── toolbar.py
├── toolbars.py
├── topen.png
├── tredo.png
├── tsave.png
├── tundo.png
└── undo_redo.py
├── skeletons
├── browser
│ ├── browser.py
│ └── images
│ │ ├── back.png
│ │ ├── book.png
│ │ ├── forw.png
│ │ ├── home.png
│ │ ├── love.png
│ │ ├── play.png
│ │ ├── refresh.png
│ │ ├── sound.png
│ │ └── stop.png
├── filemanager
│ ├── file_manager.py
│ └── images
│ │ ├── empty.png
│ │ ├── folder.png
│ │ ├── help.png
│ │ ├── home.png
│ │ ├── image.png
│ │ ├── pdf.png
│ │ ├── previous.png
│ │ ├── refresh.png
│ │ ├── source-py.png
│ │ ├── terminal.png
│ │ ├── textedit.png
│ │ ├── up.png
│ │ └── up16.png
├── player
│ ├── images
│ │ ├── back.png
│ │ ├── forw.png
│ │ ├── pause.png
│ │ ├── play.png
│ │ └── volume.png
│ └── player.py
└── spreadsheet
│ ├── images
│ ├── align-center.png
│ ├── align-left.png
│ ├── align-right.png
│ ├── asc.png
│ ├── chart.png
│ ├── copy.png
│ ├── cut.png
│ ├── delete.png
│ ├── desc.png
│ ├── exit.png
│ ├── new.png
│ ├── open.png
│ ├── paste.png
│ ├── redo.png
│ ├── save.png
│ ├── text-bold.png
│ ├── text-italic.png
│ ├── text-underline.png
│ └── undo.png
│ └── spreadsheet.py
├── tetris
└── tetris.py
├── waiting.py
└── widgets
├── button_wid.py
├── checkbox.py
├── gauge_wid.py
├── slider_wid.py
├── spin_ctrl.py
├── static_line.py
├── static_text.py
└── toggle_buttons.py
/.gitignore:
--------------------------------------------------------------------------------
1 | cairo
2 | doc
3 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | BSD 2-Clause License
2 |
3 | Copyright (c) 2021, Jan Bodnar
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 |
9 | 1. Redistributions of source code must retain the above copyright notice, this
10 | list of conditions and the following disclaimer.
11 |
12 | 2. Redistributions in binary form must reproduce the above copyright notice,
13 | this list of conditions and the following disclaimer in the documentation
14 | and/or other materials provided with the distribution.
15 |
16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | wxPython code examples
2 |
3 | https://zetcode.com/wxpython/
4 |
5 |
--------------------------------------------------------------------------------
/advanced/actresses.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 |
4 | """
5 | ZetCode wxPython tutorial
6 |
7 | In this example, we create a simple
8 | wx.ListCtrl widget.
9 |
10 | author: Jan Bodnar
11 | website: www.zetcode.com
12 | last modified: May 2018
13 | """
14 |
15 | import wx
16 |
17 | data = [('Jessica Alba', 'Pomona', '1981'), ('Sigourney Weaver', 'New York', '1949'),
18 | ('Angelina Jolie', 'los angeles', '1975'), ('Natalie Portman', 'Jerusalem', '1981'),
19 | ('Rachel Weiss', 'London', '1971'), ('Scarlett Johansson', 'New York', '1984' )]
20 |
21 |
22 | class Example(wx.Frame):
23 |
24 | def __init__(self, *args, **kw):
25 | super(Example, self).__init__(*args, **kw)
26 |
27 | self.InitUI()
28 |
29 | def InitUI(self):
30 |
31 | hbox = wx.BoxSizer(wx.HORIZONTAL)
32 | panel = wx.Panel(self)
33 |
34 | self.list = wx.ListCtrl(panel, wx.ID_ANY, style=wx.LC_REPORT)
35 | self.list.InsertColumn(0, 'name', width=140)
36 | self.list.InsertColumn(1, 'place', width=130)
37 | self.list.InsertColumn(2, 'year', wx.LIST_FORMAT_RIGHT, 90)
38 |
39 | idx = 0
40 |
41 | for i in data:
42 |
43 | index = self.list.InsertItem(idx, i[0])
44 | self.list.SetItem(index, 1, i[1])
45 | self.list.SetItem(index, 2, i[2])
46 | idx += 1
47 |
48 | hbox.Add(self.list, 1, wx.EXPAND)
49 | panel.SetSizer(hbox)
50 |
51 | self.SetTitle('Actresses')
52 | self.Centre()
53 |
54 |
55 | def main():
56 |
57 | app = wx.App()
58 | ex = Example(None)
59 | ex.Show()
60 | app.MainLoop()
61 |
62 |
63 | if __name__ == '__main__':
64 | main()
65 |
--------------------------------------------------------------------------------
/advanced/autowidth.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example, we use wx.lib.mixins.listctrl.ListCtrlAutoWidthMixin
7 | with a wx.ListBox.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: May 2018
12 | """
13 |
14 | import wx
15 | import wx.lib.mixins.listctrl
16 |
17 | data = [('Jessica Alba', 'Pomona', '1981'), ('Sigourney Weaver', 'New York', '1949'),
18 | ('Angelina Jolie', 'los angeles', '1975'), ('Natalie Portman', 'Jerusalem', '1981'),
19 | ('Rachel Weiss', 'London', '1971'), ('Scarlett Johansson', 'New York', '1984')]
20 |
21 |
22 | class AutoWidthListCtrl(wx.ListCtrl, wx.lib.mixins.listctrl.ListCtrlAutoWidthMixin):
23 |
24 | def __init__(self, parent, *args, **kw):
25 | wx.ListCtrl.__init__(self, parent, wx.ID_ANY, style=wx.LC_REPORT)
26 | wx.lib.mixins.listctrl.ListCtrlAutoWidthMixin.__init__(self)
27 |
28 |
29 | class Example(wx.Frame):
30 |
31 | def __init__(self, *args, **kw):
32 | super(Example, self).__init__(*args, **kw)
33 |
34 | self.InitUI()
35 |
36 | def InitUI(self):
37 |
38 | hbox = wx.BoxSizer(wx.HORIZONTAL)
39 |
40 | panel = wx.Panel(self)
41 |
42 | self.list = AutoWidthListCtrl(panel)
43 | self.list.InsertColumn(0, 'name', width=140)
44 | self.list.InsertColumn(1, 'place', width=130)
45 | self.list.InsertColumn(2, 'year', wx.LIST_FORMAT_RIGHT, 90)
46 |
47 | idx = 0
48 |
49 | for i in data:
50 |
51 | index = self.list.InsertItem(idx, i[0])
52 | self.list.SetItem(index, 1, i[1])
53 | self.list.SetItem(index, 2, i[2])
54 | idx += 1
55 |
56 | hbox.Add(self.list, 1, wx.EXPAND)
57 | panel.SetSizer(hbox)
58 |
59 | self.SetTitle('Actresses')
60 | self.Centre()
61 |
62 |
63 | def main():
64 |
65 | app = wx.App()
66 | ex = Example(None)
67 | ex.Show()
68 | app.MainLoop()
69 |
70 |
71 | if __name__ == '__main__':
72 | main()
73 |
--------------------------------------------------------------------------------
/advanced/closebutton.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/advanced/closebutton.png
--------------------------------------------------------------------------------
/advanced/exit.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/advanced/exit.png
--------------------------------------------------------------------------------
/advanced/help.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Table of Contents
6 |
7 |
16 |
17 |
18 |
19 | Basic Statistics
20 | Overview of elementary concepts in statistics.
21 | Variables. Correlation. Measurement scales. Statistical significance.
22 | Distributions. Normality assumption.
23 |
24 |
25 |
26 |
27 |
28 | Advanced Statistics
29 | Overview of advanced concepts in statistics. Anova. Linear regression.
30 | Estimation and hypothesis testing.
31 | Error terms.
32 |
33 |
34 |
35 |
36 |
37 | Introducing Newt
38 | Introducing the basic functionality of the Newt application. Creating sheets.
39 | Charts. Menus and Toolbars. Importing data. Saving data in various formats.
40 | Exporting data. Shortcuts. List of methods.
41 |
42 |
43 |
44 |
45 |
46 | Charts
47 | Working with charts. 2D charts. 3D charts. Bar, line, box, pie, range charts.
48 | Scatterplots. Histograms.
49 |
50 |
51 |
52 |
53 |
54 | Predicting values
55 | Time series and forecasting. Trend Analysis. Seasonality. Moving averages.
56 | Univariate methods. Multivariate methods. Holt-Winters smoothing.
57 | Exponential smoothing. ARIMA. Fourier analysis.
58 |
59 |
60 |
61 |
62 |
63 | Neural networks
64 | Overview of neural networks. Biology behind neural networks.
65 | Basic artificial Model. Training. Preprocessing. Postprocessing.
66 | Types of neural networks.
67 |
68 |
69 |
70 |
71 |
72 | Glossary
73 | Terms and definitions in statistics.
74 |
75 |
76 |
77 |
78 |
79 |
--------------------------------------------------------------------------------
/advanced/help.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/advanced/help.png
--------------------------------------------------------------------------------
/advanced/helpwindow.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example, we create a help window window
7 | with wx.html.HtmlWindow.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: May 2018
12 | """
13 |
14 | import wx
15 | import wx.html as html
16 |
17 | class Example(wx.Frame):
18 |
19 | def __init__(self, *args, **kw):
20 | super(Example, self).__init__(*args, **kw)
21 |
22 | self.InitUI()
23 |
24 | def InitUI(self):
25 |
26 | toolbar = self.CreateToolBar()
27 | toolbar.AddTool(1, 'Exit', wx.Bitmap('exit.png'))
28 | toolbar.AddTool(2, 'Help', wx.Bitmap('help.png'))
29 | toolbar.Realize()
30 |
31 | self.splitter = wx.SplitterWindow(self)
32 | self.panelLeft = wx.Panel(self.splitter, wx.ID_ANY, style=wx.BORDER_SUNKEN)
33 |
34 | self.panelRight = wx.Panel(self.splitter)
35 | vbox2 = wx.BoxSizer(wx.VERTICAL)
36 | header = wx.Panel(self.panelRight, wx.ID_ANY)
37 |
38 | header.SetBackgroundColour('#6f6a59')
39 | header.SetForegroundColour('white')
40 |
41 | hbox = wx.BoxSizer(wx.HORIZONTAL)
42 |
43 | st = wx.StaticText(header, wx.ID_ANY, 'Help')
44 | font = st.GetFont()
45 | font.SetFamily(wx.FONTFAMILY_ROMAN)
46 | font.SetPointSize(11)
47 | st.SetFont(font)
48 |
49 | hbox.Add(st, 1, wx.TOP | wx.BOTTOM | wx.LEFT, 8)
50 |
51 | closeBtn = wx.BitmapButton(header, wx.ID_ANY, wx.Bitmap('closebutton.png',
52 | wx.BITMAP_TYPE_PNG), style=wx.NO_BORDER)
53 | closeBtn.SetBackgroundColour('#6f6a59')
54 |
55 | hbox.Add(closeBtn, 0, wx.TOP|wx.BOTTOM, 8)
56 | header.SetSizer(hbox)
57 |
58 | vbox2.Add(header, 0, wx.EXPAND)
59 |
60 | helpWin = html.HtmlWindow(self.panelRight, style=wx.NO_BORDER)
61 | helpWin.LoadPage('help.html')
62 |
63 | vbox2.Add(helpWin, 1, wx.EXPAND)
64 |
65 | self.panelRight.SetSizer(vbox2)
66 | self.panelLeft.SetFocus()
67 |
68 | self.splitter.SplitVertically(self.panelLeft, self.panelRight)
69 | self.splitter.Unsplit()
70 |
71 | self.Bind(wx.EVT_BUTTON, self.CloseHelp, id=closeBtn.GetId())
72 | self.Bind(wx.EVT_TOOL, self.OnClose, id=1)
73 | self.Bind(wx.EVT_TOOL, self.OnHelp, id=2)
74 |
75 | self.panelLeft.Bind(wx.EVT_KEY_DOWN, self.OnKeyPressed)
76 | self.panelLeft.SetFocus()
77 |
78 | self.CreateStatusBar()
79 |
80 | self.SetTitle('Help')
81 | self.Centre()
82 |
83 | def OnClose(self, e):
84 | self.Close()
85 |
86 | def OnHelp(self, e):
87 |
88 | self.splitter.SplitVertically(self.panelLeft, self.panelRight)
89 | self.panelLeft.SetFocus()
90 |
91 | def CloseHelp(self, e):
92 |
93 | self.splitter.Unsplit()
94 | self.panelLeft.SetFocus()
95 |
96 | def OnKeyPressed(self, e):
97 |
98 | keycode = e.GetKeyCode()
99 | print(keycode)
100 |
101 | if keycode == wx.WXK_F1:
102 |
103 | self.splitter.SplitVertically(self.panelLeft, self.panelRight)
104 | self.panelLeft.SetFocus()
105 |
106 |
107 | def main():
108 |
109 | app = wx.App()
110 | ex = Example(None)
111 | ex.Show()
112 | app.MainLoop()
113 |
114 |
115 | if __name__ == '__main__':
116 | main()
117 |
--------------------------------------------------------------------------------
/advanced/html_window.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example, we create a wx.html.HtmlWindow widget.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: May 2018
11 | """
12 |
13 | import wx
14 | import wx.html
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | panel = wx.Panel(self)
26 |
27 | vbox = wx.BoxSizer(wx.VERTICAL)
28 | hbox = wx.BoxSizer(wx.HORIZONTAL)
29 |
30 | htmlwin = wx.html.HtmlWindow(panel, wx.ID_ANY, style=wx.NO_BORDER)
31 | htmlwin.SetStandardFonts()
32 | htmlwin.LoadPage("page.html")
33 |
34 | vbox.Add((-1, 10), 0)
35 | vbox.Add(htmlwin, 1, wx.EXPAND | wx.ALL, 9)
36 |
37 | bitmap = wx.StaticBitmap(panel, wx.ID_ANY, wx.Bitmap('newt.png'))
38 | hbox.Add(bitmap, 0, wx.LEFT | wx.BOTTOM | wx.TOP, 10)
39 | btnOk = wx.Button(panel, wx.ID_ANY, 'Ok')
40 |
41 | self.Bind(wx.EVT_BUTTON, self.OnClose, id=btnOk.GetId())
42 |
43 | hbox.Add((100, -1), 1, wx.EXPAND | wx.ALIGN_RIGHT)
44 | hbox.Add(btnOk, flag=wx.TOP | wx.BOTTOM | wx.RIGHT, border=10)
45 | vbox.Add(hbox, 0, wx.EXPAND)
46 |
47 | panel.SetSizer(vbox)
48 |
49 | self.SetTitle('Basic statistics')
50 | self.Centre()
51 |
52 | def OnClose(self, event):
53 | self.Close()
54 |
55 |
56 | def main():
57 |
58 | app = wx.App()
59 | ex = Example(None)
60 | ex.Show()
61 | app.MainLoop()
62 |
63 |
64 | if __name__ == '__main__':
65 | main()
66 |
--------------------------------------------------------------------------------
/advanced/listbox.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example, we create a wx.ListBox widget.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: May 2018
11 | """
12 |
13 | import wx
14 |
15 | class Example(wx.Frame):
16 |
17 | def __init__(self, *args, **kw):
18 | super(Example, self).__init__(*args, **kw)
19 |
20 | self.InitUI()
21 |
22 | def InitUI(self):
23 |
24 | panel = wx.Panel(self)
25 | hbox = wx.BoxSizer(wx.HORIZONTAL)
26 |
27 | self.listbox = wx.ListBox(panel)
28 | hbox.Add(self.listbox, wx.ID_ANY, wx.EXPAND | wx.ALL, 20)
29 |
30 | btnPanel = wx.Panel(panel)
31 | vbox = wx.BoxSizer(wx.VERTICAL)
32 | newBtn = wx.Button(btnPanel, wx.ID_ANY, 'New', size=(90, 30))
33 | renBtn = wx.Button(btnPanel, wx.ID_ANY, 'Rename', size=(90, 30))
34 | delBtn = wx.Button(btnPanel, wx.ID_ANY, 'Delete', size=(90, 30))
35 | clrBtn = wx.Button(btnPanel, wx.ID_ANY, 'Clear', size=(90, 30))
36 |
37 | self.Bind(wx.EVT_BUTTON, self.NewItem, id=newBtn.GetId())
38 | self.Bind(wx.EVT_BUTTON, self.OnRename, id=renBtn.GetId())
39 | self.Bind(wx.EVT_BUTTON, self.OnDelete, id=delBtn.GetId())
40 | self.Bind(wx.EVT_BUTTON, self.OnClear, id=clrBtn.GetId())
41 | self.Bind(wx.EVT_LISTBOX_DCLICK, self.OnRename)
42 |
43 | vbox.Add((-1, 20))
44 | vbox.Add(newBtn)
45 | vbox.Add(renBtn, 0, wx.TOP, 5)
46 | vbox.Add(delBtn, 0, wx.TOP, 5)
47 | vbox.Add(clrBtn, 0, wx.TOP, 5)
48 |
49 | btnPanel.SetSizer(vbox)
50 | hbox.Add(btnPanel, 0.6, wx.EXPAND | wx.RIGHT, 20)
51 | panel.SetSizer(hbox)
52 |
53 | self.SetTitle('wx.ListBox')
54 | self.Centre()
55 |
56 | def NewItem(self, event):
57 |
58 | text = wx.GetTextFromUser('Enter a new item', 'Insert dialog')
59 | if text != '':
60 | self.listbox.Append(text)
61 |
62 | def OnRename(self, event):
63 |
64 | sel = self.listbox.GetSelection()
65 | text = self.listbox.GetString(sel)
66 | renamed = wx.GetTextFromUser('Rename item', 'Rename dialog', text)
67 |
68 | if renamed != '':
69 | self.listbox.Delete(sel)
70 | item_id = self.listbox.Insert(renamed, sel)
71 | self.listbox.SetSelection(item_id)
72 |
73 | def OnDelete(self, event):
74 |
75 | sel = self.listbox.GetSelection()
76 | if sel != -1:
77 | self.listbox.Delete(sel)
78 |
79 | def OnClear(self, event):
80 | self.listbox.Clear()
81 |
82 |
83 | def main():
84 |
85 | app = wx.App()
86 | ex = Example(None)
87 | ex.Show()
88 | app.MainLoop()
89 |
90 |
91 | if __name__ == '__main__':
92 | main()
93 |
--------------------------------------------------------------------------------
/advanced/newt.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/advanced/newt.png
--------------------------------------------------------------------------------
/advanced/page.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Maximum |
7 | 9000 |
8 |
9 |
10 | Mean |
11 | 6076 |
12 |
13 |
14 | Minimum |
15 | 3800 |
16 |
17 |
18 | Median |
19 | 6000 |
20 |
21 |
22 | Standard Deviation |
23 | 6076 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/advanced/repository.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example, we create a check list control widget.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: May 2018
11 | """
12 |
13 | import wx
14 | from wx.lib.mixins.listctrl import CheckListCtrlMixin, ListCtrlAutoWidthMixin
15 |
16 | packages = [('abiword', '5.8M', 'base'), ('adie', '145k', 'base'),
17 | ('airsnort', '71k', 'base'), ('ara', '717k', 'base'), ('arc', '139k', 'base'),
18 | ('asc', '5.8M', 'base'), ('ascii', '74k', 'base'), ('ash', '74k', 'base')]
19 |
20 | class CheckListCtrl(wx.ListCtrl, CheckListCtrlMixin, ListCtrlAutoWidthMixin):
21 |
22 | def __init__(self, parent):
23 | wx.ListCtrl.__init__(self, parent, wx.ID_ANY, style=wx.LC_REPORT |
24 | wx.SUNKEN_BORDER)
25 | CheckListCtrlMixin.__init__(self)
26 | ListCtrlAutoWidthMixin.__init__(self)
27 |
28 |
29 | class Example(wx.Frame):
30 |
31 | def __init__(self, *args, **kw):
32 | super(Example, self).__init__(*args, **kw)
33 |
34 | panel = wx.Panel(self)
35 |
36 | vbox = wx.BoxSizer(wx.VERTICAL)
37 | hbox = wx.BoxSizer(wx.HORIZONTAL)
38 |
39 | leftPanel = wx.Panel(panel)
40 | rightPanel = wx.Panel(panel)
41 |
42 | self.log = wx.TextCtrl(rightPanel, style=wx.TE_MULTILINE|wx.TE_READONLY)
43 | self.list = CheckListCtrl(rightPanel)
44 | self.list.InsertColumn(0, 'Package', width=140)
45 | self.list.InsertColumn(1, 'Size')
46 | self.list.InsertColumn(2, 'Repository')
47 |
48 | idx = 0
49 |
50 | for i in packages:
51 |
52 | index = self.list.InsertItem(idx, i[0])
53 | self.list.SetItem(index, 1, i[1])
54 | self.list.SetItem(index, 2, i[2])
55 | idx += 1
56 |
57 | vbox2 = wx.BoxSizer(wx.VERTICAL)
58 |
59 | selBtn = wx.Button(leftPanel, label='Select All')
60 | desBtn = wx.Button(leftPanel, label='Deselect All')
61 | appBtn = wx.Button(leftPanel, label='Apply')
62 |
63 | self.Bind(wx.EVT_BUTTON, self.OnSelectAll, id=selBtn.GetId())
64 | self.Bind(wx.EVT_BUTTON, self.OnDeselectAll, id=desBtn.GetId())
65 | self.Bind(wx.EVT_BUTTON, self.OnApply, id=appBtn.GetId())
66 |
67 | vbox2.Add(selBtn, 0, wx.TOP|wx.BOTTOM, 5)
68 | vbox2.Add(desBtn, 0, wx.BOTTOM, 5)
69 | vbox2.Add(appBtn)
70 |
71 | leftPanel.SetSizer(vbox2)
72 |
73 | vbox.Add(self.list, 4, wx.EXPAND | wx.TOP, 3)
74 | vbox.Add((-1, 10))
75 | vbox.Add(self.log, 1, wx.EXPAND)
76 | vbox.Add((-1, 10))
77 |
78 | rightPanel.SetSizer(vbox)
79 |
80 | hbox.Add(leftPanel, 0, wx.EXPAND | wx.RIGHT, 5)
81 | hbox.Add(rightPanel, 1, wx.EXPAND)
82 | hbox.Add((3, -1))
83 |
84 | panel.SetSizer(hbox)
85 |
86 | self.SetTitle('Repository')
87 | self.Centre()
88 |
89 | def OnSelectAll(self, event):
90 |
91 | num = self.list.GetItemCount()
92 | for i in range(num):
93 | self.list.CheckItem(i)
94 |
95 | def OnDeselectAll(self, event):
96 |
97 | num = self.list.GetItemCount()
98 | for i in range(num):
99 | self.list.CheckItem(i, False)
100 |
101 | def OnApply(self, event):
102 |
103 | num = self.list.GetItemCount()
104 |
105 | for i in range(num):
106 |
107 | if i == 0: self.log.Clear()
108 |
109 | if self.list.IsChecked(i):
110 | self.log.AppendText(self.list.GetItemText(i) + '\n')
111 |
112 |
113 | def main():
114 |
115 | app = wx.App()
116 | ex = Example(None)
117 | ex.Show()
118 | app.MainLoop()
119 |
120 |
121 | if __name__ == '__main__':
122 | main()
123 |
--------------------------------------------------------------------------------
/advanced/sorted.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example, we create sortable columns with
7 | wx.lib.mixins.listctrl.ColumnSorterMixin
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: May 2018
12 | """
13 |
14 | import wx
15 | import wx.lib.mixins.listctrl
16 |
17 | actresses = {
18 | 1 : ('Jessica Alba', 'Pomona', '1981'),
19 | 2 : ('Sigourney Weaver', 'New York', '1949'),
20 | 3 : ('Angelina Jolie', 'Los Angeles', '1975'),
21 | 4 : ('Natalie Portman', 'Jerusalem', '1981'),
22 | 5 : ('Rachel Weiss', 'London', '1971'),
23 | 6 : ('Scarlett Johansson', 'New York', '1984')
24 | }
25 |
26 |
27 | class SortedListCtrl(wx.ListCtrl, wx.lib.mixins.listctrl.ColumnSorterMixin):
28 |
29 | def __init__(self, parent):
30 |
31 | wx.ListCtrl.__init__(self, parent, wx.ID_ANY, style=wx.LC_REPORT)
32 | wx.lib.mixins.listctrl.ColumnSorterMixin.__init__(self, len(actresses))
33 | self.itemDataMap = actresses
34 |
35 | def GetListCtrl(self):
36 | return self
37 |
38 | class Example(wx.Frame):
39 |
40 | def __init__(self, *args, **kw):
41 | super(Example, self).__init__(*args, **kw)
42 |
43 | self.InitUI()
44 |
45 | def InitUI(self):
46 |
47 | hbox = wx.BoxSizer(wx.HORIZONTAL)
48 | panel = wx.Panel(self)
49 |
50 | self.list = SortedListCtrl(panel)
51 | self.list.InsertColumn(0, 'name', width=140)
52 | self.list.InsertColumn(1, 'place', width=130)
53 | self.list.InsertColumn(2, 'year', wx.LIST_FORMAT_RIGHT, 90)
54 |
55 | items = actresses.items()
56 |
57 | idx = 0
58 |
59 | for key, data in items:
60 |
61 | index = self.list.InsertItem(idx, data[0])
62 | self.list.SetItem(index, 1, data[1])
63 | self.list.SetItem(index, 2, data[2])
64 | self.list.SetItemData(index, key)
65 | idx += 1
66 |
67 | hbox.Add(self.list, 1, wx.EXPAND)
68 | panel.SetSizer(hbox)
69 |
70 | self.SetTitle('Actresses')
71 | self.Centre()
72 |
73 |
74 | def main():
75 |
76 | app = wx.App()
77 | ex = Example(None)
78 | ex.Show()
79 | app.MainLoop()
80 |
81 |
82 | if __name__ == '__main__':
83 | main()
84 |
--------------------------------------------------------------------------------
/customwidgets/burning.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program creates a Burning widget.
7 |
8 | author: Jan Bodnar
9 | website: zetcode.com
10 | last edited: May 2018
11 | """
12 |
13 | import wx
14 |
15 | class Burning(wx.Panel):
16 | def __init__(self, parent):
17 | wx.Panel.__init__(self, parent, size=(-1, 30), style=wx.SUNKEN_BORDER)
18 |
19 | self.parent = parent
20 | self.font = wx.Font(9, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL,
21 | wx.FONTWEIGHT_NORMAL, False, 'Courier 10 Pitch')
22 |
23 | self.Bind(wx.EVT_PAINT, self.OnPaint)
24 | self.Bind(wx.EVT_SIZE, self.OnSize)
25 |
26 |
27 | def OnPaint(self, e):
28 |
29 | num = range(75, 700, 75)
30 | dc = wx.PaintDC(self)
31 | dc.SetFont(self.font)
32 | w, h = self.GetSize()
33 |
34 | self.cw = self.parent.GetParent().cw
35 |
36 | step = int(round(w / 10.0))
37 |
38 | j = 0
39 |
40 | till = (w / 750.0) * self.cw
41 | full = (w / 750.0) * 700
42 |
43 | if self.cw >= 700:
44 |
45 | dc.SetPen(wx.Pen('#FFFFB8'))
46 | dc.SetBrush(wx.Brush('#FFFFB8'))
47 | dc.DrawRectangle(0, 0, full, 30)
48 | dc.SetPen(wx.Pen('#ffafaf'))
49 | dc.SetBrush(wx.Brush('#ffafaf'))
50 | dc.DrawRectangle(full, 0, till-full, 30)
51 | else:
52 |
53 | dc.SetPen(wx.Pen('#FFFFB8'))
54 | dc.SetBrush(wx.Brush('#FFFFB8'))
55 | dc.DrawRectangle(0, 0, till, 30)
56 |
57 |
58 | dc.SetPen(wx.Pen('#5C5142'))
59 |
60 | for i in range(step, 10*step, step):
61 |
62 | dc.DrawLine(i, 0, i, 6)
63 | width, height = dc.GetTextExtent(str(num[j]))
64 | dc.DrawText(str(num[j]), i-width/2, 8)
65 | j = j + 1
66 |
67 | def OnSize(self, e):
68 |
69 | self.Refresh()
70 |
71 |
72 | class Example(wx.Frame):
73 |
74 | def __init__(self, *args, **kwargs):
75 | super(Example, self).__init__(*args, **kwargs)
76 |
77 | self.InitUI()
78 |
79 | def InitUI(self):
80 |
81 | self.cw = 75
82 |
83 | panel = wx.Panel(self)
84 | CenterPanel = wx.Panel(panel)
85 |
86 | self.sld = wx.Slider(CenterPanel, value=75, maxValue=750, size=(200, -1),
87 | style=wx.SL_LABELS)
88 |
89 | vbox = wx.BoxSizer(wx.VERTICAL)
90 | hbox = wx.BoxSizer(wx.HORIZONTAL)
91 | hbox2 = wx.BoxSizer(wx.HORIZONTAL)
92 | hbox3 = wx.BoxSizer(wx.HORIZONTAL)
93 |
94 | self.wid = Burning(panel)
95 | hbox.Add(self.wid, 1, wx.EXPAND)
96 |
97 | hbox2.Add(CenterPanel, 1, wx.EXPAND)
98 | hbox3.Add(self.sld, 0, wx.LEFT|wx.TOP, 35)
99 |
100 | CenterPanel.SetSizer(hbox3)
101 |
102 | vbox.Add(hbox2, 1, wx.EXPAND)
103 | vbox.Add(hbox, 0, wx.EXPAND)
104 |
105 |
106 | self.Bind(wx.EVT_SCROLL, self.OnScroll)
107 |
108 | panel.SetSizer(vbox)
109 |
110 | self.sld.SetFocus()
111 |
112 | self.SetTitle("Burning widget")
113 | self.Centre()
114 |
115 | def OnScroll(self, e):
116 |
117 | self.cw = self.sld.GetValue()
118 | self.wid.Refresh()
119 |
120 |
121 | def main():
122 |
123 | app = wx.App()
124 | ex = Example(None)
125 | ex.Show()
126 | app.MainLoop()
127 |
128 |
129 | if __name__ == '__main__':
130 | main()
131 |
--------------------------------------------------------------------------------
/customwidgets/cpu.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program creates a CPU widget.
7 |
8 | author: Jan Bodnar
9 | website: zetcode.com
10 | last edited: May 2018
11 | """
12 |
13 | import wx
14 |
15 |
16 | class CPU(wx.Panel):
17 |
18 | def __init__(self, parent):
19 | wx.Panel.__init__(self, parent, size=(80, 110))
20 |
21 | self.parent = parent
22 | self.SetBackgroundColour('#000000')
23 | self.Bind(wx.EVT_PAINT, self.OnPaint)
24 |
25 |
26 | def OnPaint(self, e):
27 |
28 | dc = wx.PaintDC(self)
29 |
30 | dc.SetDeviceOrigin(0, 100)
31 | dc.SetAxisOrientation(True, True)
32 |
33 | pos = self.parent.GetParent().GetParent().sel
34 | rect = pos / 5
35 |
36 | for i in range(1, 21):
37 |
38 | if i > rect:
39 |
40 | dc.SetBrush(wx.Brush('#075100'))
41 | dc.DrawRectangle(10, i*4, 30, 5)
42 | dc.DrawRectangle(41, i*4, 30, 5)
43 |
44 | else:
45 | dc.SetBrush(wx.Brush('#36ff27'))
46 | dc.DrawRectangle(10, i*4, 30, 5)
47 | dc.DrawRectangle(41, i*4, 30, 5)
48 |
49 |
50 | class Example(wx.Frame):
51 |
52 | def __init__(self, *args, **kwargs):
53 | super(Example, self).__init__(*args, **kwargs)
54 |
55 | self.InitUI()
56 |
57 | def InitUI(self):
58 |
59 | self.sel = 0
60 |
61 | panel = wx.Panel(self)
62 | centerPanel = wx.Panel(panel)
63 |
64 | self.cpu = CPU(centerPanel)
65 |
66 | hbox = wx.BoxSizer(wx.HORIZONTAL)
67 |
68 | self.slider = wx.Slider(panel, value=self.sel, maxValue=100, size=(-1, 100),
69 | style=wx.VERTICAL | wx.SL_INVERSE)
70 | self.slider.SetFocus()
71 |
72 | hbox.Add(centerPanel, 0, wx.LEFT | wx.TOP, 20)
73 | hbox.Add(self.slider, 0, wx.LEFT | wx.TOP, 30)
74 |
75 | self.Bind(wx.EVT_SCROLL, self.OnScroll)
76 |
77 | panel.SetSizer(hbox)
78 |
79 | self.SetTitle("CPU")
80 | self.Centre()
81 |
82 |
83 | def OnScroll(self, e):
84 |
85 | self.sel = e.GetInt()
86 | self.cpu.Refresh()
87 |
88 |
89 | def main():
90 |
91 | app = wx.App()
92 | ex = Example(None)
93 | ex.Show()
94 | app.MainLoop()
95 |
96 |
97 | if __name__ == '__main__':
98 | main()
99 |
--------------------------------------------------------------------------------
/customwidgets/hyperlink.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program creates a Hyperlink widget.
7 |
8 | author: Jan Bodnar
9 | website: zetcode.com
10 | last edited: May 2018
11 | """
12 |
13 | import wx
14 | from wx.lib.stattext import GenStaticText
15 | import webbrowser
16 |
17 |
18 | class Link(GenStaticText):
19 |
20 | def __init__(self, *args, **kw):
21 | super(Link, self).__init__(*args, **kw)
22 |
23 | self.font1 = wx.Font(11, wx.SWISS, wx.NORMAL, wx.BOLD, True, 'Verdana')
24 | self.font2 = wx.Font(11, wx.SWISS, wx.NORMAL, wx.BOLD, False, 'Verdana')
25 |
26 | self.SetFont(self.font2)
27 | self.SetForegroundColour('#0000ff')
28 |
29 | self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvent)
30 | self.Bind(wx.EVT_MOTION, self.OnMouseEvent)
31 |
32 | def SetUrl(self, url):
33 |
34 | self.url = url
35 |
36 |
37 | def OnMouseEvent(self, e):
38 |
39 | if e.Moving():
40 |
41 | self.SetCursor(wx.Cursor(wx.CURSOR_HAND))
42 | self.SetFont(self.font1)
43 |
44 | elif e.LeftUp():
45 |
46 | webbrowser.open_new(self.url)
47 |
48 | else:
49 | self.SetCursor(wx.NullCursor)
50 | self.SetFont(self.font2)
51 |
52 | e.Skip()
53 |
54 |
55 | class Example(wx.Frame):
56 |
57 | def __init__(self, *args, **kw):
58 | super(Example, self).__init__(*args, **kw)
59 |
60 | self.InitUI()
61 |
62 | def InitUI(self):
63 |
64 | panel = wx.Panel(self)
65 |
66 | vbox = wx.BoxSizer(wx.VERTICAL)
67 | hbox = wx.BoxSizer(wx.HORIZONTAL)
68 |
69 | st = GenStaticText(panel, label='Go to web site:')
70 | st.SetFont(wx.Font(11, wx.SWISS, wx.NORMAL, wx.BOLD, False, 'Verdana'))
71 | hbox.Add(st, flag=wx.LEFT, border=20)
72 |
73 | link_wid = Link(panel, label='ZetCode')
74 | link_wid.SetUrl('http://www.zetcode.com')
75 | hbox.Add(link_wid, flag=wx.LEFT, border=20)
76 |
77 | vbox.Add(hbox, flag=wx.TOP, border=30)
78 | panel.SetSizer(vbox)
79 |
80 | self.SetTitle('A Hyperlink')
81 | self.Centre()
82 |
83 |
84 | def main():
85 |
86 | app = wx.App()
87 | ex = Example(None)
88 | ex.Show()
89 | app.MainLoop()
90 |
91 |
92 | if __name__ == '__main__':
93 | main()
94 |
--------------------------------------------------------------------------------
/dialogs/about_dialog.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example, we create an
7 | about dialog box.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: April 2018
12 | """
13 |
14 | import wx
15 | import wx.adv
16 |
17 |
18 | class Example(wx.Frame):
19 |
20 | def __init__(self, *args, **kwargs):
21 | super(Example, self).__init__(*args, **kwargs)
22 |
23 | self.InitUI()
24 |
25 | def InitUI(self):
26 |
27 | menubar = wx.MenuBar()
28 | help = wx.Menu()
29 | help.Append(wx.ID_ANY, '&About')
30 | help.Bind(wx.EVT_MENU, self.OnAboutBox)
31 |
32 | menubar.Append(help, '&Help')
33 | self.SetMenuBar(menubar)
34 |
35 | self.SetSize((350, 250))
36 | self.SetTitle('About dialog box')
37 | self.Centre()
38 |
39 | def OnAboutBox(self, e):
40 |
41 | description = """File Hunter is an advanced file manager for
42 | the Unix operating system. Features include powerful built-in editor,
43 | advanced search capabilities, powerful batch renaming, file comparison,
44 | extensive archive handling and more.
45 | """
46 |
47 | licence = """File Hunter is free software; you can redistribute
48 | it and/or modify it under the terms of the GNU General Public License as
49 | published by the Free Software Foundation; either version 2 of the License,
50 | or (at your option) any later version.
51 |
52 | File Hunter is distributed in the hope that it will be useful,
53 | but WITHOUT ANY WARRANTY; without even the implied warranty of
54 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
55 | See the GNU General Public License for more details. You should have
56 | received a copy of the GNU General Public License along with File Hunter;
57 | if not, write to the Free Software Foundation, Inc., 59 Temple Place,
58 | Suite 330, Boston, MA 02111-1307 USA"""
59 |
60 |
61 | info = wx.adv.AboutDialogInfo()
62 |
63 | info.SetIcon(wx.Icon('hunter.png', wx.BITMAP_TYPE_PNG))
64 | info.SetName('File Hunter')
65 | info.SetVersion('1.0')
66 | info.SetDescription(description)
67 | info.SetCopyright('(C) 2007 - 2018 Jan Bodnar')
68 | info.SetWebSite('http://www.zetcode.com')
69 | info.SetLicence(licence)
70 | info.AddDeveloper('Jan Bodnar')
71 | info.AddDocWriter('Jan Bodnar')
72 | info.AddArtist('The Tango crew')
73 | info.AddTranslator('Jan Bodnar')
74 |
75 | wx.adv.AboutBox(info)
76 |
77 |
78 | def main():
79 |
80 | app = wx.App()
81 | ex = Example(None)
82 | ex.Show()
83 | app.MainLoop()
84 |
85 |
86 | if __name__ == '__main__':
87 | main()
88 |
--------------------------------------------------------------------------------
/dialogs/color.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/dialogs/color.png
--------------------------------------------------------------------------------
/dialogs/custom_dialog.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this code example, we create a
7 | custom dialog.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: April 2018
12 | """
13 |
14 | import wx
15 |
16 | class 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 |
66 | class 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 |
88 | def OnChangeDepth(self, e):
89 |
90 | cdDialog = ChangeDepthDialog(None,
91 | title='Change Color Depth')
92 | cdDialog.ShowModal()
93 | cdDialog.Destroy()
94 |
95 |
96 | def main():
97 |
98 | app = wx.App()
99 | ex = Example(None)
100 | ex.Show()
101 | app.MainLoop()
102 |
103 |
104 | if __name__ == '__main__':
105 | main()
106 |
--------------------------------------------------------------------------------
/dialogs/hunter.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/dialogs/hunter.png
--------------------------------------------------------------------------------
/dialogs/message_box.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This example shows a simple
7 | message box.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: April 2018
12 | """
13 |
14 | import wx
15 |
16 |
17 | class Example(wx.Frame):
18 |
19 | def __init__(self, *args, **kwargs):
20 | super(Example, self).__init__(*args, **kwargs)
21 |
22 | self.InitUI()
23 |
24 | def InitUI(self):
25 |
26 | wx.CallLater(3000, self.ShowMessage)
27 |
28 | self.SetSize((300, 200))
29 | self.SetTitle('Message box')
30 | self.Centre()
31 |
32 | def ShowMessage(self):
33 | wx.MessageBox('Download completed', 'Info',
34 | wx.OK | wx.ICON_INFORMATION)
35 |
36 |
37 | def main():
38 |
39 | app = wx.App()
40 | ex = Example(None)
41 | ex.Show()
42 | app.MainLoop()
43 |
44 |
45 | if __name__ == '__main__':
46 | main()
47 |
--------------------------------------------------------------------------------
/dialogs/message_dialogs.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This example shows four types of
7 | message dialogs.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: April 2018
12 | """
13 |
14 | import wx
15 |
16 |
17 | class Example(wx.Frame):
18 |
19 | def __init__(self, *args, **kwargs):
20 | super(Example, self).__init__(*args, **kwargs)
21 |
22 | self.InitUI()
23 |
24 | def InitUI(self):
25 |
26 | panel = wx.Panel(self)
27 |
28 | hbox = wx.BoxSizer()
29 | sizer = wx.GridSizer(2, 2, 2, 2)
30 |
31 | btn1 = wx.Button(panel, label='Info')
32 | btn2 = wx.Button(panel, label='Error')
33 | btn3 = wx.Button(panel, label='Question')
34 | btn4 = wx.Button(panel, label='Alert')
35 |
36 | sizer.AddMany([btn1, btn2, btn3, btn4])
37 |
38 | hbox.Add(sizer, 0, wx.ALL, 15)
39 | panel.SetSizer(hbox)
40 |
41 | btn1.Bind(wx.EVT_BUTTON, self.ShowMessage1)
42 | btn2.Bind(wx.EVT_BUTTON, self.ShowMessage2)
43 | btn3.Bind(wx.EVT_BUTTON, self.ShowMessage3)
44 | btn4.Bind(wx.EVT_BUTTON, self.ShowMessage4)
45 |
46 | self.SetSize((300, 200))
47 | self.SetTitle('Messages')
48 | self.Centre()
49 |
50 | def ShowMessage1(self, event):
51 | dial = wx.MessageDialog(None, 'Download completed', 'Info', wx.OK)
52 | dial.ShowModal()
53 |
54 | def ShowMessage2(self, event):
55 | dial = wx.MessageDialog(None, 'Error loading file', 'Error',
56 | wx.OK | wx.ICON_ERROR)
57 | dial.ShowModal()
58 |
59 | def ShowMessage3(self, event):
60 | dial = wx.MessageDialog(None, 'Are you sure to quit?', 'Question',
61 | wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
62 | dial.ShowModal()
63 |
64 | def ShowMessage4(self, event):
65 | dial = wx.MessageDialog(None, 'Unallowed operation', 'Exclamation',
66 | wx.OK | wx.ICON_EXCLAMATION)
67 | dial.ShowModal()
68 |
69 |
70 | def main():
71 |
72 | app = wx.App()
73 | ex = Example(None)
74 | ex.Show()
75 | app.MainLoop()
76 |
77 |
78 | if __name__ == '__main__':
79 | main()
80 |
--------------------------------------------------------------------------------
/dragdrop/dragdrop_file.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example, we drag and drop files.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: May 2018
11 | """
12 |
13 | import wx
14 |
15 | class FileDrop(wx.FileDropTarget):
16 |
17 | def __init__(self, window):
18 |
19 | wx.FileDropTarget.__init__(self)
20 | self.window = window
21 |
22 | def OnDropFiles(self, x, y, filenames):
23 |
24 | for name in filenames:
25 |
26 | try:
27 | file = open(name, 'r')
28 | text = file.read()
29 | self.window.WriteText(text)
30 |
31 | except IOError as error:
32 |
33 | msg = "Error opening file\n {}".format(str(error))
34 | dlg = wx.MessageDialog(None, msg)
35 | dlg.ShowModal()
36 |
37 | return False
38 |
39 | except UnicodeDecodeError as error:
40 |
41 | msg = "Cannot open non ascii files\n {}".format(str(error))
42 | dlg = wx.MessageDialog(None, msg)
43 | dlg.ShowModal()
44 |
45 | return False
46 |
47 | finally:
48 |
49 | file.close()
50 |
51 | return True
52 |
53 | class Example(wx.Frame):
54 |
55 | def __init__(self, *args, **kw):
56 | super(Example, self).__init__(*args, **kw)
57 |
58 | self.InitUI()
59 |
60 | def InitUI(self):
61 |
62 | self.text = wx.TextCtrl(self, style = wx.TE_MULTILINE)
63 | dt = FileDrop(self.text)
64 |
65 | self.text.SetDropTarget(dt)
66 |
67 | self.SetTitle('File drag and drop')
68 | self.Centre()
69 |
70 |
71 | def main():
72 |
73 | app = wx.App()
74 | ex = Example(None)
75 | ex.Show()
76 | app.MainLoop()
77 |
78 |
79 | if __name__ == '__main__':
80 | main()
81 |
--------------------------------------------------------------------------------
/dragdrop/dragdrop_text.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example, we drag and drop text data.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: May 2018
11 | """
12 |
13 | from pathlib import Path
14 | import os
15 | import wx
16 |
17 | class MyTextDropTarget(wx.TextDropTarget):
18 |
19 | def __init__(self, object):
20 |
21 | wx.TextDropTarget.__init__(self)
22 | self.object = object
23 |
24 | def OnDropText(self, x, y, data):
25 |
26 | self.object.InsertItem(0, data)
27 | return True
28 |
29 |
30 | class Example(wx.Frame):
31 |
32 | def __init__(self, *args, **kw):
33 | super(Example, self).__init__(*args, **kw)
34 |
35 | self.InitUI()
36 |
37 | def InitUI(self):
38 |
39 | splitter1 = wx.SplitterWindow(self, style=wx.SP_3D)
40 | splitter2 = wx.SplitterWindow(splitter1, style=wx.SP_3D)
41 |
42 | home_dir = str(Path.home())
43 |
44 | self.dirWid = wx.GenericDirCtrl(splitter1, dir=home_dir, style=wx.DIRCTRL_DIR_ONLY)
45 | self.lc1 = wx.ListCtrl(splitter2, style=wx.LC_LIST)
46 | self.lc2 = wx.ListCtrl(splitter2, style=wx.LC_LIST)
47 |
48 | dt = MyTextDropTarget(self.lc2)
49 | self.lc2.SetDropTarget(dt)
50 | self.Bind(wx.EVT_LIST_BEGIN_DRAG, self.OnDragInit, id=self.lc1.GetId())
51 |
52 | tree = self.dirWid.GetTreeCtrl()
53 |
54 | splitter2.SplitHorizontally(self.lc1, self.lc2, 150)
55 | splitter1.SplitVertically(self.dirWid, splitter2, 200)
56 |
57 | self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelect, id=tree.GetId())
58 |
59 | self.OnSelect(0)
60 |
61 | self.SetTitle('Drag and drop text')
62 | self.Centre()
63 |
64 | def OnSelect(self, event):
65 |
66 | list = os.listdir(self.dirWid.GetPath())
67 |
68 | self.lc1.ClearAll()
69 | self.lc2.ClearAll()
70 |
71 | for i in range(len(list)):
72 |
73 | if list[i][0] != '.':
74 | self.lc1.InsertItem(0, list[i])
75 |
76 | def OnDragInit(self, event):
77 |
78 | text = self.lc1.GetItemText(event.GetIndex())
79 | tdo = wx.TextDataObject(text)
80 | tds = wx.DropSource(self.lc1)
81 |
82 | tds.SetData(tdo)
83 | tds.DoDragDrop(True)
84 |
85 |
86 | def main():
87 |
88 | app = wx.App()
89 | ex = Example(None)
90 | ex.Show()
91 | app.MainLoop()
92 |
93 |
94 | if __name__ == '__main__':
95 | main()
96 |
--------------------------------------------------------------------------------
/events/custom_ids.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example we use custom event ids.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: April 2018
11 | """
12 |
13 | import wx
14 |
15 | ID_MENU_NEW = wx.NewIdRef()
16 | ID_MENU_OPEN = wx.NewIdRef()
17 | ID_MENU_SAVE = wx.NewIdRef()
18 |
19 |
20 | class Example(wx.Frame):
21 |
22 | def __init__(self, *args, **kw):
23 | super(Example, self).__init__(*args, **kw)
24 |
25 | self.InitUI()
26 |
27 | def InitUI(self):
28 |
29 | self.CreateMenuBar()
30 | self.CreateStatusBar()
31 |
32 | self.SetSize((350, 250))
33 | self.SetTitle('Custom ids')
34 | self.Centre()
35 |
36 | def CreateMenuBar(self):
37 |
38 | mb = wx.MenuBar()
39 |
40 | fMenu = wx.Menu()
41 | fMenu.Append(ID_MENU_NEW, 'New')
42 | fMenu.Append(ID_MENU_OPEN, 'Open')
43 | fMenu.Append(ID_MENU_SAVE, 'Save')
44 |
45 | mb.Append(fMenu, '&File')
46 | self.SetMenuBar(mb)
47 |
48 | self.Bind(wx.EVT_MENU, self.DisplayMessage, id=ID_MENU_NEW)
49 | self.Bind(wx.EVT_MENU, self.DisplayMessage, id=ID_MENU_OPEN)
50 | self.Bind(wx.EVT_MENU, self.DisplayMessage, id=ID_MENU_SAVE)
51 |
52 | def DisplayMessage(self, e):
53 |
54 | sb = self.GetStatusBar()
55 |
56 | eid = e.GetId()
57 |
58 | if eid == ID_MENU_NEW:
59 | msg = 'New menu item selected'
60 | elif eid == ID_MENU_OPEN:
61 | msg = 'Open menu item selected'
62 | elif eid == ID_MENU_SAVE:
63 | msg = 'Save menu item selected'
64 |
65 | sb.SetStatusText(msg)
66 |
67 |
68 | def main():
69 |
70 | app = wx.App()
71 | ex = Example(None)
72 | ex.Show()
73 | app.MainLoop()
74 |
75 |
76 | if __name__ == '__main__':
77 | main()
78 |
--------------------------------------------------------------------------------
/events/default_ids.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example we use automatic ids
7 | with wx.ID_ANY.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: April 2018
12 | """
13 |
14 | import wx
15 |
16 |
17 | class Example(wx.Frame):
18 |
19 | def __init__(self, *args, **kw):
20 | super(Example, self).__init__(*args, **kw)
21 |
22 | self.InitUI()
23 |
24 | def InitUI(self):
25 |
26 | pnl = wx.Panel(self)
27 | exitButton = wx.Button(pnl, wx.ID_ANY, 'Exit', (10, 10))
28 |
29 | self.Bind(wx.EVT_BUTTON, self.OnExit, id=exitButton.GetId())
30 |
31 | self.SetTitle("Automatic ids")
32 | self.Centre()
33 |
34 | def OnExit(self, event):
35 |
36 | self.Close()
37 |
38 |
39 | def main():
40 |
41 | app = wx.App()
42 | ex = Example(None)
43 | ex.Show()
44 | app.MainLoop()
45 |
46 |
47 | if __name__ == '__main__':
48 | main()
49 |
--------------------------------------------------------------------------------
/events/event_propagation.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This example demonstrates event propagation.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: April 2018
11 | """
12 |
13 | import wx
14 |
15 | class MyPanel(wx.Panel):
16 |
17 | def __init__(self, *args, **kw):
18 | super(MyPanel, self).__init__(*args, **kw)
19 |
20 | self.Bind(wx.EVT_BUTTON, self.OnButtonClicked)
21 |
22 | def OnButtonClicked(self, e):
23 |
24 | print('event reached panel class')
25 | e.Skip()
26 |
27 |
28 | class MyButton(wx.Button):
29 |
30 | def __init__(self, *args, **kw):
31 | super(MyButton, self).__init__(*args, **kw)
32 |
33 | self.Bind(wx.EVT_BUTTON, self.OnButtonClicked)
34 |
35 | def OnButtonClicked(self, e):
36 |
37 | print('event reached button class')
38 | e.Skip()
39 |
40 |
41 | class Example(wx.Frame):
42 |
43 | def __init__(self, *args, **kw):
44 | super(Example, self).__init__(*args, **kw)
45 |
46 | self.InitUI()
47 |
48 |
49 | def InitUI(self):
50 |
51 | mpnl = MyPanel(self)
52 |
53 | MyButton(mpnl, label='Ok', pos=(15, 15))
54 |
55 | self.Bind(wx.EVT_BUTTON, self.OnButtonClicked)
56 |
57 | self.SetTitle('Propagate event')
58 | self.Centre()
59 |
60 | def OnButtonClicked(self, e):
61 |
62 | print('event reached frame class')
63 | e.Skip()
64 |
65 |
66 | def main():
67 |
68 | app = wx.App()
69 | ex = Example(None)
70 | ex.Show()
71 | app.MainLoop()
72 |
73 |
74 | if __name__ == '__main__':
75 | main()
76 |
--------------------------------------------------------------------------------
/events/event_veto.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | import wx
4 |
5 | """
6 | ZetCode wxPython tutorial
7 |
8 | In this example we veto an event.
9 |
10 | author: Jan Bodnar
11 | website: www.zetcode.com
12 | last modified: April 2018
13 | """
14 |
15 | class Example(wx.Frame):
16 |
17 | def __init__(self, *args, **kw):
18 | super(Example, self).__init__(*args, **kw)
19 |
20 | self.InitUI()
21 |
22 | def InitUI(self):
23 |
24 | self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
25 |
26 | self.SetTitle('Event veto')
27 | self.Centre()
28 |
29 | def OnCloseWindow(self, e):
30 |
31 | dial = wx.MessageDialog(None, 'Are you sure to quit?', 'Question',
32 | wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
33 |
34 | ret = dial.ShowModal()
35 |
36 | if ret == wx.ID_YES:
37 | self.Destroy()
38 | else:
39 | e.Veto()
40 |
41 |
42 | def main():
43 |
44 | app = wx.App()
45 | ex = Example(None)
46 | ex.Show()
47 | app.MainLoop()
48 |
49 |
50 | if __name__ == '__main__':
51 | main()
52 |
--------------------------------------------------------------------------------
/events/focus_event.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example we work with wx.FocusEvent.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: April 2018
11 | """
12 |
13 | import wx
14 |
15 | class MyWindow(wx.Panel):
16 |
17 | def __init__(self, parent):
18 | super(MyWindow, self).__init__(parent)
19 |
20 | self.color = '#b3b3b3'
21 |
22 | self.Bind(wx.EVT_PAINT, self.OnPaint)
23 | self.Bind(wx.EVT_SIZE, self.OnSize)
24 | self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
25 | self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
26 |
27 | def OnPaint(self, e):
28 |
29 | dc = wx.PaintDC(self)
30 |
31 | dc.SetPen(wx.Pen(self.color))
32 | x, y = self.GetSize()
33 | dc.DrawRectangle(0, 0, x, y)
34 |
35 | def OnSize(self, e):
36 |
37 | self.Refresh()
38 |
39 | def OnSetFocus(self, e):
40 |
41 | self.color = '#ff0000'
42 | self.Refresh()
43 |
44 | def OnKillFocus(self, e):
45 |
46 | self.color = '#b3b3b3'
47 | self.Refresh()
48 |
49 |
50 | class Example(wx.Frame):
51 |
52 | def __init__(self, *args, **kw):
53 | super(Example, self).__init__(*args, **kw)
54 |
55 | self.InitUI()
56 |
57 |
58 | def InitUI(self):
59 |
60 | grid = wx.GridSizer(2, 2, 10, 10)
61 | grid.AddMany([(MyWindow(self), 0, wx.EXPAND|wx.TOP|wx.LEFT, 9),
62 | (MyWindow(self), 0, wx.EXPAND|wx.TOP|wx.RIGHT, 9),
63 | (MyWindow(self), 0, wx.EXPAND|wx.BOTTOM|wx.LEFT, 9),
64 | (MyWindow(self), 0, wx.EXPAND|wx.BOTTOM|wx.RIGHT, 9)])
65 |
66 |
67 | self.SetSizer(grid)
68 |
69 | self.SetSize((350, 250))
70 | self.SetTitle('Focus event')
71 | self.Centre()
72 |
73 |
74 | def OnMove(self, e):
75 |
76 | print(e.GetEventObject())
77 | x, y = e.GetPosition()
78 | self.st1.SetLabel(str(x))
79 | self.st2.SetLabel(str(y))
80 |
81 |
82 | def main():
83 |
84 | app = wx.App()
85 | ex = Example(None)
86 | ex.Show()
87 | app.MainLoop()
88 |
89 |
90 | if __name__ == '__main__':
91 | main()
92 |
--------------------------------------------------------------------------------
/events/key_event.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example we work with wx.KeyEvent.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: April 2018
11 | """
12 |
13 | import wx
14 |
15 | class Example(wx.Frame):
16 |
17 | def __init__(self, *args, **kw):
18 | super(Example, self).__init__(*args, **kw)
19 |
20 | self.InitUI()
21 |
22 | def InitUI(self):
23 |
24 | pnl = wx.Panel(self)
25 | pnl.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
26 | pnl.SetFocus()
27 |
28 | self.SetSize((350, 250))
29 | self.SetTitle('Key event')
30 | self.Centre()
31 |
32 | def OnKeyDown(self, e):
33 |
34 | key = e.GetKeyCode()
35 |
36 | if key == wx.WXK_ESCAPE:
37 |
38 | ret = wx.MessageBox('Are you sure to quit?', 'Question',
39 | wx.YES_NO | wx.NO_DEFAULT, self)
40 |
41 | if ret == wx.YES:
42 | self.Close()
43 |
44 |
45 | def main():
46 |
47 | app = wx.App()
48 | ex = Example(None)
49 | ex.Show()
50 | app.MainLoop()
51 |
52 |
53 | if __name__ == '__main__':
54 | main()
55 |
--------------------------------------------------------------------------------
/events/paint_event.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example we count paint events.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: April 2018
11 | """
12 |
13 | import wx
14 |
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | self.count = 0
26 | self.Bind(wx.EVT_PAINT, self.OnPaint)
27 |
28 | self.SetTitle('Paint events')
29 | self.SetSize((350, 250))
30 | self.Centre()
31 |
32 | def OnPaint(self, e):
33 |
34 | self.count += 1
35 | dc = wx.PaintDC(self)
36 | text = "Number of paint events: {0}".format(self.count)
37 | dc.DrawText(text, 20, 20)
38 |
39 |
40 | def main():
41 |
42 | app = wx.App()
43 | ex = Example(None)
44 | ex.Show()
45 | app.MainLoop()
46 |
47 |
48 | if __name__ == '__main__':
49 | main()
50 |
--------------------------------------------------------------------------------
/events/simple_event.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This is a wx.MoveEvent event demostration.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: April 2018
11 | """
12 |
13 | import wx
14 |
15 | class Example(wx.Frame):
16 |
17 | def __init__(self, *args, **kw):
18 | super(Example, self).__init__(*args, **kw)
19 |
20 | self.InitUI()
21 |
22 |
23 | def InitUI(self):
24 |
25 | wx.StaticText(self, label='x:', pos=(10,10))
26 | wx.StaticText(self, label='y:', pos=(10,30))
27 |
28 | self.st1 = wx.StaticText(self, label='', pos=(30, 10))
29 | self.st2 = wx.StaticText(self, label='', pos=(30, 30))
30 |
31 | self.Bind(wx.EVT_MOVE, self.OnMove)
32 |
33 | self.SetSize((350, 250))
34 | self.SetTitle('Move event')
35 | self.Centre()
36 |
37 | def OnMove(self, e):
38 |
39 | x, y = e.GetPosition()
40 | self.st1.SetLabel(str(x))
41 | self.st2.SetLabel(str(y))
42 |
43 |
44 | def main():
45 |
46 | app = wx.App()
47 | ex = Example(None)
48 | ex.Show()
49 | app.MainLoop()
50 |
51 |
52 | if __name__ == '__main__':
53 | main()
54 |
--------------------------------------------------------------------------------
/events/standard_ids.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example we create buttons with standard ids.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: April 2018
11 | """
12 |
13 | import wx
14 |
15 | class Example(wx.Frame):
16 |
17 | def __init__(self, *args, **kw):
18 | super(Example, self).__init__(*args, **kw)
19 |
20 | self.InitUI()
21 |
22 | def InitUI(self):
23 |
24 | pnl = wx.Panel(self)
25 | grid = wx.GridSizer(3, 2, 1, 1)
26 |
27 | grid.AddMany([(wx.Button(pnl, wx.ID_CANCEL), 0, wx.TOP | wx.LEFT, 9),
28 | (wx.Button(pnl, wx.ID_DELETE), 0, wx.TOP, 9),
29 | (wx.Button(pnl, wx.ID_SAVE), 0, wx.LEFT, 9),
30 | (wx.Button(pnl, wx.ID_EXIT)),
31 | (wx.Button(pnl, wx.ID_STOP), 0, wx.LEFT, 9),
32 | (wx.Button(pnl, wx.ID_NEW))])
33 |
34 | self.Bind(wx.EVT_BUTTON, self.OnQuitApp, id=wx.ID_EXIT)
35 |
36 | pnl.SetSizer(grid)
37 |
38 | self.SetTitle("Standard ids")
39 | self.Centre()
40 |
41 | def OnQuitApp(self, event):
42 |
43 | self.Close()
44 |
45 |
46 | def main():
47 |
48 | app = wx.App()
49 | ex = Example(None)
50 | ex.Show()
51 | app.MainLoop()
52 |
53 |
54 | if __name__ == '__main__':
55 | main()
56 |
--------------------------------------------------------------------------------
/graphics/brushes.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program draws eight rectangles filled
7 | with different brushes.
8 |
9 | author: Jan Bodnar
10 | website: zetcode.com
11 | last edited: May 2018
12 | """
13 |
14 | import wx
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | self.Bind(wx.EVT_PAINT, self.OnPaint)
26 |
27 | self.SetTitle("Brushes")
28 | self.Centre()
29 |
30 | def OnPaint(self, e):
31 |
32 | dc = wx.PaintDC(self)
33 |
34 | dc.SetBrush(wx.Brush('#4c4c4c', wx.CROSS_HATCH))
35 | dc.DrawRectangle(10, 15, 90, 60)
36 |
37 | dc.SetBrush(wx.Brush('#4c4c4c', wx.SOLID))
38 | dc.DrawRectangle(130, 15, 90, 60)
39 |
40 | dc.SetBrush(wx.Brush('#4c4c4c', wx.BDIAGONAL_HATCH))
41 | dc.DrawRectangle(250, 15, 90, 60)
42 |
43 | dc.SetBrush(wx.Brush('#4c4c4c', wx.CROSSDIAG_HATCH))
44 | dc.DrawRectangle(10, 105, 90, 60)
45 |
46 | dc.SetBrush(wx.Brush('#4c4c4c', wx.FDIAGONAL_HATCH))
47 | dc.DrawRectangle(130, 105, 90, 60)
48 |
49 | dc.SetBrush(wx.Brush('#4c4c4c', wx.HORIZONTAL_HATCH))
50 | dc.DrawRectangle(250, 105, 90, 60)
51 |
52 | dc.SetBrush(wx.Brush('#4c4c4c', wx.VERTICAL_HATCH))
53 | dc.DrawRectangle(10, 195, 90, 60)
54 |
55 | dc.SetBrush(wx.Brush('#4c4c4c', wx.TRANSPARENT))
56 | dc.DrawRectangle(130, 195, 90, 60)
57 |
58 |
59 | def main():
60 |
61 | app = wx.App()
62 | ex = Example(None)
63 | ex.Show()
64 | app.MainLoop()
65 |
66 |
67 | if __name__ == '__main__':
68 | main()
69 |
--------------------------------------------------------------------------------
/graphics/colours.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program draws nine coloured rectangles
7 | on the window.
8 |
9 | author: Jan Bodnar
10 | website: zetcode.com
11 | last edited: May 2018
12 | """
13 |
14 | import wx
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | self.Bind(wx.EVT_PAINT, self.OnPaint)
26 |
27 | self.SetTitle("Colours")
28 | self.Centre()
29 |
30 |
31 | def OnPaint(self, e):
32 |
33 | dc = wx.PaintDC(self)
34 | dc.SetPen(wx.Pen('#d4d4d4'))
35 |
36 | dc.SetBrush(wx.Brush('#c56c00'))
37 | dc.DrawRectangle(10, 15, 90, 60)
38 |
39 | dc.SetBrush(wx.Brush('#1ac500'))
40 | dc.DrawRectangle(130, 15, 90, 60)
41 |
42 | dc.SetBrush(wx.Brush('#539e47'))
43 | dc.DrawRectangle(250, 15, 90, 60)
44 |
45 | dc.SetBrush(wx.Brush('#004fc5'))
46 | dc.DrawRectangle(10, 105, 90, 60)
47 |
48 | dc.SetBrush(wx.Brush('#c50024'))
49 | dc.DrawRectangle(130, 105, 90, 60)
50 |
51 | dc.SetBrush(wx.Brush('#9e4757'))
52 | dc.DrawRectangle(250, 105, 90, 60)
53 |
54 | dc.SetBrush(wx.Brush('#5f3b00'))
55 | dc.DrawRectangle(10, 195, 90, 60)
56 |
57 | dc.SetBrush(wx.Brush('#4c4c4c'))
58 | dc.DrawRectangle(130, 195, 90, 60)
59 |
60 | dc.SetBrush(wx.Brush('#785f36'))
61 | dc.DrawRectangle(250, 195, 90, 60)
62 |
63 |
64 | def main():
65 |
66 | app = wx.App()
67 | ex = Example(None)
68 | ex.Show()
69 | app.MainLoop()
70 |
71 |
72 | if __name__ == '__main__':
73 | main()
74 |
--------------------------------------------------------------------------------
/graphics/custom_patterns.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program draws three rectangles with custom
7 | brush patterns.
8 |
9 | author: Jan Bodnar
10 | website: zetcode.com
11 | last edited: May 2018
12 | """
13 |
14 | import wx
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | self.Bind(wx.EVT_PAINT, self.OnPaint)
26 |
27 | self.SetTitle("Custom patterns")
28 | self.Centre()
29 |
30 | def OnPaint(self, e):
31 |
32 | dc = wx.PaintDC(self)
33 |
34 | dc.SetPen(wx.Pen('#C7C3C3'))
35 |
36 | brush1 = wx.Brush(wx.Bitmap('pattern1.png'))
37 | dc.SetBrush(brush1)
38 | dc.DrawRectangle(10, 15, 90, 60)
39 |
40 | brush2 = wx.Brush(wx.Bitmap('pattern2.png'))
41 | dc.SetBrush(brush2)
42 | dc.DrawRectangle(130, 15, 90, 60)
43 |
44 | brush3 = wx.Brush(wx.Bitmap('pattern3.png'))
45 | dc.SetBrush(brush3)
46 | dc.DrawRectangle(250, 15, 90, 60)
47 |
48 |
49 | def main():
50 |
51 | app = wx.App()
52 | ex = Example(None)
53 | ex.Show()
54 | app.MainLoop()
55 |
56 |
57 | if __name__ == '__main__':
58 | main()
59 |
--------------------------------------------------------------------------------
/graphics/draw_line.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program draws a line on the
7 | frame window after a while.
8 |
9 | author: Jan Bodnar
10 | website: zetcode.com
11 | last edited: May 2018
12 | """
13 |
14 | import wx
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | wx.CallLater(2000, self.DrawLine)
26 |
27 | self.SetTitle("Line")
28 | self.Centre()
29 |
30 | def DrawLine(self):
31 |
32 | dc = wx.ClientDC(self)
33 | dc.DrawLine(50, 60, 190, 60)
34 |
35 | def main():
36 |
37 | app = wx.App()
38 | ex = Example(None)
39 | ex.Show()
40 | app.MainLoop()
41 |
42 |
43 | if __name__ == '__main__':
44 | main()
45 |
--------------------------------------------------------------------------------
/graphics/draw_line2.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program draws a line in
7 | a paint event.
8 |
9 | author: Jan Bodnar
10 | website: zetcode.com
11 | last edited: May 2018
12 | """
13 |
14 | import wx
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | self.Bind(wx.EVT_PAINT, self.OnPaint)
26 |
27 | self.SetTitle("Line")
28 | self.Centre()
29 |
30 | def OnPaint(self, e):
31 |
32 | dc = wx.PaintDC(self)
33 | dc.DrawLine(50, 60, 190, 60)
34 |
35 |
36 | def main():
37 |
38 | app = wx.App()
39 | ex = Example(None)
40 | ex.Show()
41 | app.MainLoop()
42 |
43 |
44 | if __name__ == '__main__':
45 | main()
46 |
--------------------------------------------------------------------------------
/graphics/gradients.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program draws four rectangles filled
7 | with gradients.
8 |
9 | author: Jan Bodnar
10 | website: zetcode.com
11 | last edited: May 2018
12 | """
13 |
14 | import wx
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | self.Bind(wx.EVT_PAINT, self.OnPaint)
26 |
27 | self.SetTitle("Gradients")
28 | self.Centre()
29 |
30 | def OnPaint(self, event):
31 |
32 | dc = wx.PaintDC(self)
33 |
34 | dc.GradientFillLinear((20, 20, 180, 40), '#ffec00', '#000000', wx.NORTH)
35 | dc.GradientFillLinear((20, 80, 180, 40), '#ffec00', '#000000', wx.SOUTH)
36 | dc.GradientFillLinear((20, 140, 180, 40), '#ffec00', '#000000', wx.EAST)
37 | dc.GradientFillLinear((20, 200, 180, 40), '#ffec00', '#000000', wx.WEST)
38 |
39 |
40 | def main():
41 |
42 | app = wx.App()
43 | ex = Example(None)
44 | ex.Show()
45 | app.MainLoop()
46 |
47 |
48 | if __name__ == '__main__':
49 | main()
50 |
--------------------------------------------------------------------------------
/graphics/granite.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/graphics/granite.png
--------------------------------------------------------------------------------
/graphics/joins_caps.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program draws uses different joins
7 | and caps in drawing.
8 |
9 | author: Jan Bodnar
10 | website: zetcode.com
11 | last edited: May 2018
12 | """
13 |
14 | import wx
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | self.Bind(wx.EVT_PAINT, self.OnPaint)
26 |
27 | self.SetTitle("Joins and caps")
28 | self.Centre()
29 |
30 | def OnPaint(self, e):
31 |
32 | dc = wx.PaintDC(self)
33 |
34 | pen = wx.Pen('#4c4c4c', 10, wx.SOLID)
35 |
36 | pen.SetJoin(wx.JOIN_MITER)
37 | dc.SetPen(pen)
38 | dc.DrawRectangle(15, 15, 80, 50)
39 |
40 | pen.SetJoin(wx.JOIN_BEVEL)
41 | dc.SetPen(pen)
42 | dc.DrawRectangle(125, 15, 80, 50)
43 |
44 | pen.SetJoin(wx.JOIN_ROUND)
45 | dc.SetPen(pen)
46 | dc.DrawRectangle(235, 15, 80, 50)
47 |
48 | pen.SetCap(wx.CAP_BUTT)
49 | dc.SetPen(pen)
50 | dc.DrawLine(30, 150, 150, 150)
51 |
52 | pen.SetCap(wx.CAP_PROJECTING)
53 | dc.SetPen(pen)
54 | dc.DrawLine(30, 190, 150, 190)
55 |
56 | pen.SetCap(wx.CAP_ROUND)
57 | dc.SetPen(pen)
58 | dc.DrawLine(30, 230, 150, 230)
59 |
60 | pen2 = wx.Pen('#4c4c4c', 1, wx.SOLID)
61 | dc.SetPen(pen2)
62 | dc.DrawLine(30, 130, 30, 250)
63 | dc.DrawLine(150, 130, 150, 250)
64 | dc.DrawLine(155, 130, 155, 250)
65 |
66 |
67 | def main():
68 |
69 | app = wx.App()
70 | ex = Example(None)
71 | ex.Show()
72 | app.MainLoop()
73 |
74 |
75 | if __name__ == '__main__':
76 | main()
77 |
--------------------------------------------------------------------------------
/graphics/lines.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program draws various shapes on
7 | the window.
8 |
9 | author: Jan Bodnar
10 | website: zetcode.com
11 | last edited: May 2018
12 | """
13 |
14 | import wx
15 | from math import hypot, sin, cos, pi
16 |
17 | class Example(wx.Frame):
18 |
19 | def __init__(self, *args, **kw):
20 | super(Example, self).__init__(*args, **kw)
21 |
22 | self.InitUI()
23 |
24 | def InitUI(self):
25 |
26 | self.Bind(wx.EVT_PAINT, self.OnPaint)
27 |
28 | self.SetTitle('Lines')
29 | self.Centre()
30 |
31 | def OnPaint(self, e):
32 |
33 | dc = wx.PaintDC(self)
34 | size_x, size_y = self.GetClientSize()
35 | dc.SetDeviceOrigin(size_x/2, size_y/2)
36 |
37 | radius = hypot(size_x/2, size_y/2)
38 | angle = 0
39 |
40 | while (angle < 2*pi):
41 |
42 | x = radius*cos(angle)
43 | y = radius*sin(angle)
44 | dc.DrawLine((0, 0), (x, y))
45 |
46 | angle = angle + 2*pi/360
47 |
48 | def main():
49 |
50 | app = wx.App()
51 | ex = Example(None)
52 | ex.Show()
53 | app.MainLoop()
54 |
55 |
56 | if __name__ == '__main__':
57 | main()
58 |
--------------------------------------------------------------------------------
/graphics/pattern1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/graphics/pattern1.png
--------------------------------------------------------------------------------
/graphics/pattern2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/graphics/pattern2.png
--------------------------------------------------------------------------------
/graphics/pattern3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/graphics/pattern3.png
--------------------------------------------------------------------------------
/graphics/pens.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program draws six rectangles with different
7 | pens.
8 |
9 | author: Jan Bodnar
10 | website: zetcode.com
11 | last edited: May 2018
12 | """
13 |
14 | import wx
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | self.Bind(wx.EVT_PAINT, self.OnPaint)
26 |
27 | self.SetTitle("Pens")
28 | self.Centre()
29 |
30 | def OnPaint(self, event):
31 | dc = wx.PaintDC(self)
32 |
33 | dc.SetPen(wx.Pen('#4c4c4c', 1, wx.SOLID))
34 | dc.DrawRectangle(10, 15, 90, 60)
35 |
36 | dc.SetPen(wx.Pen('#4c4c4c', 1, wx.DOT))
37 | dc.DrawRectangle(130, 15, 90, 60)
38 |
39 | dc.SetPen(wx.Pen('#4c4c4c', 1, wx.LONG_DASH))
40 | dc.DrawRectangle(250, 15, 90, 60)
41 |
42 | dc.SetPen(wx.Pen('#4c4c4c', 1, wx.SHORT_DASH))
43 | dc.DrawRectangle(10, 105, 90, 60)
44 |
45 | dc.SetPen(wx.Pen('#4c4c4c', 1, wx.DOT_DASH))
46 | dc.DrawRectangle(130, 105, 90, 60)
47 |
48 | dc.SetPen(wx.Pen('#4c4c4c', 1, wx.TRANSPARENT))
49 | dc.DrawRectangle(250, 105, 90, 60)
50 |
51 |
52 | def main():
53 |
54 | app = wx.App()
55 | ex = Example(None)
56 | ex.Show()
57 | app.MainLoop()
58 |
59 |
60 | if __name__ == '__main__':
61 | main()
62 |
--------------------------------------------------------------------------------
/graphics/points.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program draws one thousand points
7 | randomly on the window.
8 |
9 | author: Jan Bodnar
10 | website: zetcode.com
11 | last edited: May 2018
12 | """
13 |
14 | import wx
15 | import random
16 |
17 | class Example(wx.Frame):
18 |
19 | def __init__(self, *args, **kw):
20 | super(Example, self).__init__(*args, **kw)
21 |
22 | self.InitUI()
23 |
24 | def InitUI(self):
25 |
26 | self.Bind(wx.EVT_PAINT, self.OnPaint)
27 |
28 | self.SetTitle("Points")
29 | self.Centre()
30 |
31 | def OnPaint(self, e):
32 |
33 | dc = wx.PaintDC(self)
34 |
35 | dc.SetPen(wx.Pen('RED'))
36 |
37 | for i in range(1000):
38 |
39 | w, h = self.GetSize()
40 | x = random.randint(1, w-1)
41 | y = random.randint(1, h-1)
42 | dc.DrawPoint(x, y)
43 |
44 |
45 | def main():
46 |
47 | app = wx.App()
48 | ex = Example(None)
49 | ex.Show()
50 | app.MainLoop()
51 |
52 |
53 | if __name__ == '__main__':
54 | main()
55 |
--------------------------------------------------------------------------------
/graphics/region_operations.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program performs set operations on regions.
7 |
8 | author: Jan Bodnar
9 | website: zetcode.com
10 | last edited: May 2018
11 | """
12 |
13 | import wx
14 |
15 | class Example(wx.Frame):
16 |
17 | def __init__(self, *args, **kw):
18 | super(Example, self).__init__(*args, **kw)
19 |
20 | self.InitUI()
21 |
22 | def InitUI(self):
23 |
24 | self.Bind(wx.EVT_PAINT, self.OnPaint)
25 |
26 | self.SetTitle("Regions")
27 | self.Centre()
28 |
29 | def OnPaint(self, e):
30 |
31 | dc = wx.PaintDC(self)
32 | dc.SetPen(wx.Pen('#d4d4d4'))
33 |
34 | dc.DrawRectangle(20, 20, 50, 50)
35 | dc.DrawRectangle(30, 40, 50, 50)
36 |
37 | dc.SetBrush(wx.Brush('#ffffff'))
38 | dc.DrawRectangle(100, 20, 50, 50)
39 | dc.DrawRectangle(110, 40, 50, 50)
40 |
41 | region1 = wx.Region(100, 20, 50, 50)
42 | region2 = wx.Region(110, 40, 50, 50)
43 | region1.Intersect(region2)
44 |
45 | rect = region1.GetBox()
46 | dc.SetDeviceClippingRegion(region1)
47 | dc.SetBrush(wx.Brush('#ff0000'))
48 | dc.DrawRectangle(rect)
49 | dc.DestroyClippingRegion()
50 |
51 | dc.SetBrush(wx.Brush('#ffffff'))
52 | dc.DrawRectangle(180, 20, 50, 50)
53 | dc.DrawRectangle(190, 40, 50, 50)
54 |
55 | region1 = wx.Region(180, 20, 50, 50)
56 | region2 = wx.Region(190, 40, 50, 50)
57 | region1.Union(region2)
58 | dc.SetDeviceClippingRegion(region1)
59 |
60 | rect = region1.GetBox()
61 | dc.SetBrush(wx.Brush('#fa8e00'))
62 | dc.DrawRectangle(rect)
63 | dc.DestroyClippingRegion()
64 |
65 | dc.SetBrush(wx.Brush('#ffffff'))
66 | dc.DrawRectangle(20, 120, 50, 50)
67 | dc.DrawRectangle(30, 140, 50, 50)
68 | region1 = wx.Region(20, 120, 50, 50)
69 | region2 = wx.Region(30, 140, 50, 50)
70 | region1.Xor(region2)
71 |
72 | rect = region1.GetBox()
73 | dc.SetDeviceClippingRegion(region1)
74 | dc.SetBrush(wx.Brush('#619e1b'))
75 | dc.DrawRectangle(rect)
76 | dc.DestroyClippingRegion()
77 |
78 | dc.SetBrush(wx.Brush('#ffffff'))
79 | dc.DrawRectangle(100, 120, 50, 50)
80 | dc.DrawRectangle(110, 140, 50, 50)
81 | region1 = wx.Region(100, 120, 50, 50)
82 | region2 = wx.Region(110, 140, 50, 50)
83 | region1.Subtract(region2)
84 |
85 | rect = region1.GetBox()
86 | dc.SetDeviceClippingRegion(region1)
87 | dc.SetBrush(wx.Brush('#715b33'))
88 | dc.DrawRectangle(rect)
89 | dc.DestroyClippingRegion()
90 |
91 | dc.SetBrush(wx.Brush('#ffffff'))
92 | dc.DrawRectangle(180, 120, 50, 50)
93 | dc.DrawRectangle(190, 140, 50, 50)
94 | region1 = wx.Region(180, 120, 50, 50)
95 | region2 = wx.Region(190, 140, 50, 50)
96 | region2.Subtract(region1)
97 |
98 | rect = region2.GetBox()
99 | dc.SetDeviceClippingRegion(region2)
100 | dc.SetBrush(wx.Brush('#0d0060'))
101 | dc.DrawRectangle(rect)
102 | dc.DestroyClippingRegion()
103 |
104 |
105 | def main():
106 |
107 | app = wx.App()
108 | ex = Example(None)
109 | ex.Show()
110 | app.MainLoop()
111 |
112 |
113 | if __name__ == '__main__':
114 | main()
115 |
--------------------------------------------------------------------------------
/graphics/ruler.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program creates a ruler.
7 |
8 | author: Jan Bodnar
9 | website: zetcode.com
10 | last edited: May 2018
11 | """
12 |
13 |
14 | import wx
15 |
16 | RW = 701 # ruler width
17 | RM = 10 # ruler margin
18 | RH = 80 # ruler height
19 |
20 |
21 | class Example(wx.Frame):
22 |
23 | def __init__(self, parent):
24 | wx.Frame.__init__(self, parent, size=(RW + 2*RM, RH),
25 | style=wx.FRAME_NO_TASKBAR | wx.NO_BORDER | wx.STAY_ON_TOP)
26 | self.font = wx.Font(7, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL,
27 | wx.FONTWEIGHT_BOLD, False, 'Courier 10 Pitch')
28 |
29 | self.InitUI()
30 |
31 | def InitUI(self):
32 |
33 | self.Bind(wx.EVT_PAINT, self.OnPaint)
34 | self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
35 | self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
36 | self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
37 | self.Bind(wx.EVT_MOTION, self.OnMouseMove)
38 |
39 | self.Centre()
40 | self.Show(True)
41 |
42 | def OnPaint(self, e):
43 |
44 | dc = wx.PaintDC(self)
45 |
46 | brush = wx.Brush(wx.Bitmap('granite.png'))
47 | dc.SetBrush(brush)
48 | dc.DrawRectangle(0, 0, RW+2*RM, RH)
49 | dc.SetFont(self.font)
50 |
51 | dc.SetPen(wx.Pen('#F8FF25'))
52 | dc.SetTextForeground('#F8FF25')
53 |
54 | for i in range(RW):
55 |
56 | if not (i % 100):
57 |
58 | dc.DrawLine(i+RM, 0, i+RM, 10)
59 | w, h = dc.GetTextExtent(str(i))
60 | dc.DrawText(str(i), i+RM-w/2, 11)
61 |
62 | elif not (i % 20):
63 |
64 | dc.DrawLine(i+RM, 0, i+RM, 8)
65 |
66 | elif not (i % 2):
67 |
68 | dc.DrawLine(i+RM, 0, i+RM, 4)
69 |
70 | def OnLeftDown(self, e):
71 |
72 | x, y = self.ClientToScreen(e.GetPosition())
73 | ox, oy = self.GetPosition()
74 |
75 | dx = x - ox
76 | dy = y - oy
77 |
78 | print(dx, dy)
79 |
80 | self.delta = ((dx, dy))
81 |
82 | def OnMouseMove(self, e):
83 |
84 | if e.Dragging() and e.LeftIsDown():
85 |
86 | self.SetCursor(wx.Cursor(wx.CURSOR_HAND))
87 |
88 | x, y = self.ClientToScreen(e.GetPosition())
89 | fp = (x - self.delta[0], y - self.delta[1])
90 | self.Move(fp)
91 |
92 | def OnLeftUp(self, e):
93 |
94 | self.SetCursor(wx.Cursor(wx.CURSOR_ARROW))
95 |
96 | def OnRightDown(self, e):
97 |
98 | self.Close()
99 |
100 |
101 | def main():
102 |
103 | app = wx.App()
104 | ex = Example(None)
105 | ex.Show()
106 | app.MainLoop()
107 |
108 |
109 | if __name__ == '__main__':
110 | main()
111 |
--------------------------------------------------------------------------------
/graphics/shapes.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program draws various shapes on
7 | the window.
8 |
9 | author: Jan Bodnar
10 | website: zetcode.com
11 | last edited: May 2018
12 | """
13 |
14 | import wx
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | self.Bind(wx.EVT_PAINT, self.OnPaint)
26 |
27 | self.SetTitle("Shapes")
28 | self.Centre()
29 |
30 |
31 | def OnPaint(self, e):
32 |
33 | dc = wx.PaintDC(self)
34 | dc.SetBrush(wx.Brush('#777'))
35 | dc.SetPen(wx.Pen("#777"))
36 |
37 | dc.DrawEllipse(20, 20, 90, 60)
38 | dc.DrawRoundedRectangle(130, 20, 90, 60, 10)
39 | dc.DrawArc(240, 40, 340, 40, 290, 20)
40 |
41 | dc.DrawPolygon(((130, 140), (180, 170), (180, 140), (220, 110), (140, 100)))
42 | dc.DrawRectangle(20, 120, 80, 50)
43 | dc.DrawSpline(((240, 170), (280, 170), (285, 110), (325, 110)))
44 |
45 | dc.DrawLines(((20, 260), (100, 260), (20, 210), (100, 210)))
46 | dc.DrawCircle(170, 230, 35)
47 | dc.DrawRectangle(250, 200, 60, 60)
48 |
49 |
50 |
51 | def main():
52 |
53 | app = wx.App()
54 | ex = Example(None)
55 | ex.Show()
56 | app.MainLoop()
57 |
58 |
59 | if __name__ == '__main__':
60 | main()
61 |
--------------------------------------------------------------------------------
/graphics/star.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program demonstrates a clipping operation
7 | when drawing a star object.
8 |
9 | author: Jan Bodnar
10 | website: zetcode.com
11 | last edited: May 2018
12 | """
13 |
14 | import wx
15 | from math import hypot, sin, cos, pi
16 |
17 | class Example(wx.Frame):
18 |
19 | def __init__(self, *args, **kw):
20 | super(Example, self).__init__(*args, **kw)
21 |
22 | self.InitUI()
23 |
24 | def InitUI(self):
25 |
26 | self.Bind(wx.EVT_PAINT, self.OnPaint)
27 |
28 | self.SetTitle("Star")
29 | self.Centre()
30 |
31 | def OnPaint(self, e):
32 |
33 | dc = wx.PaintDC(self)
34 |
35 | dc.SetPen(wx.Pen('#424242'))
36 | size_x, size_y = self.GetClientSize()
37 | dc.SetDeviceOrigin(size_x/2, size_y/2)
38 |
39 | points = (((0, 85), (75, 75), (100, 10), (125, 75), (200, 85),
40 | (150, 125), (160, 190), (100, 150), (40, 190), (50, 125)))
41 |
42 | region = wx.Region(points)
43 | dc.SetDeviceClippingRegion(region)
44 |
45 | radius = hypot(size_x/2, size_y/2)
46 | angle = 0
47 |
48 | while (angle < 2*pi):
49 |
50 | x = radius*cos(angle)
51 | y = radius*sin(angle)
52 | dc.DrawLine((0, 0), (x, y))
53 | angle = angle + 2*pi/360
54 |
55 | dc.DestroyClippingRegion()
56 |
57 |
58 | def main():
59 |
60 | app = wx.App()
61 | ex = Example(None)
62 | ex.Show()
63 | app.MainLoop()
64 |
65 |
66 | if __name__ == '__main__':
67 | main()
68 |
--------------------------------------------------------------------------------
/intro/centering.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | # centering.py
4 |
5 | import wx
6 |
7 | class Example(wx.Frame):
8 |
9 | def __init__(self, parent, title):
10 | super(Example, self).__init__(parent, title=title,
11 | size=(300, 200))
12 |
13 | self.Centre()
14 |
15 | def main():
16 |
17 | app = wx.App()
18 | ex = Example(None, title='Centering')
19 | ex.Show()
20 | app.MainLoop()
21 |
22 | if __name__ == '__main__':
23 | main()
24 |
--------------------------------------------------------------------------------
/intro/moving.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | # moving.py
4 |
5 | import wx
6 |
7 | class Example(wx.Frame):
8 |
9 | def __init__(self, parent, title):
10 | super(Example, self).__init__(parent, title=title,
11 | size=(300, 200))
12 |
13 | self.Move((800, 250))
14 |
15 | def main():
16 |
17 | app = wx.App()
18 | ex = Example(None, title='Moving')
19 | ex.Show()
20 | app.MainLoop()
21 |
22 | if __name__ == '__main__':
23 | main()
24 |
--------------------------------------------------------------------------------
/intro/no_minimize.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | # no_minimizebox.py
4 |
5 | import wx
6 |
7 | app = wx.App()
8 | frame = wx.Frame(None, style=wx.MAXIMIZE_BOX | wx.RESIZE_BORDER
9 | | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
10 | frame.Show(True)
11 |
12 | app.MainLoop()
13 |
--------------------------------------------------------------------------------
/intro/set_size.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | # set_size.py
4 |
5 | import wx
6 |
7 | class Example(wx.Frame):
8 |
9 | def __init__(self, parent, title):
10 | super(Example, self).__init__(parent, title=title,
11 | size=(350, 250))
12 |
13 | def main():
14 |
15 | app = wx.App()
16 | ex = Example(None, title='Sizing')
17 | ex.Show()
18 | app.MainLoop()
19 |
20 |
21 | if __name__ == '__main__':
22 | main()
23 |
--------------------------------------------------------------------------------
/intro/simple.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | # simple.py
4 |
5 | import wx
6 |
7 | app = wx.App()
8 |
9 | frame = wx.Frame(None, title='Simple application')
10 | frame.Show()
11 |
12 | app.MainLoop()
13 |
--------------------------------------------------------------------------------
/layout/absolute.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example, we lay out widgets using
7 | absolute positioning.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: April 2018
12 | """
13 |
14 | import wx
15 |
16 |
17 | class Example(wx.Frame):
18 |
19 | def __init__(self, parent, title):
20 | super(Example, self).__init__(parent, title=title,
21 | size=(350, 300))
22 |
23 | self.InitUI()
24 | self.Centre()
25 |
26 | def InitUI(self):
27 |
28 | self.panel = wx.Panel(self)
29 |
30 | self.panel.SetBackgroundColour("gray")
31 |
32 | self.LoadImages()
33 |
34 | self.mincol.SetPosition((20, 20))
35 | self.bardejov.SetPosition((40, 160))
36 | self.rotunda.SetPosition((170, 50))
37 |
38 |
39 | def LoadImages(self):
40 |
41 | self.mincol = wx.StaticBitmap(self.panel, wx.ID_ANY,
42 | wx.Bitmap("mincol.jpg", wx.BITMAP_TYPE_ANY))
43 |
44 | self.bardejov = wx.StaticBitmap(self.panel, wx.ID_ANY,
45 | wx.Bitmap("bardejov.jpg", wx.BITMAP_TYPE_ANY))
46 |
47 | self.rotunda = wx.StaticBitmap(self.panel, wx.ID_ANY,
48 | wx.Bitmap("rotunda.jpg", wx.BITMAP_TYPE_ANY))
49 |
50 |
51 | def main():
52 |
53 | app = wx.App()
54 | ex = Example(None, title='Absolute positioning')
55 | ex.Show()
56 | app.MainLoop()
57 |
58 |
59 | if __name__ == '__main__':
60 | main()
61 |
--------------------------------------------------------------------------------
/layout/bardejov.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/layout/bardejov.jpg
--------------------------------------------------------------------------------
/layout/border.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example we place a panel inside
7 | another panel.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: April 2018
12 | """
13 |
14 | import wx
15 |
16 |
17 | class Example(wx.Frame):
18 |
19 | def __init__(self, parent, title):
20 | super(Example, self).__init__(parent, title=title)
21 |
22 | self.InitUI()
23 | self.Centre()
24 |
25 | def InitUI(self):
26 |
27 | panel = wx.Panel(self)
28 |
29 | panel.SetBackgroundColour('#4f5049')
30 | vbox = wx.BoxSizer(wx.VERTICAL)
31 |
32 | midPan = wx.Panel(panel)
33 | midPan.SetBackgroundColour('#ededed')
34 |
35 | vbox.Add(midPan, wx.ID_ANY, wx.EXPAND | wx.ALL, 20)
36 | panel.SetSizer(vbox)
37 |
38 |
39 | def main():
40 |
41 | app = wx.App()
42 | ex = Example(None, title='Border')
43 | ex.Show()
44 | app.MainLoop()
45 |
46 |
47 | if __name__ == '__main__':
48 | main()
49 |
--------------------------------------------------------------------------------
/layout/calculator.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 |
4 | """
5 | ZetCode wxPython tutorial
6 |
7 | In this example we create a layout
8 | of a calculator with wx.GridSizer.
9 |
10 | author: Jan Bodnar
11 | website: www.zetcode.com
12 | last modified: April 2018
13 | """
14 |
15 | import wx
16 |
17 |
18 | class Example(wx.Frame):
19 |
20 | def __init__(self, parent, title):
21 | super(Example, self).__init__(parent, title=title)
22 |
23 | self.InitUI()
24 | self.Centre()
25 |
26 |
27 | def InitUI(self):
28 |
29 | menubar = wx.MenuBar()
30 | fileMenu = wx.Menu()
31 | menubar.Append(fileMenu, '&File')
32 | self.SetMenuBar(menubar)
33 |
34 | vbox = wx.BoxSizer(wx.VERTICAL)
35 | self.display = wx.TextCtrl(self, style=wx.TE_RIGHT)
36 | vbox.Add(self.display, flag=wx.EXPAND|wx.TOP|wx.BOTTOM, border=4)
37 | gs = wx.GridSizer(5, 4, 5, 5)
38 |
39 | gs.AddMany( [(wx.Button(self, label='Cls'), 0, wx.EXPAND),
40 | (wx.Button(self, label='Bck'), 0, wx.EXPAND),
41 | (wx.StaticText(self), wx.EXPAND),
42 | (wx.Button(self, label='Close'), 0, wx.EXPAND),
43 | (wx.Button(self, label='7'), 0, wx.EXPAND),
44 | (wx.Button(self, label='8'), 0, wx.EXPAND),
45 | (wx.Button(self, label='9'), 0, wx.EXPAND),
46 | (wx.Button(self, label='/'), 0, wx.EXPAND),
47 | (wx.Button(self, label='4'), 0, wx.EXPAND),
48 | (wx.Button(self, label='5'), 0, wx.EXPAND),
49 | (wx.Button(self, label='6'), 0, wx.EXPAND),
50 | (wx.Button(self, label='*'), 0, wx.EXPAND),
51 | (wx.Button(self, label='1'), 0, wx.EXPAND),
52 | (wx.Button(self, label='2'), 0, wx.EXPAND),
53 | (wx.Button(self, label='3'), 0, wx.EXPAND),
54 | (wx.Button(self, label='-'), 0, wx.EXPAND),
55 | (wx.Button(self, label='0'), 0, wx.EXPAND),
56 | (wx.Button(self, label='.'), 0, wx.EXPAND),
57 | (wx.Button(self, label='='), 0, wx.EXPAND),
58 | (wx.Button(self, label='+'), 0, wx.EXPAND) ])
59 |
60 | vbox.Add(gs, proportion=1, flag=wx.EXPAND)
61 | self.SetSizer(vbox)
62 |
63 |
64 | def main():
65 |
66 | app = wx.App()
67 | ex = Example(None, title='Calculator')
68 | ex.Show()
69 | app.MainLoop()
70 |
71 |
72 | if __name__ == '__main__':
73 | main()
74 |
--------------------------------------------------------------------------------
/layout/exec.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/layout/exec.png
--------------------------------------------------------------------------------
/layout/goto_class.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 |
4 | """
5 | ZetCode wxPython tutorial
6 |
7 | In this example we create a Go To class
8 | layout with wx.BoxSizer.
9 |
10 | author: Jan Bodnar
11 | website: www.zetcode.com
12 | last modified: April 2018
13 | """
14 |
15 | import wx
16 |
17 | class Example(wx.Frame):
18 |
19 | def __init__(self, parent, title):
20 | super(Example, self).__init__(parent, title=title)
21 |
22 | self.InitUI()
23 | self.Centre()
24 |
25 | def InitUI(self):
26 |
27 | panel = wx.Panel(self)
28 |
29 | font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
30 |
31 | font.SetPointSize(9)
32 |
33 | vbox = wx.BoxSizer(wx.VERTICAL)
34 |
35 | hbox1 = wx.BoxSizer(wx.HORIZONTAL)
36 | st1 = wx.StaticText(panel, label='Class Name')
37 | st1.SetFont(font)
38 | hbox1.Add(st1, flag=wx.RIGHT, border=8)
39 | tc = wx.TextCtrl(panel)
40 | hbox1.Add(tc, proportion=1)
41 | vbox.Add(hbox1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
42 |
43 | vbox.Add((-1, 10))
44 |
45 | hbox2 = wx.BoxSizer(wx.HORIZONTAL)
46 | st2 = wx.StaticText(panel, label='Matching Classes')
47 | st2.SetFont(font)
48 | hbox2.Add(st2)
49 | vbox.Add(hbox2, flag=wx.LEFT | wx.TOP, border=10)
50 |
51 | vbox.Add((-1, 10))
52 |
53 | hbox3 = wx.BoxSizer(wx.HORIZONTAL)
54 | tc2 = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
55 | hbox3.Add(tc2, proportion=1, flag=wx.EXPAND)
56 | vbox.Add(hbox3, proportion=1, flag=wx.LEFT|wx.RIGHT|wx.EXPAND,
57 | border=10)
58 |
59 | vbox.Add((-1, 25))
60 |
61 | hbox4 = wx.BoxSizer(wx.HORIZONTAL)
62 | cb1 = wx.CheckBox(panel, label='Case Sensitive')
63 | cb1.SetFont(font)
64 | hbox4.Add(cb1)
65 | cb2 = wx.CheckBox(panel, label='Nested Classes')
66 | cb2.SetFont(font)
67 | hbox4.Add(cb2, flag=wx.LEFT, border=10)
68 | cb3 = wx.CheckBox(panel, label='Non-Project classes')
69 | cb3.SetFont(font)
70 | hbox4.Add(cb3, flag=wx.LEFT, border=10)
71 | vbox.Add(hbox4, flag=wx.LEFT, border=10)
72 |
73 | vbox.Add((-1, 25))
74 |
75 | hbox5 = wx.BoxSizer(wx.HORIZONTAL)
76 | btn1 = wx.Button(panel, label='Ok', size=(70, 30))
77 | hbox5.Add(btn1)
78 | btn2 = wx.Button(panel, label='Close', size=(70, 30))
79 | hbox5.Add(btn2, flag=wx.LEFT|wx.BOTTOM, border=5)
80 | vbox.Add(hbox5, flag=wx.ALIGN_RIGHT|wx.RIGHT, border=10)
81 |
82 | panel.SetSizer(vbox)
83 |
84 |
85 | def main():
86 |
87 | app = wx.App()
88 | ex = Example(None, title='Go To Class')
89 | ex.Show()
90 | app.MainLoop()
91 |
92 |
93 | if __name__ == '__main__':
94 | main()
95 |
--------------------------------------------------------------------------------
/layout/mincol.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/layout/mincol.jpg
--------------------------------------------------------------------------------
/layout/newclass.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example we create a new class layout
7 | with wx.GridBagSizer.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: April 2018
12 | """
13 |
14 | import wx
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, parent, title):
19 | super(Example, self).__init__(parent, title=title)
20 |
21 | self.InitUI()
22 | self.Centre()
23 |
24 | def InitUI(self):
25 |
26 | panel = wx.Panel(self)
27 |
28 | sizer = wx.GridBagSizer(5, 5)
29 |
30 | text1 = wx.StaticText(panel, label="Java Class")
31 | sizer.Add(text1, pos=(0, 0), flag=wx.TOP|wx.LEFT|wx.BOTTOM,
32 | border=15)
33 |
34 | icon = wx.StaticBitmap(panel, bitmap=wx.Bitmap('exec.png'))
35 | sizer.Add(icon, pos=(0, 4), flag=wx.TOP|wx.RIGHT|wx.ALIGN_RIGHT,
36 | border=5)
37 |
38 | line = wx.StaticLine(panel)
39 | sizer.Add(line, pos=(1, 0), span=(1, 5),
40 | flag=wx.EXPAND|wx.BOTTOM, border=10)
41 |
42 | text2 = wx.StaticText(panel, label="Name")
43 | sizer.Add(text2, pos=(2, 0), flag=wx.LEFT, border=10)
44 |
45 | tc1 = wx.TextCtrl(panel)
46 | sizer.Add(tc1, pos=(2, 1), span=(1, 3), flag=wx.TOP|wx.EXPAND)
47 |
48 | text3 = wx.StaticText(panel, label="Package")
49 | sizer.Add(text3, pos=(3, 0), flag=wx.LEFT|wx.TOP, border=10)
50 |
51 | tc2 = wx.TextCtrl(panel)
52 | sizer.Add(tc2, pos=(3, 1), span=(1, 3), flag=wx.TOP|wx.EXPAND,
53 | border=5)
54 |
55 | button1 = wx.Button(panel, label="Browse...")
56 | sizer.Add(button1, pos=(3, 4), flag=wx.TOP|wx.RIGHT, border=5)
57 |
58 | text4 = wx.StaticText(panel, label="Extends")
59 | sizer.Add(text4, pos=(4, 0), flag=wx.TOP|wx.LEFT, border=10)
60 |
61 | combo = wx.ComboBox(panel)
62 | sizer.Add(combo, pos=(4, 1), span=(1, 3),
63 | flag=wx.TOP|wx.EXPAND, border=5)
64 |
65 | button2 = wx.Button(panel, label="Browse...")
66 | sizer.Add(button2, pos=(4, 4), flag=wx.TOP|wx.RIGHT, border=5)
67 |
68 | sb = wx.StaticBox(panel, label="Optional Attributes")
69 |
70 | boxsizer = wx.StaticBoxSizer(sb, wx.VERTICAL)
71 | boxsizer.Add(wx.CheckBox(panel, label="Public"),
72 | flag=wx.LEFT|wx.TOP, border=5)
73 | boxsizer.Add(wx.CheckBox(panel, label="Generate Default Constructor"),
74 | flag=wx.LEFT, border=5)
75 | boxsizer.Add(wx.CheckBox(panel, label="Generate Main Method"),
76 | flag=wx.LEFT|wx.BOTTOM, border=5)
77 | sizer.Add(boxsizer, pos=(5, 0), span=(1, 5),
78 | flag=wx.EXPAND|wx.TOP|wx.LEFT|wx.RIGHT , border=10)
79 |
80 | button3 = wx.Button(panel, label='Help')
81 | sizer.Add(button3, pos=(7, 0), flag=wx.LEFT, border=10)
82 |
83 | button4 = wx.Button(panel, label="Ok")
84 | sizer.Add(button4, pos=(7, 3))
85 |
86 | button5 = wx.Button(panel, label="Cancel")
87 | sizer.Add(button5, pos=(7, 4), span=(1, 1),
88 | flag=wx.BOTTOM|wx.RIGHT, border=10)
89 |
90 | sizer.AddGrowableCol(2)
91 |
92 | panel.SetSizer(sizer)
93 | sizer.Fit(self)
94 | # panel.GetSizer().Fit(self)
95 |
96 |
97 | def main():
98 |
99 | app = wx.App()
100 | ex = Example(None, title="Create Java Class")
101 | ex.Show()
102 | app.MainLoop()
103 |
104 |
105 | if __name__ == '__main__':
106 | main()
107 |
--------------------------------------------------------------------------------
/layout/rename.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example we create a rename layout
7 | with wx.GridBagSizer.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: April 2018
12 | """
13 |
14 | import wx
15 |
16 |
17 | class Example(wx.Frame):
18 |
19 | def __init__(self, parent, title):
20 | super(Example, self).__init__(parent, title=title)
21 |
22 | self.InitUI()
23 | self.Centre()
24 |
25 | def InitUI(self):
26 |
27 | panel = wx.Panel(self)
28 | sizer = wx.GridBagSizer(4, 4)
29 |
30 | text = wx.StaticText(panel, label="Rename To")
31 | sizer.Add(text, pos=(0, 0), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
32 |
33 | tc = wx.TextCtrl(panel)
34 | sizer.Add(tc, pos=(1, 0), span=(1, 5),
35 | flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
36 |
37 | buttonOk = wx.Button(panel, label="Ok", size=(90, 28))
38 | buttonClose = wx.Button(panel, label="Close", size=(90, 28))
39 | sizer.Add(buttonOk, pos=(3, 3))
40 | sizer.Add(buttonClose, pos=(3, 4), flag=wx.RIGHT|wx.BOTTOM, border=10)
41 |
42 | sizer.AddGrowableCol(1)
43 | sizer.AddGrowableRow(2)
44 | panel.SetSizer(sizer)
45 |
46 |
47 | def main():
48 |
49 | app = wx.App()
50 | ex = Example(None, title='Rename')
51 | ex.Show()
52 | app.MainLoop()
53 |
54 |
55 | if __name__ == '__main__':
56 | main()
57 |
--------------------------------------------------------------------------------
/layout/review.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example we create review
7 | layout with wx.FlexGridSizer.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: April 2018
12 | """
13 |
14 | import wx
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, parent, title):
19 | super(Example, self).__init__(parent, title=title)
20 |
21 | self.InitUI()
22 | self.Centre()
23 | self.Show()
24 |
25 | def InitUI(self):
26 |
27 | panel = wx.Panel(self)
28 |
29 | hbox = wx.BoxSizer(wx.HORIZONTAL)
30 |
31 | fgs = wx.FlexGridSizer(3, 2, 9, 25)
32 |
33 | title = wx.StaticText(panel, label="Title")
34 | author = wx.StaticText(panel, label="Author")
35 | review = wx.StaticText(panel, label="Review")
36 |
37 | tc1 = wx.TextCtrl(panel)
38 | tc2 = wx.TextCtrl(panel)
39 | tc3 = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
40 |
41 | fgs.AddMany([(title), (tc1, 1, wx.EXPAND), (author),
42 | (tc2, 1, wx.EXPAND), (review, 1, wx.EXPAND), (tc3, 1, wx.EXPAND)])
43 |
44 | fgs.AddGrowableRow(2, 1)
45 | fgs.AddGrowableCol(1, 1)
46 |
47 | hbox.Add(fgs, proportion=1, flag=wx.ALL|wx.EXPAND, border=15)
48 | panel.SetSizer(hbox)
49 |
50 |
51 | def main():
52 |
53 | app = wx.App()
54 | ex = Example(None, title='Review')
55 | ex.Show()
56 | app.MainLoop()
57 |
58 |
59 | if __name__ == '__main__':
60 | main()
61 |
--------------------------------------------------------------------------------
/layout/rotunda.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/layout/rotunda.jpg
--------------------------------------------------------------------------------
/menus/checkmenu_item.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This example creates a checked
7 | menu item.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: April 2018
12 | """
13 |
14 | import wx
15 |
16 |
17 | class Example(wx.Frame):
18 |
19 | def __init__(self, *args, **kwargs):
20 | super(Example, self).__init__(*args, **kwargs)
21 |
22 | self.InitUI()
23 |
24 | def InitUI(self):
25 |
26 | menubar = wx.MenuBar()
27 | viewMenu = wx.Menu()
28 |
29 | self.shst = viewMenu.Append(wx.ID_ANY, 'Show statusbar',
30 | 'Show Statusbar', kind=wx.ITEM_CHECK)
31 | self.shtl = viewMenu.Append(wx.ID_ANY, 'Show toolbar',
32 | 'Show Toolbar', kind=wx.ITEM_CHECK)
33 |
34 | viewMenu.Check(self.shst.GetId(), True)
35 | viewMenu.Check(self.shtl.GetId(), True)
36 |
37 | self.Bind(wx.EVT_MENU, self.ToggleStatusBar, self.shst)
38 | self.Bind(wx.EVT_MENU, self.ToggleToolBar, self.shtl)
39 |
40 | menubar.Append(viewMenu, '&View')
41 | self.SetMenuBar(menubar)
42 |
43 | self.toolbar = self.CreateToolBar()
44 | self.toolbar.AddTool(1, '', wx.Bitmap('texit.png'))
45 | self.toolbar.Realize()
46 |
47 | self.statusbar = self.CreateStatusBar()
48 | self.statusbar.SetStatusText('Ready')
49 |
50 | self.SetSize((450, 350))
51 | self.SetTitle('Check menu item')
52 | self.Centre()
53 |
54 |
55 | def ToggleStatusBar(self, e):
56 |
57 | if self.shst.IsChecked():
58 | self.statusbar.Show()
59 | else:
60 | self.statusbar.Hide()
61 |
62 | def ToggleToolBar(self, e):
63 |
64 | if self.shtl.IsChecked():
65 | self.toolbar.Show()
66 | else:
67 | self.toolbar.Hide()
68 |
69 |
70 | def main():
71 |
72 | app = wx.App()
73 | ex = Example(None)
74 | ex.Show()
75 | app.MainLoop()
76 |
77 |
78 | if __name__ == '__main__':
79 | main()
80 |
--------------------------------------------------------------------------------
/menus/context_menu.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example, we create a context menu.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: April 2018
11 | """
12 |
13 | import wx
14 |
15 | class MyPopupMenu(wx.Menu):
16 |
17 | def __init__(self, parent):
18 | super(MyPopupMenu, self).__init__()
19 |
20 | self.parent = parent
21 |
22 | mmi = wx.MenuItem(self, wx.NewId(), 'Minimize')
23 | self.Append(mmi)
24 | self.Bind(wx.EVT_MENU, self.OnMinimize, mmi)
25 |
26 | cmi = wx.MenuItem(self, wx.NewId(), 'Close')
27 | self.Append(cmi)
28 | self.Bind(wx.EVT_MENU, self.OnClose, cmi)
29 |
30 |
31 | def OnMinimize(self, e):
32 | self.parent.Iconize()
33 |
34 | def OnClose(self, e):
35 | self.parent.Close()
36 |
37 |
38 | class Example(wx.Frame):
39 |
40 | def __init__(self, *args, **kwargs):
41 | super(Example, self).__init__(*args, **kwargs)
42 |
43 | self.InitUI()
44 |
45 | def InitUI(self):
46 |
47 | self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
48 |
49 | self.SetSize((350, 250))
50 | self.SetTitle('Context menu')
51 | self.Centre()
52 |
53 | def OnRightDown(self, e):
54 | self.PopupMenu(MyPopupMenu(self), e.GetPosition())
55 |
56 |
57 | def main():
58 |
59 | app = wx.App()
60 | ex = Example(None)
61 | ex.Show()
62 | app.MainLoop()
63 |
64 |
65 | if __name__ == '__main__':
66 | main()
67 |
--------------------------------------------------------------------------------
/menus/exit.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/menus/exit.png
--------------------------------------------------------------------------------
/menus/icons_shortcuts.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example, we manually create
7 | a menu item.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: April 2018
12 | """
13 |
14 | import wx
15 |
16 | APP_EXIT = 1
17 |
18 |
19 | class Example(wx.Frame):
20 |
21 | def __init__(self, *args, **kwargs):
22 | super(Example, self).__init__(*args, **kwargs)
23 |
24 | self.InitUI()
25 |
26 | def InitUI(self):
27 |
28 | menubar = wx.MenuBar()
29 | fileMenu = wx.Menu()
30 | qmi = wx.MenuItem(fileMenu, APP_EXIT, '&Quit\tCtrl+Q')
31 | qmi.SetBitmap(wx.Bitmap('exit.png'))
32 | fileMenu.Append(qmi)
33 |
34 | self.Bind(wx.EVT_MENU, self.OnQuit, id=APP_EXIT)
35 |
36 | menubar.Append(fileMenu, '&File')
37 | self.SetMenuBar(menubar)
38 |
39 | self.SetSize((350, 250))
40 | self.SetTitle('Icons and shortcuts')
41 | self.Centre()
42 |
43 | def OnQuit(self, e):
44 | self.Close()
45 |
46 |
47 | def main():
48 |
49 | app = wx.App()
50 | ex = Example(None)
51 | ex.Show()
52 | app.MainLoop()
53 |
54 |
55 | if __name__ == '__main__':
56 | main()
57 |
--------------------------------------------------------------------------------
/menus/simple_menu.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This example shows a simple menu.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: April 2018
11 | """
12 |
13 | import wx
14 |
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kwargs):
19 | super(Example, self).__init__(*args, **kwargs)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | menubar = wx.MenuBar()
26 | fileMenu = wx.Menu()
27 | fileItem = fileMenu.Append(wx.ID_EXIT, 'Quit', 'Quit application')
28 | menubar.Append(fileMenu, '&File')
29 | self.SetMenuBar(menubar)
30 |
31 | self.Bind(wx.EVT_MENU, self.OnQuit, fileItem)
32 |
33 | self.SetSize((300, 200))
34 | self.SetTitle('Simple menu')
35 | self.Centre()
36 |
37 | def OnQuit(self, e):
38 | self.Close()
39 |
40 |
41 | def main():
42 |
43 | app = wx.App()
44 | ex = Example(None)
45 | ex.Show()
46 | app.MainLoop()
47 |
48 |
49 | if __name__ == '__main__':
50 | main()
51 |
--------------------------------------------------------------------------------
/menus/submenus.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example, we create a submenu and a menu
7 | separator.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: April 2018
12 | """
13 |
14 | import wx
15 |
16 |
17 | class Example(wx.Frame):
18 |
19 | def __init__(self, *args, **kwargs):
20 | super(Example, self).__init__(*args, **kwargs)
21 |
22 | self.InitUI()
23 |
24 | def InitUI(self):
25 |
26 | menubar = wx.MenuBar()
27 |
28 | fileMenu = wx.Menu()
29 | fileMenu.Append(wx.ID_NEW, '&New')
30 | fileMenu.Append(wx.ID_OPEN, '&Open')
31 | fileMenu.Append(wx.ID_SAVE, '&Save')
32 | fileMenu.AppendSeparator()
33 |
34 | imp = wx.Menu()
35 | imp.Append(wx.ID_ANY, 'Import newsfeed list...')
36 | imp.Append(wx.ID_ANY, 'Import bookmarks...')
37 | imp.Append(wx.ID_ANY, 'Import mail...')
38 |
39 | fileMenu.AppendMenu(wx.ID_ANY, 'I&mport', imp)
40 |
41 | qmi = wx.MenuItem(fileMenu, wx.ID_EXIT, '&Quit\tCtrl+W')
42 | fileMenu.AppendItem(qmi)
43 |
44 | self.Bind(wx.EVT_MENU, self.OnQuit, qmi)
45 |
46 | menubar.Append(fileMenu, '&File')
47 | self.SetMenuBar(menubar)
48 |
49 | self.SetSize((350, 250))
50 | self.SetTitle('Submenu')
51 | self.Centre()
52 |
53 | def OnQuit(self, e):
54 | self.Close()
55 |
56 |
57 | def main():
58 |
59 | app = wx.App()
60 | ex = Example(None)
61 | ex.Show()
62 | app.MainLoop()
63 |
64 |
65 | if __name__ == '__main__':
66 | main()
67 |
--------------------------------------------------------------------------------
/menus/texit.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/menus/texit.png
--------------------------------------------------------------------------------
/menus/tnew.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/menus/tnew.png
--------------------------------------------------------------------------------
/menus/toolbar.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This example creates a simple toolbar.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: April 2018
11 | """
12 |
13 | import wx
14 |
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kwargs):
19 | super(Example, self).__init__(*args, **kwargs)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | toolbar = self.CreateToolBar()
26 | qtool = toolbar.AddTool(wx.ID_ANY, 'Quit', wx.Bitmap('texit.png'))
27 | toolbar.Realize()
28 |
29 | self.Bind(wx.EVT_TOOL, self.OnQuit, qtool)
30 |
31 | self.SetSize((350, 250))
32 | self.SetTitle('Simple toolbar')
33 | self.Centre()
34 |
35 | def OnQuit(self, e):
36 | self.Close()
37 |
38 |
39 | def main():
40 |
41 | app = wx.App()
42 | ex = Example(None)
43 | ex.Show()
44 | app.MainLoop()
45 |
46 |
47 | if __name__ == '__main__':
48 | main()
49 |
--------------------------------------------------------------------------------
/menus/toolbars.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | '''
4 | ZetCode wxPython tutorial
5 |
6 | In this example, we create two horizontal
7 | toolbars.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: April 2018
12 | '''
13 |
14 | import wx
15 |
16 |
17 | class Example(wx.Frame):
18 |
19 | def __init__(self, *args, **kwargs):
20 | super(Example, self).__init__(*args, **kwargs)
21 |
22 | self.InitUI()
23 |
24 | def InitUI(self):
25 |
26 | vbox = wx.BoxSizer(wx.VERTICAL)
27 |
28 | toolbar1 = wx.ToolBar(self)
29 | toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('tnew.png'))
30 | toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('topen.png'))
31 | toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('tsave.png'))
32 | toolbar1.Realize()
33 |
34 | toolbar2 = wx.ToolBar(self)
35 | qtool = toolbar2.AddTool(wx.ID_EXIT, '', wx.Bitmap('texit.png'))
36 | toolbar2.Realize()
37 |
38 | vbox.Add(toolbar1, 0, wx.EXPAND)
39 | vbox.Add(toolbar2, 0, wx.EXPAND)
40 |
41 | self.Bind(wx.EVT_TOOL, self.OnQuit, qtool)
42 |
43 | self.SetSizer(vbox)
44 |
45 | self.SetSize((350, 250))
46 | self.SetTitle('Toolbars')
47 | self.Centre()
48 |
49 | def OnQuit(self, e):
50 | self.Close()
51 |
52 |
53 | def main():
54 |
55 | app = wx.App()
56 | ex = Example(None)
57 | ex.Show()
58 | app.MainLoop()
59 |
60 |
61 | if __name__ == '__main__':
62 | main()
63 |
--------------------------------------------------------------------------------
/menus/topen.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/menus/topen.png
--------------------------------------------------------------------------------
/menus/tredo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/menus/tredo.png
--------------------------------------------------------------------------------
/menus/tsave.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/menus/tsave.png
--------------------------------------------------------------------------------
/menus/tundo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/menus/tundo.png
--------------------------------------------------------------------------------
/menus/undo_redo.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example, we create two horizontal
7 | toolbars.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: April 2018
12 | """
13 |
14 | import wx
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kwargs):
19 | super(Example, self).__init__(*args, **kwargs)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | self.count = 5
26 |
27 | self.toolbar = self.CreateToolBar()
28 | tundo = self.toolbar.AddTool(wx.ID_UNDO, '', wx.Bitmap('tundo.png'))
29 | tredo = self.toolbar.AddTool(wx.ID_REDO, '', wx.Bitmap('tredo.png'))
30 | self.toolbar.EnableTool(wx.ID_REDO, False)
31 | self.toolbar.AddSeparator()
32 | texit = self.toolbar.AddTool(wx.ID_EXIT, '', wx.Bitmap('texit.png'))
33 | self.toolbar.Realize()
34 |
35 | self.Bind(wx.EVT_TOOL, self.OnQuit, texit)
36 | self.Bind(wx.EVT_TOOL, self.OnUndo, tundo)
37 | self.Bind(wx.EVT_TOOL, self.OnRedo, tredo)
38 |
39 | self.SetSize((350, 250))
40 | self.SetTitle('Undo redo')
41 | self.Centre()
42 |
43 | def OnUndo(self, e):
44 | if self.count > 1 and self.count <= 5:
45 | self.count = self.count - 1
46 |
47 | if self.count == 1:
48 | self.toolbar.EnableTool(wx.ID_UNDO, False)
49 |
50 | if self.count == 4:
51 | self.toolbar.EnableTool(wx.ID_REDO, True)
52 |
53 | def OnRedo(self, e):
54 | if self.count < 5 and self.count >= 1:
55 | self.count = self.count + 1
56 |
57 | if self.count == 5:
58 | self.toolbar.EnableTool(wx.ID_REDO, False)
59 |
60 | if self.count == 2:
61 | self.toolbar.EnableTool(wx.ID_UNDO, True)
62 |
63 |
64 | def OnQuit(self, e):
65 | self.Close()
66 |
67 |
68 | def main():
69 |
70 | app = wx.App()
71 | ex = Example(None)
72 | ex.Show()
73 | app.MainLoop()
74 |
75 |
76 | if __name__ == '__main__':
77 | main()
78 |
--------------------------------------------------------------------------------
/skeletons/browser/browser.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program creates a browser UI.
7 |
8 | author: Jan Bodnar
9 | website: zetcode.com
10 | last edited: May 2018
11 | """
12 |
13 | import wx
14 | from wx.lib.buttons import GenBitmapTextButton
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | self.CreateMenuBar()
26 |
27 | panel = wx.Panel(self)
28 | # panel.SetBackgroundColour('white')
29 |
30 | vbox = wx.BoxSizer(wx.VERTICAL)
31 | hbox1 = wx.BoxSizer(wx.HORIZONTAL)
32 | hbox2 = wx.BoxSizer(wx.HORIZONTAL)
33 |
34 | line1 = wx.StaticLine(panel)
35 | vbox.Add(line1, 0, wx.EXPAND)
36 |
37 | toolbar1 = wx.Panel(panel, size=(-1, 30))
38 |
39 | back = wx.BitmapButton(toolbar1, bitmap=wx.Bitmap('images/back.png'),
40 | style=wx.NO_BORDER)
41 | forward = wx.BitmapButton(toolbar1, bitmap=wx.Bitmap('images/forw.png'),
42 | style=wx.NO_BORDER)
43 | refresh = wx.BitmapButton(toolbar1, bitmap=wx.Bitmap('images/refresh.png'),
44 | style=wx.NO_BORDER)
45 | stop = wx.BitmapButton(toolbar1, bitmap=wx.Bitmap('images/stop.png'),
46 | style=wx.NO_BORDER)
47 | home = wx.BitmapButton(toolbar1, bitmap=wx.Bitmap('images/home.png'),
48 | style=wx.NO_BORDER)
49 | address = wx.ComboBox(toolbar1, size=(50, -1))
50 | go = wx.BitmapButton(toolbar1, bitmap=wx.Bitmap('images/play.png'),
51 | style=wx.NO_BORDER)
52 | text = wx.TextCtrl(toolbar1, size=(150, -1))
53 |
54 | hbox1.Add(back)
55 | hbox1.Add(forward)
56 | hbox1.Add(refresh)
57 | hbox1.Add(stop)
58 | hbox1.Add(home)
59 | hbox1.Add(address, 1, wx.TOP, 3)
60 | hbox1.Add(go, 0, wx.TOP | wx.LEFT, 3)
61 | hbox1.Add(text, 0, wx.TOP | wx.RIGHT, 3)
62 |
63 | toolbar1.SetSizer(hbox1)
64 | vbox.Add(toolbar1, 0, wx.EXPAND)
65 | line = wx.StaticLine(panel)
66 | vbox.Add(line, 0, wx.EXPAND)
67 |
68 | toolbar2 = wx.Panel(panel, size=(-1, 30))
69 | bookmark1 = wx.BitmapButton(toolbar2, bitmap=wx.Bitmap('images/love.png'),
70 | style=wx.NO_BORDER)
71 | bookmark2 = wx.BitmapButton(toolbar2, bitmap=wx.Bitmap('images/book.png'),
72 | style=wx.NO_BORDER)
73 | bookmark3 = wx.BitmapButton(toolbar2, bitmap=wx.Bitmap('images/sound.png'),
74 | style=wx.NO_BORDER)
75 |
76 | hbox2.Add(bookmark1, flag=wx.RIGHT, border=5)
77 | hbox2.Add(bookmark2, flag=wx.RIGHT, border=5)
78 | hbox2.Add(bookmark3)
79 |
80 | toolbar2.SetSizer(hbox2)
81 | vbox.Add(toolbar2, 0, wx.EXPAND)
82 |
83 | line2 = wx.StaticLine(panel)
84 | vbox.Add(line2, 0, wx.EXPAND)
85 |
86 | panel.SetSizer(vbox)
87 |
88 | self.CreateStatusBar()
89 |
90 | self.SetTitle("Browser")
91 | self.Centre()
92 |
93 | def CreateMenuBar(self):
94 |
95 | menubar = wx.MenuBar()
96 | file = wx.Menu()
97 | file.Append(wx.ID_ANY, '&Quit', '')
98 | edit = wx.Menu()
99 | view = wx.Menu()
100 | go = wx.Menu()
101 | bookmarks = wx.Menu()
102 | tools = wx.Menu()
103 | help = wx.Menu()
104 |
105 | menubar.Append(file, '&File')
106 | menubar.Append(edit, '&Edit')
107 | menubar.Append(view, '&View')
108 | menubar.Append(go, '&Go')
109 | menubar.Append(bookmarks, '&Bookmarks')
110 | menubar.Append(tools, '&Tools')
111 | menubar.Append(help, '&Help')
112 |
113 | self.SetMenuBar(menubar)
114 |
115 |
116 | def main():
117 |
118 | app = wx.App()
119 | ex = Example(None)
120 | ex.Show()
121 | app.MainLoop()
122 |
123 |
124 | if __name__ == '__main__':
125 | main()
126 |
--------------------------------------------------------------------------------
/skeletons/browser/images/back.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/browser/images/back.png
--------------------------------------------------------------------------------
/skeletons/browser/images/book.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/browser/images/book.png
--------------------------------------------------------------------------------
/skeletons/browser/images/forw.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/browser/images/forw.png
--------------------------------------------------------------------------------
/skeletons/browser/images/home.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/browser/images/home.png
--------------------------------------------------------------------------------
/skeletons/browser/images/love.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/browser/images/love.png
--------------------------------------------------------------------------------
/skeletons/browser/images/play.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/browser/images/play.png
--------------------------------------------------------------------------------
/skeletons/browser/images/refresh.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/browser/images/refresh.png
--------------------------------------------------------------------------------
/skeletons/browser/images/sound.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/browser/images/sound.png
--------------------------------------------------------------------------------
/skeletons/browser/images/stop.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/browser/images/stop.png
--------------------------------------------------------------------------------
/skeletons/filemanager/file_manager.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program creates a skeleton
7 | of a file manager UI.
8 |
9 | author: Jan Bodnar
10 | website: zetcode.com
11 | last edited: May 2018
12 | """
13 |
14 | import wx
15 | import os
16 | import time
17 |
18 | ID_BUTTON=100
19 | ID_EXIT=200
20 | ID_SPLITTER=300
21 |
22 |
23 | class MyListCtrl(wx.ListCtrl):
24 |
25 | def __init__(self, parent):
26 | wx.ListCtrl.__init__(self, parent, style=wx.LC_REPORT)
27 |
28 | images = ['images/empty.png', 'images/folder.png', 'images/source-py.png',
29 | 'images/image.png', 'images/pdf.png', 'images/up16.png']
30 |
31 | self.InsertColumn(0, 'Name')
32 | self.InsertColumn(1, 'Ext')
33 | self.InsertColumn(2, 'Size', wx.LIST_FORMAT_RIGHT)
34 | self.InsertColumn(3, 'Modified')
35 |
36 | self.SetColumnWidth(0, 220)
37 | self.SetColumnWidth(1, 70)
38 | self.SetColumnWidth(2, 100)
39 | self.SetColumnWidth(3, 420)
40 |
41 | self.il = wx.ImageList(16, 16)
42 |
43 | for i in images:
44 |
45 | self.il.Add(wx.Bitmap(i))
46 |
47 | self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
48 |
49 | j = 1
50 |
51 | self.InsertItem(0, '..')
52 | self.SetItemImage(0, 5)
53 |
54 | files = os.listdir('.')
55 |
56 | for i in files:
57 |
58 | (name, ext) = os.path.splitext(i)
59 | ex = ext[1:]
60 | size = os.path.getsize(i)
61 | sec = os.path.getmtime(i)
62 |
63 | self.InsertItem(j, name)
64 | self.SetItem(j, 1, ex)
65 | self.SetItem(j, 2, str(size) + ' B')
66 | self.SetItem(j, 3, time.strftime('%Y-%m-%d %H:%M', time.localtime(sec)))
67 |
68 | if os.path.isdir(i):
69 | self.SetItemImage(j, 1)
70 | elif ex == 'py':
71 | self.SetItemImage(j, 2)
72 | elif ex == 'jpg':
73 | self.SetItemImage(j, 3)
74 | elif ex == 'pdf':
75 | self.SetItemImage(j, 4)
76 | else:
77 | self.SetItemImage(j, 0)
78 |
79 | if (j % 2) == 0:
80 |
81 | self.SetItemBackgroundColour(j, '#e6f1f5')
82 |
83 | j = j + 1
84 |
85 |
86 | class Example(wx.Frame):
87 |
88 | def __init__(self, *args, **kw):
89 | super(Example, self).__init__(*args, **kw)
90 |
91 | self.InitUI()
92 |
93 | def InitUI(self):
94 |
95 | self.splitter = wx.SplitterWindow(self, ID_SPLITTER, style=wx.SP_BORDER)
96 | self.splitter.SetMinimumPaneSize(50)
97 |
98 | p1 = MyListCtrl(self.splitter)
99 | p2 = MyListCtrl(self.splitter)
100 | self.splitter.SplitVertically(p1, p2)
101 |
102 | self.Bind(wx.EVT_SIZE, self.OnSize)
103 | self.Bind(wx.EVT_SPLITTER_DCLICK, self.OnDoubleClick, id=ID_SPLITTER)
104 |
105 | filemenu= wx.Menu()
106 | filemenu.Append(ID_EXIT, "E&xit"," Terminate the program")
107 | editmenu = wx.Menu()
108 | netmenu = wx.Menu()
109 | showmenu = wx.Menu()
110 | configmenu = wx.Menu()
111 | helpmenu = wx.Menu()
112 |
113 | menuBar = wx.MenuBar()
114 | menuBar.Append(filemenu, "&File")
115 | menuBar.Append(editmenu, "&Edit")
116 | menuBar.Append(netmenu, "&Net")
117 | menuBar.Append(showmenu, "&Show")
118 | menuBar.Append(configmenu, "&Config")
119 | menuBar.Append(helpmenu, "&Help")
120 | self.SetMenuBar(menuBar)
121 |
122 | self.Bind(wx.EVT_MENU, self.OnExit, id=ID_EXIT)
123 |
124 | tb = self.CreateToolBar( wx.TB_HORIZONTAL | wx.NO_BORDER |
125 | wx.TB_FLAT)
126 |
127 | tb.AddTool(10, 'Previous', wx.Bitmap('images/previous.png'), shortHelp='Previous')
128 | tb.AddTool(20, 'Up', wx.Bitmap('images/up.png'), shortHelp='Up one directory')
129 | tb.AddTool(30, 'Home', wx.Bitmap('images/home.png'), shortHelp='Home')
130 | tb.AddTool(40, 'Refresh', wx.Bitmap('images/refresh.png'), shortHelp='Refresh')
131 | tb.AddSeparator()
132 | tb.AddTool(50, 'Edit text', wx.Bitmap('images/textedit.png'), shortHelp='Edit text')
133 | tb.AddTool(60, 'Terminal', wx.Bitmap('images/terminal.png'), shortHelp='Terminal')
134 | tb.AddSeparator()
135 | tb.AddTool(70, 'Help', wx.Bitmap('images/help.png'), shortHelp='Show help')
136 | tb.Realize()
137 |
138 | self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
139 |
140 | button1 = wx.Button(self, ID_BUTTON + 1, "F3 View")
141 | button2 = wx.Button(self, ID_BUTTON + 2, "F4 Edit")
142 | button3 = wx.Button(self, ID_BUTTON + 3, "F5 Copy")
143 | button4 = wx.Button(self, ID_BUTTON + 4, "F6 Move")
144 | button5 = wx.Button(self, ID_BUTTON + 5, "F7 Mkdir")
145 | button6 = wx.Button(self, ID_BUTTON + 6, "F8 Delete")
146 | button7 = wx.Button(self, ID_BUTTON + 7, "F9 Rename")
147 | button8 = wx.Button(self, ID_EXIT, "F10 Quit")
148 |
149 | self.sizer2.Add(button1, 1, wx.EXPAND)
150 | self.sizer2.Add(button2, 1, wx.EXPAND)
151 | self.sizer2.Add(button3, 1, wx.EXPAND)
152 | self.sizer2.Add(button4, 1, wx.EXPAND)
153 | self.sizer2.Add(button5, 1, wx.EXPAND)
154 | self.sizer2.Add(button6, 1, wx.EXPAND)
155 | self.sizer2.Add(button7, 1, wx.EXPAND)
156 | self.sizer2.Add(button8, 1, wx.EXPAND)
157 |
158 | self.Bind(wx.EVT_BUTTON, self.OnExit, id=ID_EXIT)
159 |
160 | self.sizer = wx.BoxSizer(wx.VERTICAL)
161 | self.sizer.Add(self.splitter,1,wx.EXPAND)
162 | self.sizer.Add(self.sizer2,0,wx.EXPAND)
163 | self.SetSizer(self.sizer)
164 |
165 | # size = wx.DisplaySize()
166 | # self.SetSize(size)
167 |
168 | sb = self.CreateStatusBar()
169 | sb.SetStatusText(os.getcwd())
170 |
171 | self.SetTitle("File Hunter")
172 | self.Center()
173 |
174 |
175 | def OnExit(self, e):
176 |
177 | self.Close(True)
178 |
179 | def OnSize(self, e):
180 |
181 | size = self.GetSize()
182 | self.splitter.SetSashPosition(size.x / 2)
183 |
184 | e.Skip()
185 |
186 |
187 | def OnDoubleClick(self, e):
188 |
189 | size = self.GetSize()
190 | self.splitter.SetSashPosition(size.x / 2)
191 |
192 |
193 | def main():
194 |
195 | app = wx.App()
196 | ex = Example(None)
197 | ex.Show()
198 | app.MainLoop()
199 |
200 |
201 | if __name__ == '__main__':
202 | main()
203 |
--------------------------------------------------------------------------------
/skeletons/filemanager/images/empty.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/filemanager/images/empty.png
--------------------------------------------------------------------------------
/skeletons/filemanager/images/folder.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/filemanager/images/folder.png
--------------------------------------------------------------------------------
/skeletons/filemanager/images/help.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/filemanager/images/help.png
--------------------------------------------------------------------------------
/skeletons/filemanager/images/home.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/filemanager/images/home.png
--------------------------------------------------------------------------------
/skeletons/filemanager/images/image.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/filemanager/images/image.png
--------------------------------------------------------------------------------
/skeletons/filemanager/images/pdf.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/filemanager/images/pdf.png
--------------------------------------------------------------------------------
/skeletons/filemanager/images/previous.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/filemanager/images/previous.png
--------------------------------------------------------------------------------
/skeletons/filemanager/images/refresh.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/filemanager/images/refresh.png
--------------------------------------------------------------------------------
/skeletons/filemanager/images/source-py.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/filemanager/images/source-py.png
--------------------------------------------------------------------------------
/skeletons/filemanager/images/terminal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/filemanager/images/terminal.png
--------------------------------------------------------------------------------
/skeletons/filemanager/images/textedit.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/filemanager/images/textedit.png
--------------------------------------------------------------------------------
/skeletons/filemanager/images/up.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/filemanager/images/up.png
--------------------------------------------------------------------------------
/skeletons/filemanager/images/up16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/filemanager/images/up16.png
--------------------------------------------------------------------------------
/skeletons/player/images/back.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/player/images/back.png
--------------------------------------------------------------------------------
/skeletons/player/images/forw.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/player/images/forw.png
--------------------------------------------------------------------------------
/skeletons/player/images/pause.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/player/images/pause.png
--------------------------------------------------------------------------------
/skeletons/player/images/play.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/player/images/play.png
--------------------------------------------------------------------------------
/skeletons/player/images/volume.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/player/images/volume.png
--------------------------------------------------------------------------------
/skeletons/player/player.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program creates a Player UI.
7 |
8 | author: Jan Bodnar
9 | website: zetcode.com
10 | last edited: May 2018
11 | """
12 |
13 | import wx
14 |
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | self.CreateMenuBar()
26 |
27 | panel = wx.Panel(self)
28 |
29 | pnl1 = wx.Panel(self)
30 | pnl1.SetBackgroundColour(wx.BLACK)
31 | pnl2 = wx.Panel(self)
32 |
33 | slider1 = wx.Slider(pnl2, value=18, minValue=0, maxValue=1000)
34 | pause = wx.BitmapButton(pnl2, bitmap=wx.Bitmap('images/pause.png'))
35 | play = wx.BitmapButton(pnl2, bitmap=wx.Bitmap('images/play.png'))
36 | forw = wx.BitmapButton(pnl2, bitmap=wx.Bitmap('images/forw.png'))
37 | back = wx.BitmapButton(pnl2, bitmap=wx.Bitmap('images/back.png'))
38 | vol = wx.BitmapButton(pnl2, bitmap=wx.Bitmap('images/volume.png'))
39 | slider2 = wx.Slider(pnl2, value=1, minValue=0, maxValue=100,
40 | size=(120, -1))
41 |
42 | vbox = wx.BoxSizer(wx.VERTICAL)
43 | hbox1 = wx.BoxSizer(wx.HORIZONTAL)
44 | hbox2 = wx.BoxSizer(wx.HORIZONTAL)
45 |
46 | hbox1.Add(slider1, proportion=1)
47 | hbox2.Add(pause)
48 | hbox2.Add(play, flag=wx.RIGHT, border=5)
49 | hbox2.Add(forw, flag=wx.LEFT, border=5)
50 | hbox2.Add(back)
51 | hbox2.Add((-1, -1), proportion=1)
52 | hbox2.Add(vol)
53 | hbox2.Add(slider2, flag=wx.TOP|wx.LEFT, border=5)
54 |
55 | vbox.Add(hbox1, flag=wx.EXPAND|wx.BOTTOM, border=10)
56 | vbox.Add(hbox2, proportion=1, flag=wx.EXPAND)
57 | pnl2.SetSizer(vbox)
58 |
59 | sizer = wx.BoxSizer(wx.VERTICAL)
60 | sizer.Add(pnl1, proportion=1, flag=wx.EXPAND)
61 | sizer.Add(pnl2, flag=wx.EXPAND|wx.BOTTOM|wx.TOP, border=10)
62 |
63 | self.SetMinSize((350, 300))
64 | self.CreateStatusBar()
65 | self.SetSizer(sizer)
66 |
67 | self.SetSize((350, 200))
68 | self.SetTitle('Player')
69 | self.Centre()
70 |
71 | def CreateMenuBar(self):
72 |
73 | menubar = wx.MenuBar()
74 | filem = wx.Menu()
75 | play = wx.Menu()
76 | view = wx.Menu()
77 | tools = wx.Menu()
78 | favorites = wx.Menu()
79 | help = wx.Menu()
80 |
81 | filem.Append(wx.ID_ANY, '&quit', 'Quit application')
82 |
83 | menubar.Append(filem, '&File')
84 | menubar.Append(play, '&Play')
85 | menubar.Append(view, '&View')
86 | menubar.Append(tools, '&Tools')
87 | menubar.Append(favorites, 'F&avorites')
88 | menubar.Append(help, '&Help')
89 |
90 | self.SetMenuBar(menubar)
91 |
92 |
93 | def main():
94 |
95 | app = wx.App()
96 | ex = Example(None)
97 | ex.Show()
98 | app.MainLoop()
99 |
100 |
101 | if __name__ == '__main__':
102 | main()
103 |
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/align-center.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/align-center.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/align-left.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/align-left.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/align-right.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/align-right.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/asc.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/asc.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/chart.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/chart.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/copy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/copy.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/cut.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/cut.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/delete.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/delete.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/desc.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/desc.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/exit.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/exit.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/new.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/new.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/open.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/open.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/paste.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/paste.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/redo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/redo.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/save.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/save.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/text-bold.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/text-bold.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/text-italic.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/text-italic.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/text-underline.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/text-underline.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/images/undo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/janbodnar/wxPython-examples/e07ad5389d26b34e4c308b930080355ca1d890ff/skeletons/spreadsheet/images/undo.png
--------------------------------------------------------------------------------
/skeletons/spreadsheet/spreadsheet.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This program creates a SpreadSheet UI.
7 |
8 | author: Jan Bodnar
9 | website: zetcode.com
10 | last edited: May 2018
11 | """
12 |
13 | from wx.lib import sheet
14 | import wx
15 |
16 |
17 | class MySheet(wx.grid.Grid):
18 |
19 | def __init__(self, *args, **kw):
20 | super(MySheet, self).__init__(*args, **kw)
21 |
22 | self.InitUI()
23 |
24 | def InitUI(self):
25 |
26 | nOfRows = 55
27 | nOfCols = 25
28 |
29 | self.row = self.col = 0
30 | self.CreateGrid(nOfRows, nOfCols)
31 |
32 | self.SetColLabelSize(20)
33 | self.SetRowLabelSize(50)
34 |
35 | self.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.OnGridSelectCell)
36 |
37 | for i in range(nOfRows):
38 | self.SetRowSize(i, 20)
39 |
40 | for i in range(nOfCols):
41 | self.SetColSize(i, 75)
42 |
43 | def OnGridSelectCell(self, e):
44 |
45 | self.row, self.col = e.GetRow(), e.GetCol()
46 |
47 | control = self.GetParent().GetParent().position
48 | value = self.GetColLabelValue(self.col) + self.GetRowLabelValue(self.row)
49 | control.SetValue(value)
50 |
51 | e.Skip()
52 |
53 |
54 | class Example(wx.Frame):
55 |
56 | def __init__(self, *args, **kw):
57 | super(Example, self).__init__(*args, **kw)
58 |
59 | self.InitUI()
60 |
61 | def InitUI(self):
62 |
63 | fonts = ['Times New Roman', 'Times', 'Courier', 'Courier New', 'Helvetica',
64 | 'Sans', 'verdana', 'utkal', 'aakar', 'Arial']
65 | font_sizes = ['10', '11', '12', '14', '16']
66 |
67 | box = wx.BoxSizer(wx.VERTICAL)
68 | menuBar = wx.MenuBar()
69 |
70 | menu1 = wx.Menu()
71 | menuBar.Append(menu1, '&File')
72 | menu2 = wx.Menu()
73 | menuBar.Append(menu2, '&Edit')
74 | menu3 = wx.Menu()
75 | menuBar.Append(menu3, '&Edit')
76 | menu4 = wx.Menu()
77 | menuBar.Append(menu4, '&Insert')
78 | menu5 = wx.Menu()
79 | menuBar.Append(menu5, 'F&ormat')
80 | menu6 = wx.Menu()
81 | menuBar.Append(menu6, '&Tools')
82 | menu7 = wx.Menu()
83 | menuBar.Append(menu7, '&Data')
84 | menu8 = wx.Menu()
85 | menuBar.Append(menu8, '&Help')
86 |
87 | self.SetMenuBar(menuBar)
88 |
89 | toolbar1 = wx.ToolBar(self, style= wx.TB_HORIZONTAL)
90 |
91 | toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('images/new.png'))
92 | toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('images/open.png'))
93 | toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('images/save.png'))
94 |
95 | toolbar1.AddSeparator()
96 |
97 | toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('images/cut.png'))
98 | toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('images/copy.png'))
99 | toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('images/paste.png'))
100 | toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('images/delete.png'))
101 |
102 | toolbar1.AddSeparator()
103 |
104 | toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('images/undo.png'))
105 | toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('images/redo.png'))
106 |
107 | toolbar1.AddSeparator()
108 |
109 | toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('images/asc.png'))
110 | toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('images/desc.png'))
111 |
112 | toolbar1.AddSeparator()
113 | toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('images/chart.png'))
114 |
115 | toolbar1.AddSeparator()
116 | toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('images/exit.png'))
117 |
118 | toolbar1.Realize()
119 |
120 | toolbar2 = wx.ToolBar(self, wx.TB_HORIZONTAL | wx.TB_TEXT)
121 |
122 | self.position = wx.TextCtrl(toolbar2)
123 |
124 | font = wx.ComboBox(toolbar2, value='Times', choices=fonts, size=(100, -1),
125 | style=wx.CB_DROPDOWN)
126 |
127 | font_height = wx.ComboBox(toolbar2, value='10', choices=font_sizes,
128 | size=(50, -1), style=wx.CB_DROPDOWN)
129 |
130 | toolbar2.AddControl(self.position)
131 | toolbar2.AddControl(font)
132 | toolbar2.AddControl(font_height)
133 |
134 | toolbar2.AddSeparator()
135 |
136 | toolbar2.AddCheckTool(wx.ID_ANY, '', wx.Bitmap('images/text-bold.png'))
137 | toolbar2.AddCheckTool(wx.ID_ANY, '', wx.Bitmap('images/text-italic.png'))
138 | toolbar2.AddCheckTool(wx.ID_ANY, '', wx.Bitmap('images/text-underline.png'))
139 |
140 | toolbar2.AddSeparator()
141 |
142 | toolbar2.AddTool(wx.ID_ANY, '', wx.Bitmap('images/align-left.png'))
143 | toolbar2.AddTool(wx.ID_ANY, '', wx.Bitmap('images/align-center.png'))
144 | toolbar2.AddTool(wx.ID_ANY, '', wx.Bitmap('images/align-right.png'))
145 |
146 | box.Add(toolbar1, border=5)
147 | box.Add((5,5) , 0)
148 | box.Add(toolbar2)
149 | box.Add((5,10) , 0)
150 |
151 | toolbar2.Realize()
152 | self.SetSizer(box)
153 |
154 | notebook = wx.Notebook(self, style=wx.RIGHT)
155 |
156 | sheet1 = MySheet(notebook)
157 | sheet2 = MySheet(notebook)
158 | sheet3 = MySheet(notebook)
159 | sheet1.SetFocus()
160 |
161 | notebook.AddPage(sheet1, 'Sheet1')
162 | notebook.AddPage(sheet2, 'Sheet2')
163 | notebook.AddPage(sheet3, 'Sheet3')
164 |
165 | box.Add(notebook, 1, wx.EXPAND)
166 |
167 | self.CreateStatusBar()
168 |
169 | self.SetSize((550, 550))
170 | self.SetTitle("SpreadSheet")
171 | self.Centre()
172 |
173 | def main():
174 |
175 | app = wx.App()
176 | ex = Example(None)
177 | ex.Show()
178 | app.MainLoop()
179 |
180 |
181 | if __name__ == '__main__':
182 | main()
183 |
--------------------------------------------------------------------------------
/tetris/tetris.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | This is Tetris game clone in wxPython.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: April 2018
11 | """
12 |
13 | import wx
14 | import random
15 |
16 | class Tetris(wx.Frame):
17 |
18 | def __init__(self, parent):
19 | wx.Frame.__init__(self, parent, size=(180, 380),
20 | style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER ^ wx.MAXIMIZE_BOX)
21 |
22 | self.initFrame()
23 |
24 | def initFrame(self):
25 |
26 | self.statusbar = self.CreateStatusBar()
27 | self.statusbar.SetStatusText('0')
28 | self.board = Board(self)
29 | self.board.SetFocus()
30 | self.board.start()
31 |
32 | self.SetTitle("Tetris")
33 | self.Centre()
34 |
35 |
36 | class Board(wx.Panel):
37 |
38 | BoardWidth = 10
39 | BoardHeight = 22
40 | Speed = 300
41 | ID_TIMER = 1
42 |
43 | def __init__(self, *args, **kw):
44 | # wx.Panel.__init__(self, parent)
45 | super(Board, self).__init__(*args, **kw)
46 |
47 | self.initBoard()
48 |
49 | def initBoard(self):
50 |
51 | self.timer = wx.Timer(self, Board.ID_TIMER)
52 | self.isWaitingAfterLine = False
53 | self.curPiece = Shape()
54 | self.nextPiece = Shape()
55 | self.curX = 0
56 | self.curY = 0
57 | self.numLinesRemoved = 0
58 | self.board = []
59 |
60 | self.isStarted = False
61 | self.isPaused = False
62 |
63 | self.Bind(wx.EVT_PAINT, self.OnPaint)
64 | self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
65 | self.Bind(wx.EVT_TIMER, self.OnTimer, id=Board.ID_TIMER)
66 |
67 | self.clearBoard()
68 |
69 | def shapeAt(self, x, y):
70 |
71 | return self.board[(y * Board.BoardWidth) + x]
72 |
73 | def setShapeAt(self, x, y, shape):
74 |
75 | self.board[(y * Board.BoardWidth) + x] = shape
76 |
77 | def squareWidth(self):
78 |
79 | return self.GetClientSize().GetWidth() // Board.BoardWidth
80 |
81 | def squareHeight(self):
82 |
83 | return self.GetClientSize().GetHeight() // Board.BoardHeight
84 |
85 | def start(self):
86 |
87 | if self.isPaused:
88 | return
89 |
90 | self.isStarted = True
91 | self.isWaitingAfterLine = False
92 | self.numLinesRemoved = 0
93 | self.clearBoard()
94 |
95 | self.newPiece()
96 | self.timer.Start(Board.Speed)
97 |
98 | def pause(self):
99 |
100 | if not self.isStarted:
101 | return
102 |
103 | self.isPaused = not self.isPaused
104 | statusbar = self.GetParent().statusbar
105 |
106 | if self.isPaused:
107 | self.timer.Stop()
108 | statusbar.SetStatusText('paused')
109 | else:
110 | self.timer.Start(Board.Speed)
111 | statusbar.SetStatusText(str(self.numLinesRemoved))
112 |
113 | self.Refresh()
114 |
115 | def clearBoard(self):
116 |
117 | for i in range(Board.BoardHeight * Board.BoardWidth):
118 | self.board.append(Tetrominoes.NoShape)
119 |
120 | def OnPaint(self, event):
121 |
122 | dc = wx.PaintDC(self)
123 |
124 | size = self.GetClientSize()
125 | boardTop = size.GetHeight() - Board.BoardHeight * self.squareHeight()
126 |
127 | for i in range(Board.BoardHeight):
128 | for j in range(Board.BoardWidth):
129 |
130 | shape = self.shapeAt(j, Board.BoardHeight - i - 1)
131 |
132 | if shape != Tetrominoes.NoShape:
133 | self.drawSquare(dc,
134 | 0 + j * self.squareWidth(),
135 | boardTop + i * self.squareHeight(), shape)
136 |
137 | if self.curPiece.shape() != Tetrominoes.NoShape:
138 |
139 | for i in range(4):
140 |
141 | x = self.curX + self.curPiece.x(i)
142 | y = self.curY - self.curPiece.y(i)
143 |
144 | self.drawSquare(dc, 0 + x * self.squareWidth(),
145 | boardTop + (Board.BoardHeight - y - 1) * self.squareHeight(),
146 | self.curPiece.shape())
147 |
148 |
149 | def OnKeyDown(self, event):
150 |
151 | if not self.isStarted or self.curPiece.shape() == Tetrominoes.NoShape:
152 | event.Skip()
153 | return
154 |
155 | keycode = event.GetKeyCode()
156 |
157 | if keycode == ord('P') or keycode == ord('p'):
158 | self.pause()
159 | return
160 |
161 | if self.isPaused:
162 | return
163 |
164 | elif keycode == wx.WXK_LEFT:
165 | self.tryMove(self.curPiece, self.curX - 1, self.curY)
166 |
167 | elif keycode == wx.WXK_RIGHT:
168 | self.tryMove(self.curPiece, self.curX + 1, self.curY)
169 |
170 | elif keycode == wx.WXK_DOWN:
171 | self.tryMove(self.curPiece.rotatedRight(), self.curX, self.curY)
172 |
173 | elif keycode == wx.WXK_UP:
174 | self.tryMove(self.curPiece.rotatedLeft(), self.curX, self.curY)
175 |
176 | elif keycode == wx.WXK_SPACE:
177 | self.dropDown()
178 |
179 | elif keycode == ord('D') or keycode == ord('d'):
180 | self.oneLineDown()
181 |
182 | else:
183 | event.Skip()
184 |
185 |
186 | def OnTimer(self, event):
187 |
188 | if event.GetId() == Board.ID_TIMER:
189 |
190 | if self.isWaitingAfterLine:
191 | self.isWaitingAfterLine = False
192 | self.newPiece()
193 |
194 | else:
195 | self.oneLineDown()
196 |
197 | else:
198 | event.Skip()
199 |
200 |
201 | def dropDown(self):
202 |
203 | newY = self.curY
204 |
205 | while newY > 0:
206 | if not self.tryMove(self.curPiece, self.curX, newY - 1):
207 | break
208 | newY -= 1
209 |
210 | self.pieceDropped()
211 |
212 | def oneLineDown(self):
213 |
214 | if not self.tryMove(self.curPiece, self.curX, self.curY - 1):
215 | self.pieceDropped()
216 |
217 |
218 | def pieceDropped(self):
219 |
220 | for i in range(4):
221 |
222 | x = self.curX + self.curPiece.x(i)
223 | y = self.curY - self.curPiece.y(i)
224 | self.setShapeAt(x, y, self.curPiece.shape())
225 |
226 | self.removeFullLines()
227 |
228 | if not self.isWaitingAfterLine:
229 | self.newPiece()
230 |
231 |
232 | def removeFullLines(self):
233 |
234 | numFullLines = 0
235 |
236 | statusbar = self.GetParent().statusbar
237 |
238 | rowsToRemove = []
239 |
240 | for i in range(Board.BoardHeight):
241 | n = 0
242 | for j in range(Board.BoardWidth):
243 | if not self.shapeAt(j, i) == Tetrominoes.NoShape:
244 | n = n + 1
245 |
246 | if n == 10:
247 | rowsToRemove.append(i)
248 |
249 | rowsToRemove.reverse()
250 |
251 | for m in rowsToRemove:
252 | for k in range(m, Board.BoardHeight):
253 | for l in range(Board.BoardWidth):
254 | self.setShapeAt(l, k, self.shapeAt(l, k + 1))
255 |
256 | numFullLines = numFullLines + len(rowsToRemove)
257 |
258 | if numFullLines > 0:
259 |
260 | self.numLinesRemoved = self.numLinesRemoved + numFullLines
261 | statusbar.SetStatusText(str(self.numLinesRemoved))
262 | self.isWaitingAfterLine = True
263 | self.curPiece.setShape(Tetrominoes.NoShape)
264 | self.Refresh()
265 |
266 |
267 | def newPiece(self):
268 |
269 | self.curPiece = self.nextPiece
270 | statusbar = self.GetParent().statusbar
271 | self.nextPiece.setRandomShape()
272 |
273 | self.curX = Board.BoardWidth // 2 + 1
274 | self.curY = Board.BoardHeight - 1 + self.curPiece.minY()
275 |
276 | if not self.tryMove(self.curPiece, self.curX, self.curY):
277 |
278 | self.curPiece.setShape(Tetrominoes.NoShape)
279 | self.timer.Stop()
280 | self.isStarted = False
281 | statusbar.SetStatusText('Game over')
282 |
283 |
284 | def tryMove(self, newPiece, newX, newY):
285 |
286 | for i in range(4):
287 |
288 | x = newX + newPiece.x(i)
289 | y = newY - newPiece.y(i)
290 |
291 | if x < 0 or x >= Board.BoardWidth or y < 0 or y >= Board.BoardHeight:
292 | return False
293 |
294 | if self.shapeAt(x, y) != Tetrominoes.NoShape:
295 | return False
296 |
297 | self.curPiece = newPiece
298 | self.curX = newX
299 | self.curY = newY
300 | self.Refresh()
301 |
302 | return True
303 |
304 |
305 | def drawSquare(self, dc, x, y, shape):
306 |
307 | colors = ['#000000', '#CC6666', '#66CC66', '#6666CC',
308 | '#CCCC66', '#CC66CC', '#66CCCC', '#DAAA00']
309 |
310 | light = ['#000000', '#F89FAB', '#79FC79', '#7979FC',
311 | '#FCFC79', '#FC79FC', '#79FCFC', '#FCC600']
312 |
313 | dark = ['#000000', '#803C3B', '#3B803B', '#3B3B80',
314 | '#80803B', '#803B80', '#3B8080', '#806200']
315 |
316 | pen = wx.Pen(light[shape])
317 | pen.SetCap(wx.CAP_PROJECTING)
318 | dc.SetPen(pen)
319 |
320 | dc.DrawLine(x, y + self.squareHeight() - 1, x, y)
321 | dc.DrawLine(x, y, x + self.squareWidth() - 1, y)
322 |
323 | darkpen = wx.Pen(dark[shape])
324 | darkpen.SetCap(wx.CAP_PROJECTING)
325 | dc.SetPen(darkpen)
326 |
327 | dc.DrawLine(x + 1, y + self.squareHeight() - 1,
328 | x + self.squareWidth() - 1, y + self.squareHeight() - 1)
329 | dc.DrawLine(x + self.squareWidth() - 1,
330 | y + self.squareHeight() - 1, x + self.squareWidth() - 1, y + 1)
331 |
332 | dc.SetPen(wx.TRANSPARENT_PEN)
333 | dc.SetBrush(wx.Brush(colors[shape]))
334 | dc.DrawRectangle(x + 1, y + 1, self.squareWidth() - 2,
335 | self.squareHeight() - 2)
336 |
337 |
338 | class Tetrominoes(object):
339 |
340 | NoShape = 0
341 | ZShape = 1
342 | SShape = 2
343 | LineShape = 3
344 | TShape = 4
345 | SquareShape = 5
346 | LShape = 6
347 | MirroredLShape = 7
348 |
349 |
350 | class Shape(object):
351 |
352 | coordsTable = (
353 | ((0, 0), (0, 0), (0, 0), (0, 0)),
354 | ((0, -1), (0, 0), (-1, 0), (-1, 1)),
355 | ((0, -1), (0, 0), (1, 0), (1, 1)),
356 | ((0, -1), (0, 0), (0, 1), (0, 2)),
357 | ((-1, 0), (0, 0), (1, 0), (0, 1)),
358 | ((0, 0), (1, 0), (0, 1), (1, 1)),
359 | ((-1, -1), (0, -1), (0, 0), (0, 1)),
360 | ((1, -1), (0, -1), (0, 0), (0, 1))
361 | )
362 |
363 | def __init__(self):
364 |
365 | self.coords = [[0,0] for i in range(4)]
366 | self.pieceShape = Tetrominoes.NoShape
367 |
368 | self.setShape(Tetrominoes.NoShape)
369 |
370 | def shape(self):
371 |
372 | return self.pieceShape
373 |
374 | def setShape(self, shape):
375 |
376 | table = Shape.coordsTable[shape]
377 | for i in range(4):
378 | for j in range(2):
379 | self.coords[i][j] = table[i][j]
380 |
381 | self.pieceShape = shape
382 |
383 | def setRandomShape(self):
384 |
385 | self.setShape(random.randint(1, 7))
386 |
387 | def x(self, index):
388 |
389 | return self.coords[index][0]
390 |
391 | def y(self, index):
392 |
393 | return self.coords[index][1]
394 |
395 | def setX(self, index, x):
396 |
397 | self.coords[index][0] = x
398 |
399 | def setY(self, index, y):
400 |
401 | self.coords[index][1] = y
402 |
403 | def minX(self):
404 |
405 | m = self.coords[0][0]
406 | for i in range(4):
407 | m = min(m, self.coords[i][0])
408 |
409 | return m
410 |
411 | def maxX(self):
412 |
413 | m = self.coords[0][0]
414 | for i in range(4):
415 | m = max(m, self.coords[i][0])
416 |
417 | return m
418 |
419 | def minY(self):
420 |
421 | m = self.coords[0][1]
422 | for i in range(4):
423 | m = min(m, self.coords[i][1])
424 |
425 | return m
426 |
427 | def maxY(self):
428 |
429 | m = self.coords[0][1]
430 |
431 | for i in range(4):
432 | m = max(m, self.coords[i][1])
433 |
434 | return m
435 |
436 | def rotatedLeft(self):
437 |
438 | if self.pieceShape == Tetrominoes.SquareShape:
439 | return self
440 |
441 | result = Shape()
442 | result.pieceShape = self.pieceShape
443 |
444 | for i in range(4):
445 | result.setX(i, self.y(i))
446 | result.setY(i, -self.x(i))
447 |
448 | return result
449 |
450 | def rotatedRight(self):
451 |
452 | if self.pieceShape == Tetrominoes.SquareShape:
453 | return self
454 |
455 | result = Shape()
456 | result.pieceShape = self.pieceShape
457 |
458 | for i in range(4):
459 | result.setX(i, -self.y(i))
460 | result.setY(i, self.x(i))
461 |
462 | return result
463 |
464 |
465 | def main():
466 |
467 | app = wx.App()
468 | ex = Tetris(None)
469 | ex.Show()
470 | app.MainLoop()
471 |
472 |
473 | if __name__ == '__main__':
474 | main()
475 |
--------------------------------------------------------------------------------
/waiting.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | '''
4 | ZetCode Advanced wxPython tutorial
5 |
6 | This program creates a
7 | waiting effect in cairo.
8 |
9 | author: Jan Bodnar
10 | website: zetcode.com
11 | last edited: May 2018
12 | '''
13 |
14 | import wx
15 | import wx.lib.wxcairo
16 | import cairo
17 | import math
18 |
19 |
20 | class cv(object):
21 |
22 | trs = (
23 | ( 0.0, 0.15, 0.30, 0.5, 0.65, 0.80, 0.9, 1.0 ),
24 | ( 1.0, 0.0, 0.15, 0.30, 0.5, 0.65, 0.8, 0.9 ),
25 | ( 0.9, 1.0, 0.0, 0.15, 0.3, 0.5, 0.65, 0.8 ),
26 | ( 0.8, 0.9, 1.0, 0.0, 0.15, 0.3, 0.5, 0.65 ),
27 | ( 0.65, 0.8, 0.9, 1.0, 0.0, 0.15, 0.3, 0.5 ),
28 | ( 0.5, 0.65, 0.8, 0.9, 1.0, 0.0, 0.15, 0.3 ),
29 | ( 0.3, 0.5, 0.65, 0.8, 0.9, 1.0, 0.0, 0.15 ),
30 | ( 0.15, 0.3, 0.5, 0.65, 0.8, 0.9, 1.0, 0.0, )
31 | )
32 |
33 | TIMER_ID = 1
34 | SPEED = 100
35 | CLIMIT = 1000
36 | NLINES = 8
37 |
38 |
39 | class Board(wx.Panel):
40 |
41 | def __init__(self, *args, **kw):
42 | super(Board, self).__init__(*args, **kw)
43 |
44 | self.SetDoubleBuffered(True)
45 | self.Bind(wx.EVT_PAINT, self.OnPaint)
46 |
47 | self.InitBoard()
48 |
49 |
50 | def InitBoard(self):
51 |
52 | self.count = 0
53 | self.timer = wx.Timer(self, cv.TIMER_ID)
54 | self.timer.Start(cv.SPEED)
55 |
56 | self.Bind(wx.EVT_PAINT, self.OnPaint)
57 | self.Bind(wx.EVT_TIMER, self.OnTimer,
58 | id=cv.TIMER_ID)
59 |
60 | self.Centre()
61 | self.Show()
62 |
63 |
64 | def OnTimer(self, e):
65 |
66 | self.count = self.count + 1
67 |
68 | if self.count >= cv.CLIMIT:
69 | self.count = 0
70 |
71 | self.Refresh()
72 |
73 |
74 | def OnPaint(self, e):
75 |
76 | dc = wx.PaintDC(self)
77 | cr = wx.lib.wxcairo.ContextFromDC(dc)
78 |
79 | self.DoDrawing(cr)
80 |
81 |
82 | def DoDrawing(self, cr):
83 |
84 | w, h = self.GetClientSize()
85 |
86 | cr.set_line_width(3)
87 | cr.set_line_cap(cairo.LINE_CAP_ROUND)
88 |
89 | cr.translate(w/2, h/2)
90 |
91 | for i in range(cv.NLINES):
92 |
93 | cr.set_source_rgba(0, 0, 0,
94 | cv.trs[self.count % cv.NLINES][i])
95 | cr.move_to(0.0, -10.0)
96 | cr.line_to(0.0, -40.0)
97 | cr.rotate(math.pi/4)
98 | cr.stroke()
99 |
100 |
101 | class Example(wx.Frame):
102 |
103 | def __init__(self, *args, **kw):
104 | super(Example, self).__init__(*args, **kw)
105 |
106 | self.InitUI()
107 | self.SetSize((250, 150))
108 | self.SetTitle("Waiting")
109 | self.Centre()
110 | self.Show()
111 |
112 |
113 | def InitUI(self):
114 |
115 | Board(self)
116 |
117 |
118 | def main():
119 |
120 | app = wx.App()
121 | Example(None)
122 | app.MainLoop()
123 |
124 |
125 | if __name__ == '__main__':
126 | main()
127 |
128 |
--------------------------------------------------------------------------------
/widgets/button_wid.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this code example, we create a
7 | button widget.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: April 2018
12 | """
13 |
14 | import wx
15 |
16 |
17 | class Example(wx.Frame):
18 |
19 | def __init__(self, *args, **kw):
20 | super(Example, self).__init__(*args, **kw)
21 |
22 | self.InitUI()
23 |
24 | def InitUI(self):
25 |
26 | pnl = wx.Panel(self)
27 | closeButton = wx.Button(pnl, label='Close', pos=(20, 20))
28 |
29 | closeButton.Bind(wx.EVT_BUTTON, self.OnClose)
30 |
31 | self.SetSize((350, 250))
32 | self.SetTitle('wx.Button')
33 | self.Centre()
34 |
35 | def OnClose(self, e):
36 |
37 | self.Close(True)
38 |
39 |
40 | def main():
41 |
42 | app = wx.App()
43 | ex = Example(None)
44 | ex.Show()
45 | app.MainLoop()
46 |
47 |
48 | if __name__ == '__main__':
49 | main()
50 |
--------------------------------------------------------------------------------
/widgets/checkbox.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example we create a checkbox widget.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: April 2018
11 | """
12 |
13 | import wx
14 |
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | pnl = wx.Panel(self)
26 |
27 | vbox = wx.BoxSizer(wx.HORIZONTAL)
28 |
29 | cb = wx.CheckBox(pnl, label='Show title')
30 | cb.SetValue(True)
31 | cb.Bind(wx.EVT_CHECKBOX, self.ShowOrHideTitle)
32 |
33 | vbox.Add(cb, flag=wx.TOP|wx.LEFT, border=30)
34 |
35 | pnl.SetSizer(vbox)
36 |
37 | self.SetTitle('wx.CheckBox')
38 | self.Centre()
39 |
40 | def ShowOrHideTitle(self, e):
41 |
42 | sender = e.GetEventObject()
43 | isChecked = sender.GetValue()
44 |
45 | if isChecked:
46 | self.SetTitle('wx.CheckBox')
47 | else:
48 | self.SetTitle('')
49 |
50 |
51 | def main():
52 |
53 | app = wx.App()
54 | ex = Example(None)
55 | ex.Show()
56 | app.MainLoop()
57 |
58 |
59 | if __name__ == '__main__':
60 | main()
61 |
--------------------------------------------------------------------------------
/widgets/gauge_wid.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example we create a gauge widget.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: April 2018
11 | """
12 |
13 | import wx
14 |
15 | TASK_RANGE = 50
16 |
17 | class Example(wx.Frame):
18 |
19 | def __init__(self, *args, **kw):
20 | super(Example, self).__init__(*args, **kw)
21 |
22 | self.InitUI()
23 |
24 | def InitUI(self):
25 |
26 | self.timer = wx.Timer(self, 1)
27 | self.count = 0
28 |
29 | self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
30 |
31 | pnl = wx.Panel(self)
32 | vbox = wx.BoxSizer(wx.VERTICAL)
33 | hbox1 = wx.BoxSizer(wx.HORIZONTAL)
34 | hbox2 = wx.BoxSizer(wx.HORIZONTAL)
35 | hbox3 = wx.BoxSizer(wx.HORIZONTAL)
36 |
37 | self.gauge = wx.Gauge(pnl, range=TASK_RANGE, size=(250, -1))
38 | self.btn1 = wx.Button(pnl, wx.ID_OK)
39 | self.btn2 = wx.Button(pnl, wx.ID_STOP)
40 | self.text = wx.StaticText(pnl, label='Task to be done')
41 |
42 | self.Bind(wx.EVT_BUTTON, self.OnOk, self.btn1)
43 | self.Bind(wx.EVT_BUTTON, self.OnStop, self.btn2)
44 |
45 | hbox1.Add(self.gauge, proportion=1, flag=wx.ALIGN_CENTRE)
46 | hbox2.Add(self.btn1, proportion=1, flag=wx.RIGHT, border=10)
47 | hbox2.Add(self.btn2, proportion=1)
48 | hbox3.Add(self.text, proportion=1)
49 |
50 | vbox.Add((0, 30))
51 |
52 | vbox.Add(hbox1, flag=wx.ALIGN_CENTRE)
53 |
54 | vbox.Add((0, 20))
55 |
56 | vbox.Add(hbox2, proportion=1, flag=wx.ALIGN_CENTRE)
57 | vbox.Add(hbox3, proportion=1, flag=wx.ALIGN_CENTRE)
58 |
59 | pnl.SetSizer(vbox)
60 |
61 | self.SetTitle('wx.Gauge')
62 | self.Centre()
63 |
64 | def OnOk(self, e):
65 |
66 | if self.count >= TASK_RANGE:
67 | return
68 |
69 | self.timer.Start(100)
70 | self.text.SetLabel('Task in Progress')
71 |
72 | def OnStop(self, e):
73 |
74 | if self.count == 0 or self.count >= TASK_RANGE or not self.timer.IsRunning():
75 | return
76 |
77 | self.timer.Stop()
78 | self.text.SetLabel('Task Interrupted')
79 |
80 | def OnTimer(self, e):
81 |
82 | self.count = self.count + 1
83 | self.gauge.SetValue(self.count)
84 |
85 | if self.count == TASK_RANGE:
86 |
87 | self.timer.Stop()
88 | self.text.SetLabel('Task Completed')
89 |
90 |
91 | def main():
92 |
93 | app = wx.App()
94 | ex = Example(None)
95 | ex.Show()
96 | app.MainLoop()
97 |
98 |
99 | if __name__ == '__main__':
100 | main()
101 |
--------------------------------------------------------------------------------
/widgets/slider_wid.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example we create slider control.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: April 2018
11 | """
12 |
13 | import wx
14 |
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | pnl = wx.Panel(self)
26 |
27 | sizer = wx.GridBagSizer(5, 5)
28 |
29 | sld = wx.Slider(pnl, value=200, minValue=150, maxValue=500,
30 | style=wx.SL_HORIZONTAL)
31 |
32 | sld.Bind(wx.EVT_SCROLL, self.OnSliderScroll)
33 | sizer.Add(sld, pos=(0, 0), flag=wx.ALL|wx.EXPAND, border=25)
34 |
35 | self.txt = wx.StaticText(pnl, label='200')
36 | sizer.Add(self.txt, pos=(0, 1), flag=wx.TOP|wx.RIGHT, border=25)
37 |
38 | sizer.AddGrowableCol(0)
39 | pnl.SetSizer(sizer)
40 |
41 | self.SetTitle('wx.Slider')
42 | self.Centre()
43 |
44 | def OnSliderScroll(self, e):
45 |
46 | obj = e.GetEventObject()
47 | val = obj.GetValue()
48 |
49 | self.txt.SetLabel(str(val))
50 |
51 |
52 | def main():
53 |
54 | app = wx.App()
55 | ex = Example(None)
56 | ex.Show()
57 | app.MainLoop()
58 |
59 |
60 | if __name__ == '__main__':
61 | main()
62 |
--------------------------------------------------------------------------------
/widgets/spin_ctrl.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this example we create spin control.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: April 2018
11 | """
12 |
13 | import wx
14 |
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | pnl = wx.Panel(self)
26 |
27 | sizer = wx.GridBagSizer(5, 5)
28 |
29 | st1 = wx.StaticText(pnl, label='Convert Fahrenheit temperature to Celsius')
30 | sizer.Add(st1, pos=(0, 0), span=(1, 2), flag=wx.ALL, border=15)
31 |
32 | st2 = wx.StaticText(pnl, label='Fahrenheit:')
33 | sizer.Add(st2, pos=(1, 0), flag=wx.ALL | wx.ALIGN_CENTER, border=15)
34 | self.sc = wx.SpinCtrl(pnl, value='0')
35 | self.sc.SetRange(-459, 1000)
36 |
37 | sizer.Add(self.sc, pos=(1, 1), flag=wx.ALIGN_CENTER)
38 |
39 | st3 = wx.StaticText(pnl, label='Celsius:')
40 | sizer.Add(st3, pos=(2, 0), flag=wx.ALL|wx.ALIGN_RIGHT, border=15)
41 |
42 | self.celsius = wx.StaticText(pnl, label='')
43 | sizer.Add(self.celsius, pos=(2, 1), flag=wx.ALL, border=15)
44 |
45 | computeButton = wx.Button(pnl, label='Compute')
46 | computeButton.SetFocus()
47 | sizer.Add(computeButton, pos=(3, 0), flag=wx.ALIGN_RIGHT|wx.TOP, border=30)
48 |
49 | closeButton = wx.Button(pnl, label='Close')
50 | sizer.Add(closeButton, pos=(3, 1), flag=wx.ALIGN_LEFT|wx.TOP, border=30)
51 |
52 | computeButton.Bind(wx.EVT_BUTTON, self.OnCompute)
53 | closeButton.Bind(wx.EVT_BUTTON, self.OnClose)
54 |
55 | pnl.SetSizer(sizer)
56 |
57 | self.SetTitle('wx.SpinCtrl')
58 | self.Centre()
59 |
60 | def OnClose(self, e):
61 |
62 | self.Close(True)
63 |
64 | def OnCompute(self, e):
65 |
66 | fahr = self.sc.GetValue()
67 | cels = round((fahr - 32) * 5 / 9.0, 2)
68 | self.celsius.SetLabel(str(cels))
69 |
70 |
71 | def main():
72 |
73 | app = wx.App()
74 | ex = Example(None)
75 | ex.Show()
76 | app.MainLoop()
77 |
78 |
79 | if __name__ == '__main__':
80 | main()
81 |
--------------------------------------------------------------------------------
/widgets/static_line.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this code example, we create a static line.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: April 2018
11 | """
12 |
13 | import wx
14 |
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | pnl = wx.Panel(self)
26 |
27 | font = wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD)
28 | heading = wx.StaticText(self, label='The Central Europe',
29 | pos=(25, 15), size=(200, -1))
30 | heading.SetFont(font)
31 |
32 | wx.StaticLine(self, pos=(25, 50), size=(300,1))
33 |
34 | wx.StaticText(self, label='Slovakia', pos=(25, 80))
35 | wx.StaticText(self, label='Hungary', pos=(25, 100))
36 | wx.StaticText(self, label='Poland', pos=(25, 120))
37 | wx.StaticText(self, label='Czech Republic', pos=(25, 140))
38 | wx.StaticText(self, label='Germany', pos=(25, 160))
39 | wx.StaticText(self, label='Slovenia', pos=(25, 180))
40 | wx.StaticText(self, label='Austria', pos=(25, 200))
41 | wx.StaticText(self, label='Switzerland', pos=(25, 220))
42 |
43 | wx.StaticText(self, label='5 445 000', pos=(250, 80))
44 | wx.StaticText(self, label='10 014 000', pos=(250, 100))
45 | wx.StaticText(self, label='38 186 000', pos=(250, 120))
46 | wx.StaticText(self, label='10 562 000', pos=(250, 140))
47 | wx.StaticText(self, label='81 799 000', pos=(250, 160))
48 | wx.StaticText(self, label='2 050 000', pos=(250, 180))
49 | wx.StaticText(self, label='8 414 000', pos=(250, 200))
50 | wx.StaticText(self, label='7 866 000', pos=(250, 220))
51 |
52 | wx.StaticLine(self, pos=(25, 260), size=(300,1))
53 |
54 | tsum = wx.StaticText(self, label='164 336 000', pos=(240, 280))
55 | sum_font = tsum.GetFont()
56 | sum_font.SetWeight(wx.BOLD)
57 | tsum.SetFont(sum_font)
58 |
59 | btn = wx.Button(self, label='Close', pos=(140, 310))
60 |
61 | btn.Bind(wx.EVT_BUTTON, self.OnClose)
62 |
63 | self.SetSize((360, 380))
64 | self.SetTitle('wx.StaticLine')
65 | self.Centre()
66 |
67 | def OnClose(self, e):
68 |
69 | self.Close(True)
70 |
71 |
72 | def main():
73 |
74 | app = wx.App()
75 | ex = Example(None)
76 | ex.Show()
77 | app.MainLoop()
78 |
79 |
80 | if __name__ == '__main__':
81 | main()
82 |
--------------------------------------------------------------------------------
/widgets/static_text.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this code example, we create a static text.
7 |
8 | author: Jan Bodnar
9 | website: www.zetcode.com
10 | last modified: April 2018
11 | """
12 |
13 | import wx
14 |
15 |
16 | class Example(wx.Frame):
17 |
18 | def __init__(self, *args, **kw):
19 | super(Example, self).__init__(*args, **kw)
20 |
21 | self.InitUI()
22 |
23 | def InitUI(self):
24 |
25 | txt1 = '''I'm giving up the ghost of love
26 | in the shadows cast on devotion
27 | She is the one that I adore
28 | creed of my silent suffocation
29 | Break this bittersweet spell on me
30 | lost in the arms of destiny'''
31 |
32 | txt2 = '''There is something in the way
33 | You're always somewhere else
34 | Feelings have deserted me
35 | To a point of no return
36 | I don't believe in God
37 | But I pray for you'''
38 |
39 | pnl = wx.Panel(self)
40 | vbox = wx.BoxSizer(wx.VERTICAL)
41 |
42 | font = wx.Font(13, wx.DEFAULT, wx.NORMAL, wx.DEFAULT)
43 |
44 | st1 = wx.StaticText(pnl, label=txt1, style=wx.ALIGN_LEFT)
45 | st2 = wx.StaticText(pnl, label=txt2, style=wx.ALIGN_LEFT)
46 |
47 | st1.SetFont(font)
48 | st2.SetFont(font)
49 |
50 | vbox.Add(st1, flag=wx.ALL, border=15)
51 | vbox.Add(st2, flag=wx.ALL, border=15)
52 | pnl.SetSizer(vbox)
53 |
54 | self.SetTitle('Bittersweet')
55 | self.Centre()
56 |
57 |
58 | def main():
59 |
60 | app = wx.App()
61 | ex = Example(None)
62 | ex.Show()
63 | app.MainLoop()
64 |
65 |
66 | if __name__ == '__main__':
67 | main()
68 |
--------------------------------------------------------------------------------
/widgets/toggle_buttons.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | """
4 | ZetCode wxPython tutorial
5 |
6 | In this code example, we create three
7 | toggle button widgets.
8 |
9 | author: Jan Bodnar
10 | website: www.zetcode.com
11 | last modified: April 2018
12 | """
13 |
14 | import wx
15 |
16 |
17 | class Example(wx.Frame):
18 |
19 | def __init__(self, *args, **kw):
20 | super(Example, self).__init__(*args, **kw)
21 |
22 | self.InitUI()
23 |
24 | def InitUI(self):
25 |
26 | pnl = wx.Panel(self)
27 |
28 | self.col = wx.Colour(0, 0, 0)
29 |
30 | rtb = wx.ToggleButton(pnl, label='red', pos=(20, 25))
31 | gtb = wx.ToggleButton(pnl, label='green', pos=(20, 60))
32 | btb = wx.ToggleButton(pnl, label='blue', pos=(20, 100))
33 |
34 | self.cpnl = wx.Panel(pnl, pos=(150, 20), size=(110, 110))
35 | self.cpnl.SetBackgroundColour(self.col)
36 |
37 | rtb.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleRed)
38 | gtb.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleGreen)
39 | btb.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleBlue)
40 |
41 | self.SetSize((350, 250))
42 | self.SetTitle('Toggle buttons')
43 | self.Centre()
44 |
45 | def ToggleRed(self, e):
46 |
47 | obj = e.GetEventObject()
48 | isPressed = obj.GetValue()
49 |
50 | green = self.col.Green()
51 | blue = self.col.Blue()
52 |
53 | if isPressed:
54 | self.col.Set(255, green, blue)
55 | else:
56 | self.col.Set(0, green, blue)
57 |
58 | self.cpnl.SetBackgroundColour(self.col)
59 | self.cpnl.Refresh()
60 |
61 | def ToggleGreen(self, e):
62 |
63 | obj = e.GetEventObject()
64 | isPressed = obj.GetValue()
65 |
66 | red = self.col.Red()
67 | blue = self.col.Blue()
68 |
69 | if isPressed:
70 | self.col.Set(red, 255, blue)
71 | else:
72 | self.col.Set(red, 0, blue)
73 |
74 | self.cpnl.SetBackgroundColour(self.col)
75 | self.cpnl.Refresh()
76 |
77 | def ToggleBlue(self, e):
78 |
79 | obj = e.GetEventObject()
80 | isPressed = obj.GetValue()
81 |
82 | red = self.col.Red()
83 | green = self.col.Green()
84 |
85 | if isPressed:
86 | self.col.Set(red, green, 255)
87 | else:
88 | self.col.Set(red, green, 0)
89 |
90 | self.cpnl.SetBackgroundColour(self.col)
91 | self.cpnl.Refresh()
92 |
93 |
94 | def main():
95 |
96 | app = wx.App()
97 | ex = Example(None)
98 | ex.Show()
99 | app.MainLoop()
100 |
101 |
102 | if __name__ == '__main__':
103 | main()
104 |
--------------------------------------------------------------------------------