Some random test apps that I've been playing with

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@42803 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn 2006-10-31 00:59:32 +00:00
parent 5c9d9745fe
commit 6ed100b4a1
8 changed files with 1077 additions and 0 deletions

View File

@ -0,0 +1,85 @@
import wx
import os
print "PID:", os.getpid()
count = 0
class ResultsFrame(wx.Frame):
def __init__(self, parent, bmp):
global count
count += 1
wx.Frame.__init__(self, parent, title=str(count), size=(100,100))
p = wx.Panel(self)
sb = wx.StaticBitmap(p, -1, bmp, (20,20))
self.Show()
class TestPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
b = wx.Button(self, -1, "Test", (50,50))
self.Bind(wx.EVT_BUTTON, self.OnButton, b)
def RunTest(self, bdc, bg, fg):
# draw to the buffered dc
bdc.SetBackground(wx.Brush(bg))
bdc.Clear()
bdc.SetPen(wx.Pen(fg, 2))
bdc.DrawLine(0,0, 30,30)
bdc.DrawLine(0,30, 30,0)
# now bilt it to a bitmap
bmp = wx.EmptyBitmap(30,30)
mdc = wx.MemoryDC()
mdc.SelectObject(bmp)
mdc.Blit(0,0, 30,30, bdc, 0,0)
del mdc
# display the results
ResultsFrame(self, bmp)
def OnButton(self, evt):
# 1. test a buffered dc not using a buffer bitmap
bdc = wx.BufferedDC(wx.ClientDC(self), self.GetSize())
self.RunTest(bdc, "yellow", "red")
del bdc
# 2. now do one that does have a buffer bitmap
buf = wx.EmptyBitmap(100,100)
bdc = wx.BufferedDC(wx.ClientDC(self), buf)
self.RunTest(bdc, "red", "blue")
del bdc
# 3. now one without a real DC
buf = wx.EmptyBitmap(100,100)
bdc = wx.BufferedDC(None, buf)
self.RunTest(bdc, "purple", "yellow")
del bdc
# 4. finally test a real unbuffered DC
dc = wx.ClientDC(self)
self.RunTest(dc, "red", "white")
# blit from the last buffer back to the main window
buf.SetMaskColour("yellow")
mdc = wx.MemoryDC()
mdc.SelectObject(buf)
dc.Blit(100,100,30,30, mdc, 0,0, useMask=True)
app = wx.App(0)
frm = wx.Frame(None)
pnl = TestPanel(frm)
frm.Show()
app.MainLoop()

View File

