Hi Guys,
I am trying to draw on PlotCanvas using wx.DC module. I have several problems with drawing on WXBufferedPaintDC, for some reason my DC text disappears after windows resizing and it looks that I draw my text in the wrong place. Any help appreciated. Here is my code:
import wx
import wx.lib.plot as plot
USE_BUFFERED_DC = True
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, id=-1, title="Graph display", size=(1000,1000))
main_panel = wx.Panel(self, -1)
#data for the plot
data = [[1,5],[2,4],[3,8],[4,3],[5,2]]
line = plot.PolyLine(data, colour='red', width=1)
#plot window & bindings
self.plot_window = plot.PlotCanvas(main_panel, -1)
gc = plot.PlotGraphics([line], 'y-axis', 'x-axis', 'Title')
self.plot_window.Draw(gc)
self.plot_window.canvas.Bind(wx.EVT_LEFT_DOWN, self.mouseleftdown)
self.plot_window.canvas.Bind(wx.EVT_PAINT, self.Paint)
self.buffer = wx.EmptyBitmap(500, 500) # draw to this
dc = wx.BufferedDC(wx.PaintDC(self.plot_window.canvas), self.buffer)
vbox_sizer = wx.BoxSizer(wx.VERTICAL)
vbox_sizer.Add(self.plot_window, 1, wx.EXPAND|wx.ALIGN_LEFT)
main_panel.SetAutoLayout(True)
main_panel.SetSizer(vbox_sizer)
main_panel.Layout()
def mouseleftdown(self, event):
x, y = event.GetPosition()
print "Left Mouse Down at Point:", x, y
dc = wx.BufferedDC(wx.PaintDC(self.plot_window.canvas), self.buffer)
dc.Clear() # black window otherwise
dc.DrawText("SomeText", x, y)
def Paint(self, event):
if USE_BUFFERED_DC:
dc = wx.BufferedPaintDC(self, self.buffer)
else:
dc = wx.PaintDC(self)
dc.DrawBitmap(self.buffer, 100, 100)
self.UpdateDrawing()
def UpdateDrawing(self):
dc = wx.MemoryDC()
dc.SelectObject(self.buffer)
del dc # need to get rid of the MemoryDC before Update() is called.
self.Refresh()
self.Update()
app = wx.PySimpleApp()
MyFrame().Show()
app.MainLoop()