新手求助!wxpython 画20个长方体,却画出了40个
用wxpython画20个长方体,可是却产生了40个,求各位大侠帮忙!我是python新手
wxpython python
import wx
import random
size = 20
BarData = []
class TopPanel(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self,parent)
self.SetBackgroundColour("White")
self.Bind(wx.EVT_PAINT, self.OnPaint)
#parameter of box
def GenerateBarData(self,xPos):
width = 900/(size*2)
gap = 900/(size*4)
xPos += (width + gap)
height = random.randrange(1,500)
barParameters=[xPos, 50, width, height]
BarData.append(barParameters)
return barParameters
def OnPaint(self, event):
dc = wx.PaintDC(self)
self.paintBox(dc)
def paintBox(self, dc):
xPos = 50
dc.SetBrush(wx.Brush("green"))
for num in range(0,size):
barParameters = self.GenerateBarData(xPos)
xPos = barParameters[0]
#print boxParameters
print "generate %d bar" %(num)
for box in range(0,size):
dc.DrawRectangle(*BarData[box])
print "round------%d" %(box)
print BarData
class TopFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Sorting Comparision", size = (900,700))
TopPanel(self)
if __name__ == '__main__':
app = wx.App()
frame = TopFrame()
frame.Show()
app.MainLoop()
[解决办法]
每个函数的开头与结尾,每个循环的开头与结尾都用print。
你会找到错在了哪里。
还可以把所有的size都换成20,然后一个再一个换回去
[解决办法]
OnPaint是可能多次调用,譬如你在拖拉窗口时。所以你最好在其他时机进行必要的数据更新,重绘时应该已经都是固化的数据...
[解决办法]
import wx
import random
size = 20
BarData = []
class TopPanel(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self,parent)
self.SetBackgroundColour("White")
self.Bind(wx.EVT_PAINT, self.OnPaint)
xPos = 50
for num in range(0, size):
barParameters = self.GenerateBarData(xPos)
xPos = barParameters[0]
#parameter of box
def GenerateBarData(self,xPos):
width = 900/(size*2)
gap = 900/(size*4)
xPos += (width + gap)
height = random.randrange(1,500)
barParameters=[xPos, 50, width, height]
BarData.append(barParameters)
return barParameters
def OnPaint(self, event):
dc = wx.PaintDC(self)
self.paintBox(dc)
def paintBox(self, dc):
dc.SetBrush(wx.Brush("green"))
for box in range(0,size):
dc.DrawRectangle(*BarData[box])
print "round------%d" %(box)
print BarData
class TopFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Sorting Comparision", size = (900,700))
TopPanel(self)
if __name__ == '__main__':
app = wx.App()
frame = TopFrame()
frame.Show()
app.MainLoop()