@ -0,0 +1,371 @@
import wx, unittest
from wx.lib.combotreebox import ComboTreeBox, IterableTreeCtrl
class ComboTreeBoxTest(unittest.TestCase):
def setUp(self):
self.comboBoxEventReceived = False
frame = wx.Frame(None)
self.comboBox = ComboTreeBox(frame, platform=platform)
self.tree = self.comboBox._popupFrame.GetTree()
def onComboBox(self, event):
self.comboBoxEventReceived = True
def testComboBoxIsEmptyByDefault(self):
self.assertEqual(0, self.comboBox.GetCount())
def testAddingOneItem(self):
self.comboBox.Append('Item 1')
self.assertEqual(1, self.comboBox.GetCount())
def testAddingTwoItems(self):
self.comboBox.Append('Item 1')
self.comboBox.Append('Item 2')
self.assertEqual(2, self.comboBox.GetCount())
def testAddingTwoParentAndChild(self):
item1 = self.comboBox.Append('Item 1')
self.comboBox.Append('Item 2', item1)
self.assertEqual(2, self.comboBox.GetCount())
def testSelectingAnItemPutsItInTheComboBox(self):
self.comboBox.Append('Item 1')
self.comboBox.Bind(wx.EVT_COMBOBOX, self.onComboBox)
self.comboBox.NotifyItemSelected('Item 1')
self.failUnless(self.comboBoxEventReceived)
def testClear(self):
self.comboBox.Append('Item 1')
self.comboBox.Clear()
self.assertEqual(0, self.comboBox.GetCount())
def testDelete(self):
self.comboBox.Append('Item 1')
self.comboBox.Delete(self.tree.GetFirstItem())
self.assertEqual(0, self.comboBox.GetCount())
def testGetSelection_NoItems(self):
self.failIf(self.comboBox.GetSelection().IsOk())
def testGetSelection_NoSelection(self):
self.comboBox.Append('Item 1')
self.failIf(self.comboBox.GetSelection().IsOk())
def testGetSelection_WithSelection(self):
item1 = self.comboBox.Append('Item 1')
self.comboBox.SetValue('Item 1')
self.assertEqual(item1, self.comboBox.GetSelection())
def testGetSelection_EquallyNamedNodes_SelectedInTree(self):
item1 = self.comboBox.Append('Item')
item2 = self.comboBox.Append('Item')
self.tree.SelectItem(item2)
self.assertEqual(self.tree.GetSelection(), self.comboBox.GetSelection())
def testGetSelection_EquallyNamedNodes_TypedInTextBox(self):
item1 = self.comboBox.Append('Item')
item2 = self.comboBox.Append('Item')
self.comboBox.SetValue('Item')
self.assertEqual(item1, self.comboBox.GetSelection())
def testFindString_NotPresent(self):
self.comboBox.Append('Item 1')
self.failIf(self.comboBox.FindString('Item 2').IsOk())
def testFindString_Present(self):
self.comboBox.Append('Item 1')
self.assertEqual(self.tree.GetFirstItem(),
self.comboBox.FindString('Item 1'))
def testFindString_Child(self):
parent = self.comboBox.Append('Parent')
child = self.comboBox.Append('Child', parent=parent)
self.assertEqual(child, self.comboBox.FindString('Child'))
def testGetString_NotPresent(self):
self.assertEqual('', self.comboBox.GetString(self.tree.GetFirstItem()))
def testGetString_Present(self):
self.comboBox.Append('Item 1')
self.assertEqual('Item 1',
self.comboBox.GetString(self.tree.GetFirstItem()))
def testGetStringSelection_NotPresent(self):
self.assertEqual('', self.comboBox.GetStringSelection())
def testGetStringSelection_Present(self):
self.comboBox.SetValue('Item 1')
self.assertEqual('Item 1', self.comboBox.GetStringSelection())
def testInsertAsFirstItem(self):
self.comboBox.Insert('Item 1')
self.assertEqual('Item 1',
self.comboBox.GetString(self.tree.GetFirstItem()))
def testInsertAsFirstItemBeforeExistingItem(self):
item1 = self.comboBox.Append('Item 1')
item2 = self.comboBox.Insert('Item 2')
self.assertEqual(item2, self.tree.GetFirstItem())
def testInsertAsFirstChildBeforeExistingChild(self):
parent = self.comboBox.Append('parent')
child1 = self.comboBox.Append('child 1', parent)
child2 = self.comboBox.Insert('child 2', parent=parent)
self.assertEqual(child2, self.tree.GetFirstChild(parent)[0])
def testSelect(self):
item1 = self.comboBox.Append('Item 1')
self.comboBox.Select(item1)
self.assertEqual('Item 1', self.comboBox.GetValue())
def testSetString(self):
item1 = self.comboBox.Append('Item 1')
self.comboBox.SetString(item1, 'Item 2')
self.assertEqual('Item 2', self.comboBox.GetString(item1))
def testSetStringSelection_ExistingString(self):
self.comboBox.Append('Hi')
self.comboBox.SetStringSelection('Hi')
self.assertEqual('Hi', self.comboBox.GetStringSelection())
def testSetStringSelection_NonExistingString(self):
self.comboBox.SetStringSelection('Hi')
self.assertEqual('', self.comboBox.GetStringSelection())
def testAppendWithClientData(self):
item1 = self.comboBox.Append('Item 1', clientData=[1,2,3])
self.assertEqual([1,2,3], self.comboBox.GetClientData(item1))
def testInsertWithClientData(self):
item1 = self.comboBox.Append('Item 1')
item2 = self.comboBox.Insert('Item 2', previous=item1,
clientData=[1,2,3])
self.assertEqual([1,2,3], self.comboBox.GetClientData(item2))
def testSetClientData(self):
item1 = self.comboBox.Append('Item 1')
self.comboBox.SetClientData(item1, [1,2,3])
self.assertEqual([1,2,3], self.comboBox.GetClientData(item1))
def testFindClientData(self):
item1 = self.comboBox.Append('Item 1', clientData='A')
self.assertEqual(item1, self.comboBox.FindClientData('A'))
def testFindClientData_NoItems(self):
self.failIf(self.comboBox.FindClientData('A'))
def testFindClientData_NoSuchData(self):
item1 = self.comboBox.Append('Item 1', clientData='A')
self.failIf(self.comboBox.FindClientData('B'))
def testSetClientDataSelection(self):
item1 = self.comboBox.Append('Item 1', clientData='A')
self.comboBox.SetClientDataSelection('A')
self.assertEqual(item1, self.comboBox.GetSelection())
def testSetClientDataSelection_NoSuchData(self):
item1 = self.comboBox.Append('Item 1', clientData='A')
self.comboBox.SetClientDataSelection('B')
self.failIf(self.comboBox.GetSelection())
class SortedComboTreeBoxTest(unittest.TestCase):
def setUp(self):
frame = wx.Frame(None)
self.comboBox = ComboTreeBox(frame, style=wx.CB_SORT, platform=platform)
self.tree = self.comboBox._popupFrame.GetTree()
def testAppend(self):
itemB = self.comboBox.Append('B')
itemA = self.comboBox.Append('A')
self.assertEqual(itemA, self.tree.GetFirstItem())
def testInsert(self):
itemA = self.comboBox.Append('A')
itemB = self.comboBox.Insert('B')
self.assertEqual(itemA, self.tree.GetFirstItem())
def testAppend_Child(self):
itemA = self.comboBox.Append('A')
itemA2 = self.comboBox.Append('2', parent=itemA)
itemA1 = self.comboBox.Append('1', parent=itemA)
self.assertEqual(itemA1, self.tree.GetFirstChild(itemA)[0])
def testInsert_Child(self):
itemA = self.comboBox.Append('A')
itemA1 = self.comboBox.Append('1', parent=itemA)
itemA2 = self.comboBox.Insert('2', parent=itemA)
self.assertEqual(itemA1, self.tree.GetFirstChild(itemA)[0])
def testSetString(self):
itemB = self.comboBox.Append('B')
itemC = self.comboBox.Append('C')
self.comboBox.SetString(itemC, 'A')
self.assertEqual(itemC, self.tree.GetFirstItem())
class ReadOnlyComboTreeBoxTest(unittest.TestCase):
def setUp(self):
frame = wx.Frame(None)
self.comboBox = ComboTreeBox(frame, style=wx.CB_READONLY)
self.tree = self.comboBox._popupFrame.GetTree()
def testSetValue_ToNonExistingValue(self):
self.comboBox.SetValue('Ignored value')
self.assertEqual('', self.comboBox.GetValue())
def testSetValue_ToExistingValue(self):
self.comboBox.Append('This works')
self.comboBox.SetValue('This works')
self.assertEqual('This works', self.comboBox.GetValue())
class IterableTreeCtrlTest(unittest.TestCase):
def setUp(self):
self.frame = wx.Frame(None)
self.tree = IterableTreeCtrl(self.frame)
self.root = self.tree.AddRoot('root')
def testPreviousOfRootIsInvalid(self):
item = self.tree.GetPreviousItem(self.root)
self.failIf(item.IsOk())
def testPreviousOfChildOfRootIsRoot(self):
child = self.tree.AppendItem(self.root, 'child')
self.assertEqual(self.root, self.tree.GetPreviousItem(child))
def testPreviousOfSecondChildOfRootIsFirstChild(self):
child1 = self.tree.AppendItem(self.root, 'child1')
child2 = self.tree.AppendItem(self.root, 'child2')
self.assertEqual(child1, self.tree.GetPreviousItem(child2))
def testPreviousOfGrandChildIsChild(self):
child = self.tree.AppendItem(self.root, 'child')
grandchild = self.tree.AppendItem(child, 'grandchild')
self.assertEqual(child, self.tree.GetPreviousItem(grandchild))
def testPreviousOfSecondChildWhenFirstChildHasChildIsThatChild(self):
child1 = self.tree.AppendItem(self.root, 'child1')
grandchild = self.tree.AppendItem(child1, 'child of child1')
child2 = self.tree.AppendItem(self.root, 'child2')
self.assertEqual(grandchild, self.tree.GetPreviousItem(child2))
def testPreviousOfSecondChildWhenFirstChildHasGrandChildIsThatGrandChild(self):
child1 = self.tree.AppendItem(self.root, 'child1')
grandchild = self.tree.AppendItem(child1, 'child of child1')
greatgrandchild = self.tree.AppendItem(grandchild,
'grandchild of child1')
child2 = self.tree.AppendItem(self.root, 'child2')
self.assertEqual(greatgrandchild, self.tree.GetPreviousItem(child2))
def testNextOfRootIsInvalidWhenRootHasNoChildren(self):
item = self.tree.GetNextItem(self.root)
self.failIf(item.IsOk())
def testNextOfRootIsItsChildWhenRootHasOneChild(self):
child = self.tree.AppendItem(self.root, 'child')
self.assertEqual(child, self.tree.GetNextItem(self.root))
def testNextOfLastChildIsInvalid(self):
child = self.tree.AppendItem(self.root, 'child')
self.failIf(self.tree.GetNextItem(child).IsOk())
def testNextOfFirstChildIsSecondChild(self):
child1 = self.tree.AppendItem(self.root, 'child1')
child2 = self.tree.AppendItem(self.root, 'child2')
self.assertEqual(child2, self.tree.GetNextItem(child1))
def testNextOfGrandChildIsItsParentsSibling(self):
child1 = self.tree.AppendItem(self.root, 'child1')
grandchild = self.tree.AppendItem(child1, 'child of child1')
child2 = self.tree.AppendItem(self.root, 'child2')
self.assertEqual(child2, self.tree.GetNextItem(grandchild))
def testNextOfGreatGrandChildIsItsParentsSiblingRecursively(self):
child1 = self.tree.AppendItem(self.root, 'child1')
grandchild = self.tree.AppendItem(child1, 'child of child1')
greatgrandchild = self.tree.AppendItem(grandchild,
'grandchild of child1')
child2 = self.tree.AppendItem(self.root, 'child2')
self.assertEqual(child2, self.tree.GetNextItem(greatgrandchild))
def testNextOfGrandChildWhenItIsLastIsInvalid(self):
child = self.tree.AppendItem(self.root, 'child')
grandchild = self.tree.AppendItem(child, 'child of child')
self.failIf(self.tree.GetNextItem(grandchild).IsOk())
def testFirstItemIsRoot(self):
self.assertEqual(self.root, self.tree.GetFirstItem())
def testGetFirstItemWithoutRootIsInvalid(self):
tree = IterableTreeCtrl(self.frame)
self.failIf(tree.GetFirstItem().IsOk())
def testGetSelection_NoSelection(self):
self.tree.Unselect()
self.failIf(self.tree.GetSelection().IsOk())
def testGetSelection_RootItemSelected(self):
self.tree.SelectItem(self.tree.GetRootItem())
self.assertEqual(self.tree.GetRootItem(), self.tree.GetSelection())
def testGetSelection_OtherItem(self):
child = self.tree.AppendItem(self.root, 'child')
self.tree.SelectItem(child)
self.assertEqual(child, self.tree.GetSelection())
class IterableTreeCtrlWithHiddenRootTest(unittest.TestCase):
def setUp(self):
frame = wx.Frame(None)
self.tree = IterableTreeCtrl(frame, style=wx.TR_HIDE_ROOT)
self.root = self.tree.AddRoot('root')
def testPreviousOfChildOfRootIsInvalid(self):
child = self.tree.AppendItem(self.root, 'child')
self.failIf(self.tree.GetPreviousItem(child).IsOk())
def testNextOfGrandChildWhenItIsLastIsInvalid(self):
child = self.tree.AppendItem(self.root, 'child')
grandchild = self.tree.AppendItem(child, 'child of child')
self.failIf(self.tree.GetNextItem(grandchild).IsOk())
def testRootIsNotTheFirstItem(self):
self.failIf(self.tree.GetFirstItem().IsOk())
def testFirstChildOfRootIsTheFirstItem(self):
child = self.tree.AppendItem(self.root, 'child')
self.assertEqual(child, self.tree.GetFirstItem())
def testGetSelection_NoSelection(self):
self.tree.Unselect()
self.failIf(self.tree.GetSelection().IsOk())
def testGetSelection_RootItemSelected(self):
# Apparently, selecting a hidden root item crashes wxPython on
# Windows, so don't do that.
if '__WXMSW__' not in wx.PlatformInfo:
self.tree.SelectItem(self.tree.GetRootItem())
self.failIf(self.tree.GetSelection().IsOk())
def testGetSelection_OtherItem(self):
child = self.tree.AppendItem(self.root, 'child')
self.tree.SelectItem(child)
self.assertEqual(child, self.tree.GetSelection())
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
platform = sys.argv[1].upper()
del sys.argv[1]
else:
platform = None
app = wx.App(False)
unittest.main()

