Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 151663d

Browse filesBrowse files
committed
add more gui examples
1 parent bf9b3f7 commit 151663d
Copy full SHA for 151663d

File tree

Expand file treeCollapse file tree

6 files changed

+194
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

6 files changed

+194
-1
lines changed
Open diff view settings
Collapse file

‎chapter42_gui/button_events.py‎

Copy file name to clipboard
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# button_events.py
2+
3+
import wx
4+
5+
6+
class MyPanel(wx.Panel):
7+
8+
def __init__(self, parent):
9+
super().__init__(parent)
10+
11+
button = wx.Button(self, label='Press Me')
12+
button.Bind(wx.EVT_BUTTON, self.on_button1)
13+
button2 = wx.Button(self, label='Second button')
14+
button2.Bind(wx.EVT_BUTTON, self.on_button2)
15+
16+
main_sizer = wx.BoxSizer(wx.HORIZONTAL)
17+
main_sizer.Add(button, proportion=1,
18+
flag=wx.ALL | wx.CENTER | wx.EXPAND,
19+
border=5)
20+
main_sizer.Add(button2, 0, wx.ALL, 5)
21+
self.SetSizer(main_sizer)
22+
23+
def on_button1(self, event):
24+
print('You clicked the first button')
25+
26+
def on_button2(self, event):
27+
print('You clicked the second button')
28+
29+
30+
class MyFrame(wx.Frame):
31+
32+
def __init__(self):
33+
super().__init__(None, title='Hello World')
34+
panel = MyPanel(self)
35+
self.Show()
36+
37+
38+
if __name__ == '__main__':
39+
app = wx.App(redirect=False)
40+
frame = MyFrame()
41+
app.MainLoop()
Collapse file

‎chapter42_gui/hello_wx.py‎

Copy file name to clipboardExpand all lines: chapter42_gui/hello_wx.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
import wx
44

55
app = wx.App(False)
6-
frame = wx.Frame(None, title='Hello World')
6+
frame = wx.Frame(parent=None, title='Hello World')
77
frame.Show()
88
app.MainLoop()
Collapse file

‎chapter42_gui/hello_wx_class.py‎

Copy file name to clipboard
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# hello_wx_class.py
2+
3+
import wx
4+
5+
class MyFrame(wx.Frame):
6+
7+
def __init__(self):
8+
super().__init__(None, title='Hello World')
9+
self.Show()
10+
11+
if __name__ == '__main__':
12+
app = wx.App(False)
13+
frame = MyFrame()
14+
frame.Show()
15+
app.MainLoop()
Collapse file

‎chapter42_gui/image_viewer.py‎

Copy file name to clipboard
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# image_viewer.py
2+
3+
import wx
4+
5+
class ImagePanel(wx.Panel):
6+
7+
def __init__(self, parent, image_size):
8+
super().__init__(parent)
9+
10+
img = wx.Image(*image_size)
11+
self.image_ctrl = wx.StaticBitmap(self,
12+
bitmap=wx.Bitmap(img))
13+
browse_btn = wx.Button(self, label='Browse')
14+
15+
main_sizer = wx.BoxSizer(wx.VERTICAL)
16+
main_sizer.Add(self.image_ctrl, 0, wx.ALL, 5)
17+
main_sizer.Add(browse_btn)
18+
self.SetSizer(main_sizer)
19+
main_sizer.Fit(parent)
20+
self.Layout()
21+
22+
23+
class MainFrame(wx.Frame):
24+
25+
def __init__(self):
26+
super().__init__(None, title='Image Viewer')
27+
panel = ImagePanel(self, image_size=(240,240))
28+
self.Show()
29+
30+
31+
if __name__ == '__main__':
32+
app = wx.App(redirect=False)
33+
frame = MainFrame()
34+
app.MainLoop()
Collapse file
+78Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# image_viewer_working.py
2+
3+
import wx
4+
5+
class ImagePanel(wx.Panel):
6+
7+
def __init__(self, parent, image_size):
8+
super().__init__(parent)
9+
self.max_size = 240
10+
11+
img = wx.Image(*image_size)
12+
self.image_ctrl = wx.StaticBitmap(self,
13+
bitmap=wx.Bitmap(img))
14+
15+
browse_btn = wx.Button(self, label='Browse')
16+
browse_btn.Bind(wx.EVT_BUTTON, self.on_browse)
17+
18+
self.photo_txt = wx.TextCtrl(self, size=(200, -1))
19+
20+
main_sizer = wx.BoxSizer(wx.VERTICAL)
21+
hsizer = wx.BoxSizer(wx.HORIZONTAL)
22+
23+
main_sizer.Add(self.image_ctrl, 0, wx.ALL, 5)
24+
hsizer.Add(browse_btn, 0, wx.ALL, 5)
25+
hsizer.Add(self.photo_txt, 0, wx.ALL, 5)
26+
main_sizer.Add(hsizer, 0, wx.ALL, 5)
27+
28+
self.SetSizer(main_sizer)
29+
main_sizer.Fit(parent)
30+
self.Layout()
31+
32+
def on_browse(self, event):
33+
"""
34+
Browse for an image file
35+
@param event: The event object
36+
"""
37+
wildcard = "JPEG files (*.jpg)|*.jpg"
38+
with wx.FileDialog(None, "Choose a file",
39+
wildcard=wildcard,
40+
style=wx.ID_OPEN) as dialog:
41+
if dialog.ShowModal() == wx.ID_OK:
42+
self.photo_txt.SetValue(dialog.GetPath())
43+
self.load_image()
44+
45+
def load_image(self):
46+
"""
47+
Load the image and display it to the user
48+
"""
49+
filepath = self.photo_txt.GetValue()
50+
img = wx.Image(filepath, wx.BITMAP_TYPE_ANY)
51+
52+
# scale the image, preserving the aspect ratio
53+
W = img.GetWidth()
54+
H = img.GetHeight()
55+
if W > H:
56+
NewW = self.max_size
57+
NewH = self.max_size * H / W
58+
else:
59+
NewH = self.max_size
60+
NewW = self.max_size * W / H
61+
img = img.Scale(NewW,NewH)
62+
63+
self.image_ctrl.SetBitmap(wx.Bitmap(img))
64+
self.Refresh()
65+
66+
67+
class MainFrame(wx.Frame):
68+
69+
def __init__(self):
70+
super().__init__(None, title='Image Viewer')
71+
panel = ImagePanel(self, image_size=(240,240))
72+
self.Show()
73+
74+
75+
if __name__ == '__main__':
76+
app = wx.App(redirect=False)
77+
frame = MainFrame()
78+
app.MainLoop()
Collapse file

‎chapter42_gui/stacked_buttons.py‎

Copy file name to clipboard
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# stacked_buttons.py
2+
3+
import wx
4+
5+
6+
class MyPanel(wx.Panel):
7+
8+
def __init__(self, parent):
9+
super().__init__(parent)
10+
button = wx.Button(self, label='Press Me')
11+
button2 = wx.Button(self, label='Press Me too')
12+
button3 = wx.Button(self, label='Another button')
13+
14+
class MyFrame(wx.Frame):
15+
16+
def __init__(self):
17+
super().__init__(None, title='Hello World')
18+
panel = MyPanel(self)
19+
self.Show()
20+
21+
22+
if __name__ == '__main__':
23+
app = wx.App(redirect=False)
24+
frame = MyFrame()
25+
app.MainLoop()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.