Skip to content

Commit 155a061

Browse files
committed
Some updating for python3, wxPython4, and style
1 parent 1316ab9 commit 155a061

File tree

4 files changed

+94
-87
lines changed

4 files changed

+94
-87
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
3+

BlitTest.py

+48-42
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,101 @@
11
#!/usr/bin/env python
22

3-
import wx, random
3+
"""
4+
demo oif blitting from a MemoryDC to the screen using DC.DrawBitmap
5+
"""
6+
7+
from __future__ import print_function, unicode_literals
8+
9+
import random
10+
import wx
11+
412

513
class MainWindow(wx.Frame):
6-
""" This window displays a button """
7-
def __init__(self,parent,id,title):
8-
wx.Frame.__init__(self, None, -1, "Blit Test",
9-
wx.DefaultPosition,
10-
size=(500,500),
11-
style=wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
12-
13-
wx.EVT_CLOSE(self,self.OnQuit)
14+
""" This window displays some random lines """
15+
16+
def __init__(self, parent, id, title):
17+
wx.Frame.__init__(
18+
self,
19+
None,
20+
-1,
21+
"Blit Test",
22+
wx.DefaultPosition,
23+
size=(500, 500),
24+
style=wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
25+
26+
wx.EVT_CLOSE(self, self.OnQuit)
1427
self.Bind(wx.EVT_TIMER, self.OnTimer)
1528
self.Bind(wx.EVT_SIZE, self.BuildImage)
1629

1730
self.Numtimer = 0
1831
self.NumLines = 100
1932

20-
self.t=wx.Timer(self)
33+
self.t = wx.Timer(self)
2134
self.BuildImage()
2235
self.t.Start(100)
23-
24-
def OnQuit(self,Event):
36+
37+
def OnQuit(self, Event):
2538
self.Destroy()
2639

27-
def BuildImage(self, event = None):
28-
Size = self.GetClientSizeTuple()
40+
def BuildImage(self, event=None):
41+
Size = self.ClientSize
2942

3043
# Make new offscreen bitmap: this bitmap will always have the
3144
# current drawing in it, so it can be used to save the image to
3245
# a file, or whatever.
33-
print "making new buffer:",Size
34-
self._Buffer = wx.EmptyBitmap(Size[0],Size[1])
46+
print("making new buffer:", Size)
47+
self._Buffer = wx.EmptyBitmap(Size[0], Size[1])
3548

3649
dc = wx.MemoryDC()
3750
dc.SelectObject(self._Buffer)
3851

39-
4052
self.Lines = []
4153
for i in range(self.NumLines):
42-
x1,y1,x2,y2 = (random.randint(1,max(Size)),
43-
random.randint(1,max(Size)),
44-
random.randint(1,max(Size)),
45-
random.randint(1,max(Size)))
46-
54+
x1, y1, x2, y2 = (random.randint(1, max(Size)),
55+
random.randint(1, max(Size)),
56+
random.randint(1, max(Size)),
57+
random.randint(1, max(Size)))
58+
4759
color = self.random_color()
48-
self.Lines.append( [color, (x1,y1,x2,y2)] )
49-
50-
dc.BeginDrawing()
60+
self.Lines.append([color, (x1, y1, x2, y2)])
61+
5162
dc.Clear()
5263
for line in self.Lines:
5364
dc.SetPen(wx.Pen(line[0], 2))
5465
dc.DrawLine(*line[1])
55-
dc.EndDrawing()
5666

57-
def OnTimer(self,event):
67+
def OnTimer(self, event):
5868
self.Numtimer += 1
59-
print "Timer fired: %i times"%self.Numtimer
69+
print("Timer fired: %i times" % self.Numtimer)
6070

6171
# change one color:
6272
self.Lines[random.randrange(self.NumLines)][0] = self.random_color()
6373
# update the screen
6474
dc = wx.MemoryDC()
6575
dc.SelectObject(self._Buffer)
66-
dc.BeginDrawing()
6776
dc.Clear()
6877
for line in self.Lines:
6978
dc.SetPen(wx.Pen(line[0], 2))
7079
dc.DrawLine(*line[1])
71-
dc.EndDrawing()
72-
del dc
73-
wx.ClientDC(self).DrawBitmap(self._Buffer,0,0)
74-
80+
# del dc
81+
wx.ClientDC(self).DrawBitmap(self._Buffer, 0, 0)
82+
7583
def random_color(self):
76-
return apply(wx.Colour,(random.randrange(255),random.randrange(255),random.randrange(255)))
84+
return wx.Colour(random.randrange(255),
85+
random.randrange(255),
86+
random.randrange(255),
87+
)
7788

7889

7990
class MyApp(wx.App):
8091
def OnInit(self):
8192

82-
frame = MainWindow(None, -1, "BlitTest")
93+
frame = MainWindow(None, wx.ID_ANY, title="BlitTest")
8394
self.SetTopWindow(frame)
8495
frame.Show()
85-
96+
8697
return True
87-
98+
8899

89100
app = MyApp(0)
90101
app.MainLoop()
91-
92-
93-
94-
95-

CalculatorDemo.py

+25-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python
2-
32
"""
43
wxPython Calculator Demo in 50 lines of code
54
@@ -11,79 +10,78 @@
1110
It has been altered to allow it to be "driven" by an external script,
1211
plus a little layout improvement
1312
14-
See CalcualtorDemoDriver.py
13+
See CalcualtorDemoDriver.py
1514
1615
for an example
1716
"""
1817

19-
2018
# Calculator GUI:
2119

2220
# ___________v
23-
# [7][8][9][/]
21+
# [7][8][9][/]
2422
# [4][5][6][*]
2523
# [1][2][3][-]
2624
# [0][.][C][+]
2725
# [ = ]
2826

29-
from __future__ import division # So that 8/3 will be 2.6666 and not 2
27+
from __future__ import (division, unicode_literals, print_function)
3028
import wx
31-
from math import * # So we can evaluate "sqrt(8)"
29+
from math import * # So we can evaluate "sqrt(8)" and others
3230

3331

3432
class Calculator(wx.Panel):
3533
'''Main calculator dialog'''
34+
3635
def __init__(self, *args, **kwargs):
3736
wx.Panel.__init__(self, *args, **kwargs)
38-
sizer = wx.BoxSizer(wx.VERTICAL) # Main vertical sizer
37+
sizer = wx.BoxSizer(wx.VERTICAL) # Main vertical sizer
3938

40-
self.display = wx.ComboBox(self) # Current calculation
41-
sizer.Add(self.display, 0, wx.EXPAND|wx.BOTTOM, 8) # Add to main sizer
39+
self.display = wx.ComboBox(self) # Current calculation
40+
sizer.Add(self.display, 0, wx.EXPAND | wx.BOTTOM,
41+
8) # Add to main sizer
4242

43-
# [7][8][9][/]
43+
# [7][8][9][/]
4444
# [4][5][6][*]
4545
# [1][2][3][-]
4646
# [0][.][C][+]
4747
gsizer = wx.GridSizer(4, 4, 8, 8)
48-
for row in (("7", "8", "9", "/"),
49-
("4", "5", "6", "*"),
50-
("1", "2", "3", "-"),
51-
("0", ".", "C", "+")):
48+
for row in (("7", "8", "9", "/"), ("4", "5", "6", "*"),
49+
("1", "2", "3", "-"), ("0", ".", "C", "+")):
5250
for label in row:
53-
b = wx.Button(self, label=label, size=(40,-1))
51+
b = wx.Button(self, label=label, size=(40, -1))
5452
gsizer.Add(b)
5553
b.Bind(wx.EVT_BUTTON, self.OnButton)
5654
sizer.Add(gsizer, 1, wx.EXPAND)
5755

5856
# [ = ]
5957
b = wx.Button(self, label="=")
6058
b.Bind(wx.EVT_BUTTON, self.OnButton)
61-
sizer.Add(b, 0, wx.EXPAND|wx.ALL, 8)
59+
sizer.Add(b, 0, wx.EXPAND | wx.ALL, 8)
6260
self.equal = b
6361

6462
# Set sizer and center
6563
self.SetSizerAndFit(sizer)
6664

6765
def OnButton(self, evt):
6866
'''Handle button click event'''
69-
67+
7068
# Get title of clicked button
7169
label = evt.GetEventObject().GetLabel()
7270

73-
if label == "=": # Calculate
71+
if label == "=": # Calculate
7472
self.Calculate()
75-
elif label == "C": # Clear
73+
elif label == "C": # Clear
7674
self.display.SetValue("")
7775

78-
else: # Just add button text to current calculation
76+
else: # Just add button text to current calculation
7977
self.display.SetValue(self.display.GetValue() + label)
8078
self.display.SetInsertionPointEnd()
81-
self.equal.SetFocus() # Set the [=] button in focus
79+
self.equal.SetFocus() # Set the [=] button in focus
8280

8381
def Calculate(self):
8482
"""
8583
do the calculation itself
86-
84+
8785
in a separate method, so it can be called outside of a button event handler
8886
"""
8987
try:
@@ -100,20 +98,21 @@ def Calculate(self):
10098

10199
# Show result
102100
self.display.SetValue(str(result))
103-
except Exception, e:
101+
except Exception as e:
104102
wx.LogError(str(e))
105103
return
106104

107105
def ComputeExpression(self, expression):
108106
"""
109107
Compute the expression passed in.
110-
108+
111109
This can be called from another class, module, etc.
112110
"""
113-
print "ComputeExpression called with:", expression
111+
print("ComputeExpression called with:", expression)
114112
self.display.SetValue(expression)
115113
self.Calculate()
116114

115+
117116
class MainFrame(wx.Frame):
118117
def __init__(self, *args, **kwargs):
119118
kwargs.setdefault('title', "Calculator")
@@ -123,7 +122,7 @@ def __init__(self, *args, **kwargs):
123122

124123
# put the panel on -- in a sizer to give it some space
125124
S = wx.BoxSizer(wx.VERTICAL)
126-
S.Add(self.calcPanel, 1, wx.GROW|wx.ALL, 10)
125+
S.Add(self.calcPanel, 1, wx.GROW | wx.ALL, 10)
127126
self.SetSizerAndFit(S)
128127
self.CenterOnScreen()
129128

@@ -134,4 +133,3 @@ def __init__(self, *args, **kwargs):
134133
frame = MainFrame(None)
135134
frame.Show()
136135
app.MainLoop()
137-

FontSizeTest.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#!/usr/bin/env python
22
"""
3-
Simple demo/sample for testing Font Size with a DC -- using wx.FontFromPixel Size
4-
3+
Simple demo/sample for testing Font Size with a DC --
4+
using wx.FontFromPixel Size
55
"""
66

7+
from __future__ import division, unicode_literals, print_function
8+
79
import wx
810

11+
912
class MyPanel(wx.Panel):
1013
def __init__(self, *args, **kwargs):
1114
wx.Panel.__init__(self, *args, **kwargs)
@@ -15,44 +18,41 @@ def __init__(self, *args, **kwargs):
1518
def OnPaint(self, event):
1619
dc = wx.PaintDC(self)
1720
self.Draw(dc)
18-
21+
1922
def Draw(self, dc):
2023
dc.Clear()
21-
x,y = 20, 0
24+
x, y = 20, 0
2225
for fs in [8, 10, 12, 14, 18, 20, 30, 60]:
23-
y += 1.2*fs
26+
y += 1.2 * fs
2427
w = fs * 11
25-
S = (0.45*fs, fs) # this hieght/width ratio seems to match what I get on OS-X and GTK
26-
text = "%i pixel Font and Box"%fs
27-
Font = wx.FontFromPixelSize(S, wx.SWISS, wx.NORMAL, wx.NORMAL, underlined=True)
28+
S = wx.Size((0, fs))
29+
# S = wx.Size((fs, fs))
30+
text = "%i pixel Font and Box" % fs
31+
Font = wx.Font(S, wx.SWISS, wx.NORMAL, wx.NORMAL, underline=True)
2832
dc.SetFont(Font)
2933
E = dc.GetTextExtent(text)
3034
dc.SetFont(Font)
3135
E = dc.GetTextExtent(text)
32-
print "Font size: %s, Extent ratio: %s"%(S, E[0] / E[1])
33-
print "font point size::", Font.GetPointSize()
36+
print("Font size: %s, Extent ratio: %s" % (S, E[0] / E[1]))
37+
print("font point size::", Font.GetPointSize())
3438
dc.DrawText(text, x, y)
3539
dc.DrawRectangle(x, y, w, fs)
3640
dc.DrawText(text, x, y)
3741

42+
3843
class MyFrame(wx.Frame):
3944
def __init__(self, parent):
40-
wx.Frame.__init__(self, parent, title="test", size = (500, 500))
45+
wx.Frame.__init__(self, parent, title="test", size=(500, 500))
4146

4247
self.Panel = MyPanel(self)
4348

4449
sizer = wx.BoxSizer(wx.VERTICAL)
45-
sizer.Add(self.Panel,1,wx.EXPAND)
50+
sizer.Add(self.Panel, 1, wx.EXPAND)
4651

4752
self.SetSizer(sizer)
48-
49-
def UpdatePanel(self,event):
50-
#self.Panel.DrawRect()
51-
pass
52-
53+
5354
if __name__ == '__main__':
5455
app = wx.App(0)
5556
frame = MyFrame(None)
5657
frame.Show()
5758
app.MainLoop()
58-

0 commit comments

Comments
 (0)