View File

@ -0,0 +1,244 @@
import wx
import delayedresult as dr
def testStruct():
ss=dr.Struct(a='a', b='b')
assert ss.a == 'a'
assert ss.b == 'b'
def testHandler():
def handler(b, d, a=None, c=None):
assert a=='a'
assert b=='b'
assert c=='c'
assert d==1
hh=dr.Handler(handler, 1, a='a')
hh('b', c='c')
def handler2(*args, **kwargs):
assert args[0] == 3
assert args[1] == 1
assert kwargs['a'] == 'a'
assert kwargs['b'] == 'b'
hh2 = dr.Handler(handler2, 1, a='a')
args = ()
hh3 = dr.Handler(hh2, b='b', *args)
hh3(3)
def testSender():
triplet = (1,'a',2.34)
b = dr.Struct(called=False, which=1)
assert not b.called
def consumer(result, a, b=None):
assert result.get() == 789 + b.which
assert result.getJobID() == 456
assert a == 'a'
b.called = True
handler = dr.Handler(consumer, 'a', **dict(b=b))
ss = dr.SenderNoWx( handler, jobID=456 )
ss.sendResult(789+1)
assert b.called
def testSendExcept():
def consumer(result):
try:
result.get()
raise RuntimeError('should have raised!')
except AssertionError:
pass
ss = dr.SenderNoWx( dr.Handler(consumer) )
ss.sendException( AssertionError('test') )
def testThread():
expect = dr.Struct(value=1)
def consumer(result):
assert result.getJobID() is None
assert result.get() == expect.value
expect.value += 2
ss = dr.SenderNoWx( dr.Handler(consumer) )
import time
def worker(sender=None):
sender.sendResult(1)
time.sleep(0.1)
sender.sendResult(3)
time.sleep(0.1)
sender.sendResult(5)
time.sleep(0.1)
return 7
tt = dr.Producer(ss, worker, senderArg='sender')
tt.start()
while expect.value < 7:
time.sleep(0.1)
print '.'
def testStartWorker():
print 'Doing worker thread with call-after'
import time
def handleButtonClick():
produce = [123, 456, 789, 012, 345, 678, 901]
expect = dr.Struct(idx=0)
def worker(a, b=None, sender=None):
assert a == 2
assert b == 'b'
for val in produce:
time.sleep(0.5)
sender.sendResult(val)
def consumer(result, b, a=None):
assert b == 1
assert a=='a'
result = result.get()
print 'got result', result
if expect.idx < len(produce):
assert result == produce[ expect.idx ]#, 'Expected %s, got %s' % (
else:
assert result is None
app.ExitMainLoop()
expect.idx += 1
dr.startWorker(consumer, worker, cargs=(1,), ckwargs={'a':'a'},
wargs=(2,), wkwargs={'b':'b'}, senderArg='sender')
app = wx.PySimpleApp()
frame = wx.Frame(None) # need this otherwise MainLoop() returns immediately
# pretend user has clicked:
import thread
thread.start_new_thread( wx.CallAfter, (handleButtonClick,))
app.MainLoop()
def testStartWorkerEvent():
print 'Doing same with events'
import time
produce = [123, 456, 789, 012, 345, 678, 901]
expect = dr.Struct(idx=0)
def worker(a, b=None, sender=None):
assert a == 2
assert b == 'b'
for val in produce:
time.sleep(0.5)
sender.sendResult(val)
def consumer(event):
assert event.a=='a'
result = event.result.get()
print 'got result', result
if expect.idx < len(produce):
assert result == produce[ expect.idx ]#, 'Expected %s, got %s' % (
else:
assert result is None
app.ExitMainLoop()
expect.idx += 1
def handleButtonClick():
dr.startWorker(frame, worker,
cargs=(eventClass,), ckwargs={'a':'a','resultAttr':'result'},
wargs=(2,), wkwargs={'b':'b'}, senderArg='sender')
app = wx.PySimpleApp()
frame = wx.Frame(None) # need this otherwise MainLoop() returns immediately
from wx.lib.newevent import NewEvent as wxNewEvent
eventClass, eventBinder = wxNewEvent()
frame.Bind(eventBinder, consumer)
# pretend user has clicked:
import thread
thread.start_new_thread( wx.CallAfter, (handleButtonClick,))
app.MainLoop()
def testAbort():
import threading
abort = dr.AbortEvent()
# create a wx app and a function that will cause
# app to close when abort occurs
app = wx.PySimpleApp()
frame = wx.Frame(None) # need this otherwise MainLoop() returns immediately
def exiter():
abort.wait()
# make sure any events have time to be processed before exit
wx.FutureCall(2000, app.ExitMainLoop)
threading.Thread(target=exiter).start()
# now do the delayed result computation:
def worker():
count = 0
while not abort(1):
print 'Result computation not done, not aborted'
return 'Result computed'
def consumer(dr): # never gets called but as example
print 'Got dr=', dr.get()
app.ExitMainLoop()
dr.startWorker(consumer, worker)
# pretend user doing other stuff
import time
time.sleep(5)
# pretend user aborts now:
print 'Setting abort event'
abort.set()
app.MainLoop()
def testPreProcChain():
# test when no chain
def handler(dr):
assert dr.getJobID() == 123
assert dr.get() == 321
pp=dr.PreProcessChain( handler )
pp( dr.DelayedResult(321, jobID=123) )
# test with chaining
def handlerPP(chainTrav, n, a=None):
print 'In handlerPP'
assert n==1
assert a=='a'
assert chainTrav.getJobID() == 321
res = chainTrav.get()
assert res == 135
print 'Done handlerPP'
def subStart1(handler):
pp=dr.PreProcessChain(handler)
pp.addSub(subEnd1, 1, b='b')
subStart2(pp.clone())
def subEnd1(chainTrav, aa, b=None):
print 'In subEnd1'
assert aa==1
assert b=='b'
assert chainTrav.getJobID() == 321
res = chainTrav.get()
assert res == 246, 'res=%s' % res
print 'Returning from subEnd1'
return res - 111
def subStart2(preProc):
preProc.addSub(subEnd2, 3, c='c')
ss = dr.SenderNoWx(preProc, jobID=321)
ss.sendResult(123)
def subEnd2(chainTrav, a, c=None):
print 'In subEnd2'
assert a==3
assert c=='c'
assert chainTrav.getJobID() == 321
res = chainTrav.get()
assert res == 123
print 'Returning from subEnd2'
return 123*2
subStart1( dr.Handler(handlerPP, 1, a='a') )
testStruct()
testHandler()
testSender()
testSendExcept()
testThread()
testStartWorker()
testStartWorkerEvent()
testAbort()
testPreProcChain()

