1999-11-13 05:52:53 +00:00
|
|
|
|
|
|
|
from wxPython.wx import *
|
|
|
|
|
|
|
|
import thread
|
|
|
|
import time
|
|
|
|
from whrandom import random
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2001-12-19 21:25:11 +00:00
|
|
|
wxEVT_UPDATE_BARGRAPH = wxNewEventType()
|
1999-11-13 05:52:53 +00:00
|
|
|
|
|
|
|
def EVT_UPDATE_BARGRAPH(win, func):
|
|
|
|
win.Connect(-1, -1, wxEVT_UPDATE_BARGRAPH, func)
|
|
|
|
|
|
|
|
|
|
|
|
class UpdateBarEvent(wxPyEvent):
|
|
|
|
def __init__(self, barNum, value):
|
|
|
|
wxPyEvent.__init__(self)
|
|
|
|
self.SetEventType(wxEVT_UPDATE_BARGRAPH)
|
|
|
|
self.barNum = barNum
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class CalcBarThread:
|
|
|
|
def __init__(self, win, barNum, val):
|
|
|
|
self.win = win
|
|
|
|
self.barNum = barNum
|
|
|
|
self.val = val
|
|
|
|
|
|
|
|
def Start(self):
|
|
|
|
self.keepGoing = self.running = true
|
|
|
|
thread.start_new_thread(self.Run, ())
|
|
|
|
|
|
|
|
def Stop(self):
|
|
|
|
self.keepGoing = false
|
|
|
|
|
|
|
|
def IsRunning(self):
|
|
|
|
return self.running
|
|
|
|
|
|
|
|
def Run(self):
|
|
|
|
while self.keepGoing:
|
|
|
|
evt = UpdateBarEvent(self.barNum, int(self.val))
|
|
|
|
wxPostEvent(self.win, evt)
|
2001-12-19 21:25:11 +00:00
|
|
|
#del evt
|
1999-11-13 05:52:53 +00:00
|
|
|
|
|
|
|
sleeptime = (random() * 2) + 0.5
|
2001-12-19 21:25:11 +00:00
|
|
|
time.sleep(sleeptime/4)
|
1999-11-13 05:52:53 +00:00
|
|
|
|
|
|
|
sleeptime = sleeptime * 5
|
|
|
|
if int(random() * 2):
|
|
|
|
self.val = self.val + sleeptime
|
|
|
|
else:
|
|
|
|
self.val = self.val - sleeptime
|
|
|
|
|
|
|
|
if self.val < 0: self.val = 0
|
|
|
|
if self.val > 300: self.val = 300
|
|
|
|
|
|
|
|
self.running = false
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
class GraphWindow(wxWindow):
|
|
|
|
def __init__(self, parent, labels):
|
|
|
|
wxWindow.__init__(self, parent, -1)
|
|
|
|
|
|
|
|
self.values = []
|
|
|
|
for label in labels:
|
|
|
|
self.values.append((label, 0))
|
|
|
|
|
2001-11-20 06:04:42 +00:00
|
|
|
font = wxFont(12, wxSWISS, wxNORMAL, wxBOLD)
|
|
|
|
self.SetFont(font)
|
1999-11-13 05:52:53 +00:00
|
|
|
|
1999-11-13 07:33:29 +00:00
|
|
|
self.colors = [ wxRED, wxGREEN, wxBLUE, wxCYAN,
|
2001-11-20 06:04:42 +00:00
|
|
|
"Yellow", "Navy" ]
|
1999-11-13 05:52:53 +00:00
|
|
|
|
2000-07-15 19:51:35 +00:00
|
|
|
EVT_ERASE_BACKGROUND(self, self.OnEraseBackground)
|
|
|
|
EVT_PAINT(self, self.OnPaint)
|
|
|
|
|
1999-11-13 05:52:53 +00:00
|
|
|
|
|
|
|
def SetValue(self, index, value):
|
|
|
|
assert index < len(self.values)
|
|
|
|
cur = self.values[index]
|
|
|
|
self.values[index:index+1] = [(cur[0], value)]
|
|
|
|
|
|
|
|
|
|
|
|
def SetFont(self, font):
|
|
|
|
wxWindow.SetFont(self, font)
|
|
|
|
wmax = hmax = 0
|
|
|
|
for label, val in self.values:
|
|
|
|
w,h = self.GetTextExtent(label)
|
|
|
|
if w > wmax: wmax = w
|
|
|
|
if h > hmax: hmax = h
|
|
|
|
self.linePos = wmax + 10
|
|
|
|
self.barHeight = hmax
|
|
|
|
|
|
|
|
|
2001-11-20 06:04:42 +00:00
|
|
|
def GetBestHeight(self):
|
|
|
|
return 2 * (self.barHeight + 1) * len(self.values)
|
|
|
|
|
|
|
|
|
1999-11-13 07:33:29 +00:00
|
|
|
def Draw(self, dc, size):
|
2001-11-20 06:04:42 +00:00
|
|
|
dc.SetFont(self.GetFont())
|
1999-11-13 05:52:53 +00:00
|
|
|
dc.SetTextForeground(wxBLUE)
|
1999-11-13 07:33:29 +00:00
|
|
|
dc.SetBackground(wxBrush(self.GetBackgroundColour()))
|
|
|
|
dc.Clear()
|
1999-11-13 05:52:53 +00:00
|
|
|
dc.SetPen(wxPen(wxBLACK, 3, wxSOLID))
|
|
|
|
dc.DrawLine(self.linePos, 0, self.linePos, size.height-10)
|
|
|
|
|
|
|
|
bh = ypos = self.barHeight
|
|
|
|
for x in range(len(self.values)):
|
|
|
|
label, val = self.values[x]
|
|
|
|
dc.DrawText(label, 5, ypos)
|
|
|
|
|
|
|
|
if val:
|
|
|
|
color = self.colors[ x % len(self.colors) ]
|
|
|
|
dc.SetPen(wxPen(color))
|
|
|
|
dc.SetBrush(wxBrush(color))
|
|
|
|
dc.DrawRectangle(self.linePos+3, ypos, val, bh)
|
|
|
|
|
|
|
|
ypos = ypos + 2*bh
|
|
|
|
if ypos > size.height-10:
|
|
|
|
break
|
|
|
|
|
1999-11-13 07:33:29 +00:00
|
|
|
|
|
|
|
def OnPaint(self, evt):
|
|
|
|
size = self.GetSize()
|
|
|
|
bmp = wxEmptyBitmap(size.width, size.height)
|
|
|
|
dc = wxMemoryDC()
|
|
|
|
dc.SelectObject(bmp)
|
|
|
|
self.Draw(dc, size)
|
|
|
|
|
|
|
|
wdc = wxPaintDC(self)
|
|
|
|
wdc.BeginDrawing()
|
|
|
|
wdc.Blit(0,0, size.width, size.height, dc, 0,0)
|
|
|
|
wdc.EndDrawing()
|
|
|
|
|
1999-12-29 20:07:27 +00:00
|
|
|
dc.SelectObject(wxNullBitmap)
|
|
|
|
|
1999-11-13 07:33:29 +00:00
|
|
|
|
|
|
|
def OnEraseBackground(self, evt):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
1999-11-13 05:52:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class TestFrame(wxFrame):
|
|
|
|
def __init__(self, parent, log):
|
|
|
|
wxFrame.__init__(self, parent, -1, "Thread Test", size=(450,300))
|
|
|
|
self.log = log
|
|
|
|
|
|
|
|
#self.CenterOnParent()
|
|
|
|
|
|
|
|
panel = wxPanel(self, -1)
|
|
|
|
panel.SetFont(wxFont(10, wxSWISS, wxNORMAL, wxBOLD))
|
|
|
|
wxStaticText(panel, -1,
|
|
|
|
"This demo shows multiple threads interacting with this\n"
|
2001-12-19 21:25:11 +00:00
|
|
|
"window by sending events to it, one thread for each bar.",
|
|
|
|
wxPoint(5,5))
|
1999-11-13 05:52:53 +00:00
|
|
|
panel.Fit()
|
|
|
|
|
1999-11-13 07:33:29 +00:00
|
|
|
self.graph = GraphWindow(self, ['Zero', 'One', 'Two', 'Three', 'Four',
|
|
|
|
'Five', 'Six', 'Seven'])
|
2001-11-20 06:19:02 +00:00
|
|
|
self.graph.SetSize((450, self.graph.GetBestHeight()))
|
1999-11-13 05:52:53 +00:00
|
|
|
|
|
|
|
sizer = wxBoxSizer(wxVERTICAL)
|
|
|
|
sizer.Add(panel, 0, wxEXPAND)
|
|
|
|
sizer.Add(self.graph, 1, wxEXPAND)
|
|
|
|
|
|
|
|
self.SetSizer(sizer)
|
|
|
|
self.SetAutoLayout(true)
|
2001-11-20 06:04:42 +00:00
|
|
|
sizer.Fit(self)
|
1999-11-13 05:52:53 +00:00
|
|
|
|
|
|
|
EVT_UPDATE_BARGRAPH(self, self.OnUpdate)
|
|
|
|
self.threads = []
|
1999-11-13 07:33:29 +00:00
|
|
|
self.threads.append(CalcBarThread(self, 0, 50))
|
|
|
|
self.threads.append(CalcBarThread(self, 1, 75))
|
|
|
|
self.threads.append(CalcBarThread(self, 2, 100))
|
|
|
|
self.threads.append(CalcBarThread(self, 3, 150))
|
|
|
|
self.threads.append(CalcBarThread(self, 4, 225))
|
|
|
|
self.threads.append(CalcBarThread(self, 5, 300))
|
|
|
|
self.threads.append(CalcBarThread(self, 6, 250))
|
|
|
|
self.threads.append(CalcBarThread(self, 7, 175))
|
1999-11-13 05:52:53 +00:00
|
|
|
|
|
|
|
for t in self.threads:
|
|
|
|
t.Start()
|
|
|
|
|
2000-07-15 19:51:35 +00:00
|
|
|
EVT_CLOSE(self, self.OnCloseWindow)
|
1999-11-13 05:52:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def OnUpdate(self, evt):
|
|
|
|
self.graph.SetValue(evt.barNum, evt.value)
|
1999-11-13 07:33:29 +00:00
|
|
|
self.graph.Refresh(false)
|
1999-11-13 05:52:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def OnCloseWindow(self, evt):
|
|
|
|
busy = wxBusyInfo("One moment please, waiting for threads to die...")
|
2001-11-20 06:19:02 +00:00
|
|
|
wxYield()
|
1999-11-13 05:52:53 +00:00
|
|
|
for t in self.threads:
|
|
|
|
t.Stop()
|
|
|
|
running = 1
|
|
|
|
while running:
|
|
|
|
running = 0
|
|
|
|
for t in self.threads:
|
|
|
|
running = running + t.IsRunning()
|
|
|
|
time.sleep(0.1)
|
|
|
|
self.Destroy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
def runTest(frame, nb, log):
|
|
|
|
win = TestFrame(frame, log)
|
|
|
|
frame.otherWin = win
|
|
|
|
win.Show(true)
|
|
|
|
return None
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
overview = """\
|
|
|
|
The main issue with multi-threaded GUI programming is the thread safty
|
|
|
|
of the GUI itself. On most platforms the GUI is not thread safe and
|
|
|
|
so any cross platform GUI Toolkit and applications written with it
|
|
|
|
need to take that into account.
|
|
|
|
|
|
|
|
The solution is to only allow interaction with the GUI from a single
|
|
|
|
thread, but this often severly limits what can be done in an
|
|
|
|
application and makes it difficult to use additional threads at all.
|
|
|
|
|
|
|
|
Since wxPython already makes extensive use of event handlers, it is a
|
|
|
|
logical extension to allow events to be sent to GUI objects from
|
|
|
|
alternate threads. A function called wxPostEvent allows you to do
|
|
|
|
this. It accepts an event and an event handler (window) and instead
|
|
|
|
of sending the event immediately in the current context like
|
|
|
|
ProcessEvent does, it processes it later from the context of the GUI
|
|
|
|
thread.
|
|
|
|
|
|
|
|
"""
|