Skip to content

Commit 1444181

Browse files
committed
First commit
0 parents  commit 1444181

File tree

196 files changed

+9217
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

196 files changed

+9217
-0
lines changed

Chapter-01/bare.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import wx
2+
3+
class App(wx.App):
4+
5+
def OnInit(self):
6+
frame = wx.Frame(parent=None, title='Bare')
7+
frame.Show()
8+
return True
9+
10+
app = App()
11+
app.MainLoop()

Chapter-01/hello.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python
2+
3+
"""Hello, wxPython! program."""
4+
5+
import wx
6+
7+
class Frame(wx.Frame):
8+
"""Frame class that displays an image."""
9+
10+
def __init__(self, image, parent=None, id=-1,
11+
pos=wx.DefaultPosition, title='Hello, wxPython!'):
12+
"""Create a Frame instance and display image."""
13+
temp = image.ConvertToBitmap()
14+
size = temp.GetWidth(), temp.GetHeight()
15+
wx.Frame.__init__(self, parent, id, title, pos, size)
16+
self.bmp = wx.StaticBitmap(parent=self, bitmap=temp)
17+
self.SetClientSize(size)
18+
19+
class App(wx.App):
20+
"""Application class."""
21+
22+
def OnInit(self):
23+
image = wx.Image('wxPython.jpg', wx.BITMAP_TYPE_JPEG)
24+
self.frame = Frame(image)
25+
self.frame.Show()
26+
self.SetTopWindow(self.frame)
27+
return True
28+
29+
def main():
30+
app = App()
31+
app.MainLoop()
32+
33+
if __name__ == '__main__':
34+
main()
35+

Chapter-01/python_compare.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import wx
2+
3+
class MyApp(wx.App):
4+
5+
def OnInit(self):
6+
frame = MyFrame("Hello World", (50, 60), (450, 340))
7+
frame.Show()
8+
self.SetTopWindow(frame)
9+
return True
10+
11+
class MyFrame(wx.Frame):
12+
13+
def __init__(self, title, pos, size):
14+
wx.Frame.__init__(self, None, -1, title, pos, size)
15+
menuFile = wx.Menu()
16+
menuFile.Append(1, "&About...")
17+
menuFile.AppendSeparator()
18+
menuFile.Append(2, "E&xit")
19+
menuBar = wx.MenuBar()
20+
menuBar.Append(menuFile, "&File")
21+
self.SetMenuBar(menuBar)
22+
self.CreateStatusBar()
23+
self.SetStatusText("Welcome to wxPython!")
24+
self.Bind(wx.EVT_MENU, self.OnAbout, id=1)
25+
self.Bind(wx.EVT_MENU, self.OnQuit, id=2)
26+
27+
def OnQuit(self, event):
28+
self.Close()
29+
30+
def OnAbout(self, event):
31+
wx.MessageBox("This is a wxPython Hello world sample",
32+
"About Hello World", wx.OK | wx.ICON_INFORMATION, self)
33+
34+
if __name__ == '__main__':
35+
app = MyApp(False)
36+
app.MainLoop()

Chapter-01/sample.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/env python
2+
import wx
3+
4+
class MyFrame(wx.Frame):
5+
6+
def __init__(self):
7+
wx.Frame.__init__(self, None, -1, "My Frame", size=(300, 300))
8+
panel = wx.Panel(self, -1)
9+
panel.Bind(wx.EVT_MOTION, self.OnMove)
10+
wx.StaticText(panel, -1, "Pos:", pos=(10, 12))
11+
self.posCtrl = wx.TextCtrl(panel, -1, "", pos=(40, 10))
12+
13+
def OnMove(self, event):
14+
pos = event.GetPosition()
15+
self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y))
16+
17+
if __name__ == '__main__':
18+
app = wx.PySimpleApp()
19+
frame = MyFrame()
20+
frame.Show(True)
21+
app.MainLoop()

Chapter-01/spare.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python
2+
3+
"""Spare.py is a starting point for simple wxPython programs."""
4+
5+
import wx
6+
7+
class Frame(wx.Frame):
8+
pass
9+
10+
class App(wx.App):
11+
12+
def OnInit(self):
13+
self.frame = Frame(parent=None, title='Spare')
14+
self.frame.Show()
15+
self.SetTopWindow(self.frame)
16+
return True
17+
18+
if __name__ == '__main__':
19+
app = App()
20+
app.MainLoop()

Chapter-01/wxPython.jpg

14.2 KB
Loading