View File

@ -0,0 +1,35 @@
import wx
##import os; print os.getpid(); raw_input("press enter...")
class TestPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, evt):
dc = wx.GCDC(wx.PaintDC(self))
#dc = wx.PaintDC(self)
r = wx.Rect(64, 25, 45, 18)
dc.SetPen(wx.Pen("black", 1))
dc.SetBrush(wx.Brush("red"))
dc.DrawRectangleRect(r)
dc.SetPen(wx.TRANSPARENT_PEN)
#dc.SetPen(wx.Pen("light blue", 1))
dc.SetBrush(wx.Brush("light blue"))
dc.DrawRectangle(r.x+1, r.y+1, r.width-2, r.height-2)
dc.SetPen(wx.Pen("black", 1))
dc.DrawLine(r.x+r.width+02, r.y,
r.x+r.width+15, r.y)
dc.DrawLine(r.x+r.width+02, r.y+r.height-1,
r.x+r.width+15, r.y+r.height-1)
app = wx.App(False)
frm = wx.Frame(None)
pnl = TestPanel(frm)
frm.Show()
app.MainLoop()

View File

@ -0,0 +1,81 @@
import wx
# This class is just an experiment to see how easy it would be to
# handle simulating transfer of ownership of object to a 'parent'
# object, and then automatically calling Destroy on those when the
# parent is destroyed. Conclusion: It's not too hard at all. Now,
# what should I do with it...
class DestroyWrapper(object):
def __init__(self):
import weakref
self.items = weakref.WeakValueDictionary()
def AddItem(self, obj):
self.items[len(self.items)+1] = obj
def __del__(self):
for item in self.items.values():
item.Destroy()
class MyEvtHandler(wx.EvtHandler):
instCount = 0
def __init__(self):
wx.EvtHandler.__init__(self)
MyEvtHandler.instCount += 1
self.cnt = MyEvtHandler.instCount
self.Bind(wx.EVT_CHECKBOX, self.OnCheckBox)
def __del__(self):
print "%02d: deleted" % self.cnt
def OnCheckBox(self, evt):
print "%02d: %s" % (self.cnt, evt.IsChecked())
evt.Skip()
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="wx.EvtHandler Test")
p = wx.Panel(self)
pushBtn = wx.Button(p, -1, "Push EvtHandler", (20,20))
popBtn = wx.Button(p, -1, "Pop EvtHandler", (20,60))
checkBox = wx.CheckBox(p, -1, "Test EvtHandler", (200, 25))
self.Bind(wx.EVT_BUTTON, self.OnPushBtn, pushBtn)
self.Bind(wx.EVT_BUTTON, self.OnPopBtn, popBtn)
## self.dw = DestroyWrapper()
def OnPushBtn(self, evt):
eh = MyEvtHandler()
self.PushEventHandler(eh)
## self.dw.AddItem(eh)
print "%02d: pushed" % eh.cnt
def OnPopBtn(self, evt):
eh = self.GetEventHandler()
if eh.this == self.this:
print "All already popped!"
else:
eh = self.PopEventHandler()
print "%02d: popped( %s )" % (eh.cnt, eh.__class__)
eh.Destroy()
app = wx.App(False)
frm = MyFrame()
frm.Show()
app.MainLoop()

