Skip to content

Commit 5f42549

Browse files
2 parents fbaabb3 + 16ded0f commit 5f42549

File tree

4 files changed

+141
-136
lines changed

4 files changed

+141
-136
lines changed

AutoSizeBitmap.py

+48-19
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,83 @@
55
66
Example for how to have a bitmap autosize itself in wxPython
77
"""
8-
8+
import math
99
import wx
10-
print "using wxPython version:", wx.__version__
10+
11+
KEEP_ASPECT_RATIO = True
12+
13+
14+
class AutoSizeFrame(wx.Frame):
15+
def __init__(self, image, *args, **kwargs):
16+
wx.Frame.__init__(self, *args, **kwargs)
17+
18+
self.aspect_ratio = float(image.Width) / float(image.Height)
19+
self.canvas = AutoSizeBitmap(image, self)
20+
self.canvas.SetSize((300, 400))
21+
22+
self.Bind(wx.EVT_SIZE, self.OnSize)
23+
24+
self.Fit()
25+
self.Show()
26+
27+
def OnSize(self, evt=None):
28+
size = self.Size
29+
if (size[0] > 0 and size[1] > 0):
30+
width, height = size
31+
if KEEP_ASPECT_RATIO:
32+
total_size = width * height
33+
height = int(math.sqrt(total_size / self.aspect_ratio))
34+
width = int(total_size / height)
35+
# resize window on the fly to keep the aspect ratio
36+
self.SetSize((width, height))
37+
self.canvas.SetSize((width, height))
38+
1139

1240
class AutoSizeBitmap(wx.Window):
1341
"""
1442
A subclass of wx.Window that will hold an image (much like a StaticBitmap),
1543
but re-size it to fit the current size of the Window
16-
" """
17-
def __init__(self, parent, image, *args, **kwargs):
44+
"""
45+
def __init__(self, image, *args, **kwargs):
1846
"""
1947
initialize an AutoSizeBitmap
20-
48+
2149
:param parent: parent Window for this window
2250
:param image: a wx.Image that you want to display
23-
"""
24-
wx.Window.__init__(self, parent, *args, **kwargs)
51+
"""
52+
wx.Window.__init__(self, *args, **kwargs)
2553

2654
self.orig_image = image
2755
self.bitmap = None
28-
56+
self.prev_size = self.Size
57+
2958
self.Bind(wx.EVT_SIZE, self.OnSize)
3059
self.Bind(wx.EVT_PAINT, self.OnPaint)
3160

3261
def OnSize(self, evt=None):
33-
img = self.orig_image.Copy()
34-
img.Rescale(*self.Size)
35-
self.bitmap = wx.BitmapFromImage(img)
36-
self.Refresh()
62+
size = self.Size
63+
if size[0] > 0 and size[1] > 0:
64+
img = self.orig_image.Copy()
65+
img.Rescale(*size)
66+
self.bitmap = wx.BitmapFromImage(img)
67+
self.Refresh()
3768

3869
def OnPaint(self, evt=None):
3970
dc = wx.PaintDC(self)
4071
try:
41-
dc.DrawBitmap(self.bitmap,0,0)
42-
except ValueError: # in case bitmap has not yet been initialized
72+
dc.DrawBitmap(self.bitmap, 0, 0)
73+
except ValueError: # in case bitmap has not yet been initialized
4374
pass
4475

4576
if __name__ == "__main__":
4677
import sys
47-
48-
try:
78+
79+
try:
4980
filename = sys.argv[1]
5081
except:
5182
filename = "Images/cute_close_up.jpg"
5283
App = wx.App(False)
53-
f = wx.Frame(None)
5484
img = wx.Image(filename)
55-
b = AutoSizeBitmap(f, img)
56-
f.Show()
85+
f = AutoSizeFrame(img, None, size=(400, 600))
5786
App.MainLoop()
5887

Jeopardy.py

+48-70
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
lets you run a simple game yourself, with questions and
88
answers provided by you.
99
10+
NOTE: very much a work in progress!
11+
1012
This code is in the public domain
1113
1214
1315
1416
"""
1517

16-
1718
import wx
1819

20+
1921
class Question(object):
2022
"""
2123
A class to hold each question, and data about it.
@@ -182,7 +184,6 @@ def Draw(self, dc):
182184
# dc.DrawLine(x0, y0 + d*i, x0 + d*3, y0 + d*i)
183185
# dc.DrawLine(x0 + d*i, y0, x0 + d*i, y0 + d*3)
184186

185-
186187
def OnPaint(self, event):
187188
dc = wx.BufferedPaintDC(self, self.buffer)
188189