Chapter-02/dialog_scratch.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
3+
import wx
4+
import images
5+
6+
class App(wx.App):
7+
8+
def __init__(self, redirect=True, filename=None):
9+
wx.App.__init__(self, redirect, filename)
10+
11+
def OnInit(self):
12+
dlg = wx.MessageDialog(None, 'Is this the coolest thing ever!',
13+
'MessageDialog', wx.YES_NO | wx.ICON_QUESTION)
14+
result = dlg.ShowModal()
15+
dlg.Destroy()
16+
17+
dlg = wx.TextEntryDialog(None, "Who is buried in Grant's tomb?",
18+
'A Question', 'Cary Grant')
19+
if dlg.ShowModal() == wx.ID_OK:
20+
response = dlg.GetValue()
21+
dlg.Destroy()
22+
23+
dlg = wx.SingleChoiceDialog(None,
24+
'What version of Python are you using?', 'Single Choice',
25+
['1.5.2', '2.0', '2.1.3', '2.2', '2.3.1'])
26+
if dlg.ShowModal() == wx.ID_OK:
27+
response = dlg.GetStringSelection()
28+
dlg.Destroy()
29+
30+
return True
31+
32+
33+
if __name__ == '__main__':
34+
app = App(False, "output")
35+
fred = app.MainLoop()
36+