101
wxPython/tests/test_gc.py Normal file
View File

@ -0,0 +1,101 @@
import wx
class TestPanel(wx.Panel):
def __init__(self, *args, **kw):
wx.Panel.__init__(self, *args, **kw)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, evt):
gc = wx.GraphicsContext.Create(wx.PaintDC(self))
pen = wx.Pen("navy", 2)
gc.SetPen(pen)
brush = wx.Brush((255,32,32,128))
gc.SetBrush(brush)
gc.PushState()
path = gc.CreatePath()
path.MoveToPoint(50, 50)
path.AddLineToPoint(25,25)
path.AddLineToPoint(50,25)
path.AddLineToPoint(50,50)
path.CloseSubpath()
gc.DrawPath(path)
gc.Scale(2,2)
gc.Translate(10,5)
gc.DrawPath(path)
gc.Translate(50,0)
gc.FillPath(path)
gc.Translate(0,5)
gc.StrokePath(path)
gc.Translate(0,5)
brush = wx.Brush((32,32,255,128))
gc.SetBrush(brush)
gc.FillPath(path)
gc.Translate(50,0)
gc.DrawPath(path)
gc.PopState()
points = [ (5.2, 5.9),
(50, 50),
(35, 50),
(25,40),
wx.Point2D(20.5,50.9),
wx.Point2D(5,25),
(5,6)
]
gc.Translate(0, 150)
gc.DrawLines(points)
gc.Translate(75, 0)
gc.StrokeLines(points)
begin = [ (0,0),
(0,10),
(0,20),
(0,30),
(0,40),
(0,50),
]
end = [ (50,0),
(50,10),
(50,20),
(50,30),
(50,40),
(50,50),
]
# in a floating point coordinate system the center of the
# pixel is actually at x+0.5, y+0.5, so with anti-aliasing
# turned on we'll get a crisper line by positioning our line
# segments at the 0.5 offset. For this test we'll just let
# the GC do the translation for us.
gc.Translate(0.5, 0.5)
pen = wx.Pen("purple", 1)
gc.SetPen(pen)
gc.Translate(75, 0)
gc.StrokeLineSegements(begin, end)
gc.Translate(75, 0)
gc.Scale(2,2)
gc.StrokeLineSegements(begin, end)
gc.DrawLines(points)
del path
app = wx.App(False)
frm = wx.Frame(None)
pnl = TestPanel(frm)
frm.Show()
app.MainLoop()