@@ -191,7 +192,7 @@ def OnLeftDown(self, e):
191192
grid = self.grid
192193
x, y = e.GetPositionTuple()
193194
i = x / grid.box_w
194-
j = y / grid.box_h
195+
j = y / grid.box_h - 1 # compensate for header
195196
if i >= 0 and i < grid.num_cat and j >= 0 and j < grid.num_ques:
196197
self.game.questions[i][j].answered = not self.game.questions[i][j].answered
197198
self.DrawNow()
@@ -378,92 +379,69 @@ def wrap_to_width(self):
378379
# self.BoxHeight = BoxHeight
379380
# self.CalcBoundingBox()
380381

382+
# def draw(self, DC):
383+
# for word in question:
384+
# pass
381385

382-
def draw(self, DC):
383-
for word in question:
384-
pass
385-
386-
387-
# class TestFrame(wx.Frame):
388-
# def __init__(self, *args, **kwargs):
389-
# wx.Frame.__init__(self, *args, **kwargs)
390-
391-
# self.Bind(wx.EVT_PAINT, self.OnPaint)
392-
# #self.header = Header("This is a pretty long question that will need to wrap")
393-
394-
# #header.wrap_to_width
395-
# #print header.question_lines
396-
397-
# def OnPaint(self, evt):
398-
# dc = wx.PaintDC(self)
399-
# #if self.header is not None:
400-
# # self.header.draw(dc)
401-
402-
403-
# if __name__ == '__main__':
404-
# A = wx.App()
405-
# F = TestFrame(None, title="test frame")
406-
# F.Show()
407-
# A.MainLoop()
408386

409387
if __name__ == '__main__':
410388

411389
catagories = [None for i in range(6)]
412-
questions = [ [None for i in range(5)] for j in range(6) ]
413-
## test data:
390+
questions = [[None for i in range(5)] for j in range(6)]
391+
392+
# test data:
414393

415394
catagories[0] = "Household Pets"
416-
questions[0][0] = Question("slobbery","what is a dog?", 100)
417-
questions[0][1] = Question("cute an fuzzy","what is a cat?", 200)
418-
questions[0][2] = Question("long and slithery","what is a snake?", 300)
419-
questions[0][3] = Question("sometimes lives in a sewer","what is a rat?", 400)
420-
questions[0][4] = Question("a reptile often mistaken for an amphibian","what is a turtle?", 500)
395+
questions[0][0] = Question("slobbery", "what is a dog?", 100)
396+
questions[0][1] = Question("cute and fuzzy", "what is a cat?", 200)
397+
questions[0][2] = Question("long and slithery", "what is a snake?", 300)
398+
questions[0][3] = Question("sometimes lives in a sewer", "what is a rat?", 400)
399+
questions[0][4] = Question("a reptile often mistaken for an amphibian",
400+
"what is a turtle?", 500)
421401

422-
## test data:
423402
catagories[1] = "Household Pets"
424-
questions[1][0] = Question("slobbery","what is a dog?", 100)
425-
questions[1][1] = Question("cute an fuzzy","what is a cat?", 200)
426-
questions[1][2] = Question("long and slithery","what is a snake?", 300)
427-
questions[1][3] = Question("sometimes lives in a sewer","what is a rat?", 400)
428-
questions[1][4] = Question("a reptile often mistaken for an amphibian","what is a turtle?", 500)
403+
questions[1][0] = Question("slobbery", "what is a dog?", 100)
404+
questions[1][1] = Question("cute an fuzzy", "what is a cat?", 200)
405+
questions[1][2] = Question("long and slithery", "what is a snake?", 300)
406+
questions[1][3] = Question("sometimes lives in a sewer", "what is a rat?", 400)
407+
questions[1][4] = Question("a reptile often mistaken for an amphibian",
408+
"what is a turtle?", 500)
429409

430-
## test data:
431410
catagories[2] = "Household Pets"
432-
questions[2][0] = Question("slobbery","what is a dog?", 100)
433-
questions[2][1] = Question("cute an fuzzy","what is a cat?", 200)
434-
questions[2][2] = Question("long and slithery","what is a snake?", 300)
435-
questions[2][3] = Question("sometimes lives in a sewer","what is a rat?", 400)
436-
questions[2][4] = Question("a reptile often mistaken for an amphibian","what is a turtle?", 500)
411+
questions[2][0] = Question("slobbery", "what is a dog?", 100)
412+
questions[2][1] = Question("cute an fuzzy", "what is a cat?", 200)
413+
questions[2][2] = Question("long and slithery", "what is a snake?", 300)
414+
questions[2][3] = Question("sometimes lives in a sewer", "what is a rat?", 400)
415+
questions[2][4] = Question("a reptile often mistaken for an amphibian",
416+
"what is a turtle?", 500)
437417