Chapter-02/images.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#----------------------------------------------------------------------
2+
# This file was generated by encode_bitmaps.py
3+
#
4+
from wx import ImageFromStream, BitmapFromImage
5+
from wx import EmptyIcon
6+
import cStringIO
7+
8+
def getNewData():
9+
return \
10+
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x0f\x08\x06\
11+
\x00\x00\x00\xedsO/\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\x00\
12+
\x00YIDATx\x9c\xed\xd31\n@!\x0c\x03\xd0\xa4\xfe\xfb\xdfX\xe3\xf0\x97R\xa5(.\
13+
\x0ef\x13\xe45\xa2\x92Vp\x92\xcf/\xd4\xaa\xb2\xcd\xb4\xc2\x14\x00\x00in\x90\
14+
\x84ZUDl\xa9\xa7\xc3c\xcb-\x80\xfc\x87{d8B6=B\xdb\rfy\xc0\r\xc0\xf0\x0e\xfc\
15+
\x1d\xaf\x84\xa7\xbf\xb1\x03\xe1,\x19&\x93\x9a\xd2\x97\x00\x00\x00\x00IEND\
16+
\xaeB`\x82'
17+
18+
def getNewBitmap():
19+
return BitmapFromImage(getNewImage())
20+
21+
def getNewImage():
22+
stream = cStringIO.StringIO(getNewData())
23+
return ImageFromStream(stream)
24+

Chapter-02/images.pyc

965 Bytes
Binary file not shown.

Chapter-02/insert.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python
2+
3+
import wx
4+
5+
class InsertFrame(wx.Frame):
6+
7+
def __init__(self, parent, id):
8+
wx.Frame.__init__(self, parent, id, 'Frame With Button',
9+
size=(300, 100))
10+
panel = wx.Panel(self)
11+
button = wx.Button(panel, label="Close", pos=(125, 10),
12+
size=(50, 50))
13+
self.Bind(wx.EVT_BUTTON, self.OnCloseMe, button)
14+
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
15+
16+
def OnCloseMe(self, event):
17+
self.Close(True)
18+
19+
def OnCloseWindow(self, event):
20+
self.Destroy()
21+
22+
if __name__ == '__main__':
23+
app = wx.PySimpleApp()
24+
frame = InsertFrame(parent=None, id=-1)
25+
frame.Show()
26+
app.MainLoop()
27+

Chapter-02/startup.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python
2+
3+
import wx
4+
import sys
5+
6+
class Frame(wx.Frame):
7+
8+
def __init__(self, parent, id, title):
9+
print "Frame __init__"
10+
wx.Frame.__init__(self, parent, id, title)
11+
12+
class App(wx.App):
13+
14+
def __init__(self, redirect=True, filename=None):
15+
print "App __init__"
16+
wx.App.__init__(self, redirect, filename)
17+
18+
def OnInit(self):
19+
print "OnInit"
20+
self.frame = Frame(parent=None, id=-1, title='Startup Window')
21+
self.frame.Show()
22+
self.SetTopWindow(self.frame)
23+
print >> sys.stderr, "A pretend error message"
24+
print "app name: <", self.GetVendorName(), ">"
25+
return True
26+
27+
def OnExit(self):
28+
print "OnExit"
29+
30+
if __name__ == '__main__':
31+
app = App(redirect=True)
32+
print "before MainLoop"
33+
fred = app.MainLoop()
34+
print "after MainLoop", fred
35+

Chapter-02/toolbar.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
3+
import wx
4+
import images
5+
6+
class ToolbarFrame(wx.Frame):
7+
8+
def __init__(self, parent, id):
9+
wx.Frame.__init__(self, parent, id, 'Toolbars',
10+
size=(300, 200))
11+
panel = wx.Panel(self)
12+
panel.SetBackgroundColour('White')
13+
statusBar = self.CreateStatusBar()
14+
toolbar = self.CreateToolBar()
15+
toolbar.AddSimpleTool(wx.NewId(), images.getNewBitmap(),
16+
"New", "Long help for 'New'")
17+
toolbar.Realize()
18+
menuBar = wx.MenuBar()
19+
menu1 = wx.Menu()
20+
menuBar.Append(menu1, "&File")
21+
menu2 = wx.Menu()
22+
menu2.Append(wx.NewId(), "&Copy", "Copy in status bar")
23+
menu2.Append(wx.NewId(), "C&ut", "")
24+
menu2.Append(wx.NewId(), "Paste", "")
25+
menu2.AppendSeparator()
26+
menu2.Append(wx.NewId(), "&Options...", "Display Options")
27+
menuBar.Append(menu2, "&Edit")
28+
self.SetMenuBar(menuBar)
29+
30+
def OnCloseMe(self, event):
31+
self.Close(True)
32+
33+
def OnCloseWindow(self, event):
34+
self.Destroy()
35+
36+
if __name__ == '__main__':
37+
app = wx.PySimpleApp()
38+
frame = ToolbarFrame(parent=None, id=-1)
39+
frame.Show()
40+
app.MainLoop()
41+

Chapter-02/toolbar.pyc

1.88 KB
Binary file not shown.

Chapter-03/customEvent.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import wx
2+
3+
class TwoButtonEvent(wx.PyCommandEvent):
4+
def __init__(self, evtType, id):
5+
wx.PyCommandEvent.__init__(self, evtType, id)
6+
self.clickCount = 0
7+
8+
def GetClickCount(self):
9+
return self.clickCount
10+
11+
def SetClickCount(self, count):
12+
self.clickCount = count
13+
14+
myEVT_TWO_BUTTON = wx.NewEventType()
15+
EVT_TWO_BUTTON = wx.PyEventBinder(myEVT_TWO_BUTTON, 1)
16+
17+
class TwoButtonPanel(wx.Panel):
18+
def __init__(self, parent, id=-1, leftText="Left",
19+
rightText="Right"):
20+
wx.Panel.__init__(self, parent, id)
21+
self.leftButton = wx.Button(self, label=leftText)
22+
self.rightButton = wx.Button(self, label=rightText,
23+
pos=(100,0))
24+
self.leftClick = False
25+
self.rightClick = False
26+
self.clickCount = 0
27+
self.leftButton.Bind(wx.EVT_LEFT_DOWN, self.OnLeftClick)
28+
self.rightButton.Bind(wx.EVT_LEFT_DOWN, self.OnRightClick)
29+
30+
def OnLeftClick(self, event):
31+
self.leftClick = True
32+
self.OnClick()
33+
event.Skip()
34+
35+
def OnRightClick(self, event):
36+
self.rightClick = True
37+
self.OnClick()
38+
event.Skip()
39+
40+
def OnClick(self):
41+
self.clickCount += 1
42+
if self.leftClick and self.rightClick:
43+
self.leftClick = False
44+
self.rightClick = False
45+
evt = TwoButtonEvent(myEVT_TWO_BUTTON, self.GetId())
46+
evt.SetClickCount(self.clickCount)
47+
self.GetEventHandler().ProcessEvent(evt)
48+
49+
50+
class CustomEventFrame(wx.Frame):
51+
def __init__(self, parent, id):
52+
wx.Frame.__init__(self, parent, id, 'Click Count: 0',
53+
size=(300, 100))
54+
panel = TwoButtonPanel(self)
55+
self.Bind(EVT_TWO_BUTTON, self.OnTwoClick, panel)
56+
57+
def OnTwoClick(self, event):
58+
self.SetTitle("Click Count: %s" % event.GetClickCount())
59+
60+
if __name__ == '__main__':
61+
app = wx.PySimpleApp()
62+
frame = CustomEventFrame(parent=None, id=-1)
63+
frame.Show()
64+
app.MainLoop()

Chapter-03/double_event_one.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
3+
import wx
4+
5+
class DoubleEventFrame(wx.Frame):
6+
7+
def __init__(self, parent, id):
8+
wx.Frame.__init__(self, parent, id, 'Frame With Button',
9+
size=(300, 100))
10+
self.panel = wx.Panel(self, -1)
11+
self.button = wx.Button(self.panel, -1, "Click Me", pos=(100, 15))
12+
self.Bind(wx.EVT_BUTTON, self.OnButtonClick, self.button)
13+
self.button.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
14+
15+
def OnButtonClick(self, event):
16+
self.panel.SetBackgroundColour('Green')
17+
self.panel.Refresh()
18+
19+
def OnMouseDown(self, event):
20+
self.button.SetLabel("Again!")
21+
event.Skip()
22+
23+
if __name__ == '__main__':
24+
app = wx.PySimpleApp()
25+
frame = DoubleEventFrame(parent=None, id=-1)
26+
frame.Show()
27+
app.MainLoop()
28+

0 commit comments

Comments
 (0)