View File

@ -0,0 +1,30 @@
import wx
class TestPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.gauge = wx.Gauge(self, range=100, pos=(20,20), size=(100,-1))
self.Bind(wx.EVT_IDLE, self.OnIdle)
self.count = 1
self.skipNext = False
def OnIdle(self, evt):
if self.skipNext:
self.skipNext = False
return
self.skipNext = True
print "OnIdle:", self.count
#self.gauge.SetValue(self.count)
self.count += 1
if self.count >= 100:
self.count = 1
app = wx.App(False)
frm = wx.Frame(None)
pnl = TestPanel(frm)
frm.Show()
app.MainLoop()

View File

@ -0,0 +1,130 @@
import wx
import cStringIO
#import os; print os.getpid(); raw_input("Press enter...")
class Panel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, evt):
dc = wx.PaintDC(self)
dc.SetFont(self.GetFont())
r = wx.RendererNative.Get()
rect = wx.Rect(40,10, 95,r.GetHeaderButtonHeight(self))
#print rect
# simple helper to make calling DrawHeaderButton so many times a
# bit easier and less messy
def dhb(idx, rect, flags=0, sort=0, params=None):
dc.DrawText("%02d." % idx, rect.x-25, rect.y)
r.DrawHeaderButton(self, dc, rect, flags, sort, params)
rect.y += 30
dhb(1, rect)
dhb(2, rect, wx.CONTROL_SELECTED)
dhb(3, rect, wx.CONTROL_CURRENT)
dhb(4, rect, wx.CONTROL_SELECTED|wx.CONTROL_CURRENT)
dhb(5, rect, 0, wx.HDR_SORT_ICON_UP)
dhb(6, rect, 0, wx.HDR_SORT_ICON_DOWN)
dhb(7, rect, wx.CONTROL_SELECTED, wx.HDR_SORT_ICON_UP)
dhb(8, rect, wx.CONTROL_SELECTED, wx.HDR_SORT_ICON_DOWN)
rect.x = 180
rect.y = 10
hp = wx.HeaderButtonParams()
hp.m_labelText = "Hello"
dhb(9, rect, params=hp)
hp.m_labelAlignment = wx.ALIGN_CENTER
dhb(10, rect, params=hp)
hp.m_labelAlignment = wx.ALIGN_RIGHT
dhb(11, rect, params=hp)
hp.m_labelAlignment = wx.ALIGN_CENTER
dhb(12, rect, wx.CONTROL_SELECTED, 0, hp)
dhb(13, rect, wx.CONTROL_SELECTED, wx.HDR_SORT_ICON_UP, hp)
hp.m_labelText = "This label is too long"
dhb(14, rect, params=hp)
dhb(15, rect, wx.CONTROL_SELECTED, 0, hp)
dhb(16, rect, wx.CONTROL_SELECTED, wx.HDR_SORT_ICON_UP, hp)
rect.x = 320
rect.y = 10
hp = wx.HeaderButtonParams()
hp.m_labelBitmap = getBitmap()
dhb(17, rect, params=hp)
hp.m_labelAlignment = wx.ALIGN_CENTER
dhb(18, rect, params=hp)
hp.m_labelAlignment = wx.ALIGN_RIGHT
dhb(19, rect, params=hp)
hp.m_labelAlignment = wx.ALIGN_CENTER
dhb(20, rect, wx.CONTROL_SELECTED, 0, hp)
dhb(21, rect, wx.CONTROL_SELECTED, wx.HDR_SORT_ICON_UP, hp)
hp.m_labelText = "label"
hp.m_labelAlignment = wx.ALIGN_LEFT
dhb(22, rect, 0, 0, hp)
hp.m_labelAlignment = wx.ALIGN_CENTER
dhb(23, rect, 0, 0, hp)
hp.m_labelAlignment = wx.ALIGN_RIGHT
dhb(24, rect, 0, 0, hp)
rect.x = 460
rect.y = 10
hp.m_labelAlignment = wx.ALIGN_LEFT
dhb(25, rect, wx.CONTROL_SELECTED, wx.HDR_SORT_ICON_UP, hp)
hp.m_labelAlignment = wx.ALIGN_CENTER
dhb(26, rect, wx.CONTROL_SELECTED, wx.HDR_SORT_ICON_UP, hp)
hp.m_labelAlignment = wx.ALIGN_RIGHT
dhb(27, rect, wx.CONTROL_SELECTED, wx.HDR_SORT_ICON_UP, hp)
hp.m_labelText = "longer label"
hp.m_labelAlignment = wx.ALIGN_LEFT
dhb(28, rect, wx.CONTROL_SELECTED, wx.HDR_SORT_ICON_UP, hp)
hp.m_labelAlignment = wx.ALIGN_CENTER
dhb(29, rect, wx.CONTROL_SELECTED, wx.HDR_SORT_ICON_UP, hp)
hp.m_labelAlignment = wx.ALIGN_RIGHT
dhb(30, rect, wx.CONTROL_SELECTED, wx.HDR_SORT_ICON_UP, hp)
def getData():
return \
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
\x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\
\x00\x01\xceIDAT8\x8d\x95\x93=h\x14A\x14\xc7\x7f\xfbq\xc7m\xce\x0b\xe4\xa2F/\
~\x04\x9b\x03O\x0bE\x10\x04\x91\x10\x88X\xa4J\xe7&\x8a\x10A\xc4Bb\xe9uje\xc0\
\xd2\xd2\xca\x94V\xa9\xa3X\x88i\x14\x0b\x91\x14\xae$dO\x8d\x97#\xf1<n/;\xb3k\
!;\xd9\xbd\xdd\x14\xfea\x9a7\xef\xff{\x8f7\xf34M7\x88+\x90"d\x1f\xe9\x86\xa9\
\xf5\xc7\xcc~\xf3\x8d\x87W\xb9pbR\xc5\xb6\xfc5\x00\xbe}_%\x90"\xec\x87\x98q\
\xf3\xfc\xc2,\xf7\xef=\x00\xa0\xc36\x8d\xf0\x03#\x0c\x00p\x9asL?:\x9b\x82\
\x98q\xf3\x8c=\xcbr\xf3E\xa2E\xcf\xf7\x01\x90\x81`|\xae\x96\x82\xe8\x81\x14\
\xe1\xf4\x9d\te\xf6|_\x9dN\xaf\x8b\x0c\x04\x83\xc6a\xa6F\xef\xe2\xfd\xb0\x18\
\x9f\xabq\xf1\xfa\xa8\x9a\x95\x1eUjt\xbf&*\xcb@\x000\x94\xab`W\xeb\x9c?:I^\
\xb7\x0084r0{\x88Q\xbb\x91\x8a\xc6\x10v\xb5\xcep\xa1\xc2\xe7\xd6;~\xcbM\xacP\
&r\x12\x80\x82V\xe2\xda\xc9\xdb\x1c\x19\x18\xe3\xe5\xeac&\x8e\xcf0\\\xa8\xb0\
\xe4<gy\xfd\x159\xd3\xc0\xc2\xe2\xd7\xcf\xa6\xf2\xe8q@^\xb7h\xf56(\xe5\xca\
\xd8\xd5:\xb5\xf2%e\xdeO\x89\x0e\xdc\xb6\x83\xdbv\xf8\xb8\xf9\x06\xbbZO\x99}\
!\x91\xa1\xc8\x06\xec\xc85\xda\x9d.\xa5\xa2\x85\xdbvx\xf2\xfef"\xd1\xeb\xed\
\x02`h\x07\x92\x00\xdd0\xb5@\x8ap\xea\xd6e\xae\xcc\x1f\xa3\xb1\xd5T\xc9\xfd\
\x1a;3\xc8\xeb\xa7\x0e+\x8b\xae\xfa\xd6&\xfc\xfb\xe3qHP\xfeCgg\xefEtS\xc3*\
\x9a\xbc]Xg\xe9\xd9\'v\xa5\xa7\xee\xb4\xf82E\x90\xfc\xa9\xedT\xf5\x8d/-V\x16\
]\xa2\x82\x99\x80\x08\x92\xd9?\xd9\xdb\x98\x02\xfc\xaf\xfe\x02\xb1S\xd1)\xa5\
\x1a\xc2\x99\x00\x00\x00\x00IEND\xaeB`\x82'
def getBitmap():
return wx.BitmapFromImage(getImage())
def getImage():
stream = cStringIO.StringIO(getData())
return wx.ImageFromStream(stream)
app = wx.App(False)
frm = wx.Frame(None, title="DrawHeaderButton Test", size=(580,300))
pnl = Panel(frm)
frm.Show()
app.MainLoop()