438-
## test data:
439418
catagories[3] = "Household Pets"
440-
questions[3][0] = Question("slobbery","what is a dog?", 100)
441-
questions[3][1] = Question("cute an fuzzy","what is a cat?", 200)
442-
questions[3][2] = Question("long and slithery","what is a snake?", 300)
443-
questions[3][3] = Question("sometimes lives in a sewer","what is a rat?", 400)
444-
questions[3][4] = Question("a reptile often mistaken for an amphibian","what is a turtle?", 500)
445-
## test data:
419+
questions[3][0] = Question("slobbery", "what is a dog?", 100)
420+
questions[3][1] = Question("cute an fuzzy", "what is a cat?", 200)
421+
questions[3][2] = Question("long and slithery", "what is a snake?", 300)
422+
questions[3][3] = Question("sometimes lives in a sewer", "what is a rat?", 400)
423+
questions[3][4] = Question("a reptile often mistaken for an amphibian",
424+
"what is a turtle?", 500)
446425
catagories[4] = "Household Pets"
447-
questions[4][0] = Question("slobbery","what is a dog?", 100)
448-
questions[4][1] = Question("cute an fuzzy","what is a cat?", 200)
449-
questions[4][2] = Question("long and slithery","what is a snake?", 300)
450-
questions[4][3] = Question("sometimes lives in a sewer","what is a rat?", 400)
451-
questions[4][4] = Question("a reptile often mistaken for an amphibian","what is a turtle?", 500)
426+
questions[4][0] = Question("slobbery", "what is a dog?", 100)
427+
questions[4][1] = Question("cute an fuzzy", "what is a cat?", 200)
428+
questions[4][2] = Question("long and slithery", "what is a snake?", 300)
429+
questions[4][3] = Question("sometimes lives in a sewer", "what is a rat?", 400)
430+
questions[4][4] = Question("a reptile often mistaken for an amphibian",
431+
"what is a turtle?", 500)
452432

453-
## test data:
454433
catagories[5] = "Household Pets"
455-
questions[5][0] = Question("slobbery","what is a dog?", 100)
456-
questions[5][1] = Question("cute an fuzzy","what is a cat?", 200)
457-
questions[5][2] = Question("long and slithery","what is a snake?", 300)
458-
questions[5][3] = Question("sometimes lives in a sewer","what is a rat?", 400)
459-
questions[5][4] = Question("a reptile often mistaken for an amphibian","what is a turtle?", 500)
460-
461-
462-
#set a few as answered
434+
questions[5][0] = Question("slobbery", "what is a dog?", 100)
435+
questions[5][1] = Question("cute an fuzzy", "what is a cat?", 200)
436+
questions[5][2] = Question("long and slithery", "what is a snake?", 300)
437+
questions[5][3] = Question("sometimes lives in a sewer", "what is a rat?", 400)
438+
questions[5][4] = Question("a reptile often mistaken for an amphibian",
439+
"what is a turtle?", 500)
440+
441+
# set a few as answered
463442
questions[3][3].answered = True
464443
questions[2][4].answered = True
465444

466-
467445
app = wx.App(0)
468446
game = Game(catagories, questions)
469447
frame = MainFrame(None, game)

RadioBox.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,31 @@ def __init__(self, title = "Micro App"):
1111

1212
FileMenu = wx.Menu()
1313

14-
item = wx.MenuItem(FileMenu, wx.ID_ANY, "&Quit")
15-
FileMenu.AppendItem(item)
14+
item = wx.MenuItem(FileMenu, wx.ID_EXIT, "&Quit")
15+
FileMenu.Append(item)
1616
self.Bind(wx.EVT_MENU, self.OnQuit, item)
1717

1818
MenuBar.Append(FileMenu, "&File")
1919
self.SetMenuBar(MenuBar)
2020

2121

2222
RB = wx.RadioBox(self,
23-
-1,
24-
"Hedges",
25-
wx.DefaultPosition,
26-
wx.DefaultSize,
27-
["ABOUT", "AROUND", "ABOVE", "POSITIVE", "BELOW",
28-
"VICINITY", "GENERALLY", "CLOSE", "NOT", "SOMEWHAT", "VERY", "EXTREMELY",
29-
"SLIGHTLY", "AFTER", "BEFORE"],
30-
2,
31-
wx.RA_SPECIFY_COLS)
32-
33-
wx.EVT_CLOSE(self,self.OnQuit)
23+
label="Hedges",
24+
choices = ["ABOUT", "AROUND", "ABOVE", "POSITIVE",
25+
"BELOW", "VICINITY", "GENERALLY", "CLOSE",
26+
"NOT", "SOMEWHAT", "VERY", "EXTREMELY",
27+
"SLIGHTLY", "AFTER", "BEFORE"],
28+
majorDimension=2,
29+
style=wx.RA_SPECIFY_COLS)
30+
31+
self.Bind(wx.EVT_CLOSE, self.OnQuit)
3432

3533
self.Fit()
3634

3735
def OnQuit(self,Event):
3836
self.Destroy()
3937

40-
app = wx.PySimpleApp(0)
38+
app = wx.App(False)
4139
frame = DemoFrame()
4240
frame.Show()
4341
app.MainLoop()

0 commit comments

Comments
 (0)