关于wxPython显示图形的问题
先贴代码import wx
class SketchWindow(wx.Window):
def __init__(self, parent, ID):
wx.Window.__init__(self, parent, ID)
self.SetBackgroundColour("Black")
self.color = "red"
self.thickness = 1
self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)
self.lines = []
self.curLine = []
self.pos = (0, 0)
self.reInitBuffer = True
self.Bind(wx.EVT_MOTION, self.test) #1
#self.test(1) #2
def InitBuffer(self):
size = self.GetClientSize() #3
#self.buffer = wx.EmptyBitmap(max(792, size.width), max(566, size.height))
self.buffer = wx.EmptyBitmap(size.width, size.height)
print self.buffer,size.width, size.height #4
if self.buffer.Ok():
dc = wx.BufferedDC(None, self.buffer)
dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
dc.Clear()
self.reInitBuffer = False
def drawMotion(self, dc):
dc.SetPen(self.pen)
newPos = (500,200)
self.pos = (200,500)
coords = self.pos + newPos
dc.DrawLine(*coords)
def test(self,evt):
self.OnIdle()
def OnIdle(self):
if self.reInitBuffer:
self.InitBuffer() #5
self.Refresh(False)
dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)
self.drawMotion(dc)
print "OnIdle"
class SketchFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "Sketch Frame",
size=(800,600))
self.sketch = SketchWindow(self, -1)
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = SketchFrame(None)
frame.Show(True)
app.MainLoop()
问题:
当我调用#1注释#2的时候,程序能正常显示(黑色背景上有一条红线),
当我调用#2注释#1的时候,程序不能正常显示,
区别事#3取得的size不一样,InitBuffer() 是在#5地方被调用的,请高手解释下原因以及解决不用事件驱动能产生图形的办法,谢谢
该问题已经解决,出现一个新的问题,画图只能在window里实现,在grid的每个cell怎么写进去呢?
grid.SetCellValue(row, col,dc.DrawLine(*coords))不能实现,如何实现呢?