为什么我的wxpython程序在我关闭时会崩溃?
我被难住了——希望另一双眼睛能够看到我在这里错过了什么 我已经创建了一个wxpython程序(Windows8.1上Python2.7.5下的wxpython2.8.12.1)。该程序的一部分是我创建的自定义控件,用于为用户窗体生成透明箭头形状的按钮 当我在表单中包含这些按钮时,它们的工作和外观完全符合预期;但是当我关闭顶层窗口时,程序崩溃了。我在Visual Studio中打开了它,它告诉我有一个损坏的堆,并说问题出在ntdll.dll中。确切的错误信息如下所示: python.exe中0x00007FFC5FE912E0(ntdll.dll)处未处理的异常:0xC0000374:堆已损坏(参数:0x00007FFC5FECD40) 我尝试过禁用部分代码,但我似乎无法跟踪问题,也无法在Eclipse调试器中遍历它(这似乎发生在我的任何类获得wx.EVT_CLOSE事件之前)。如果程序中包含“我的箭头”中的任何一个,则崩溃会持续发生,否则不会发生。我怀疑这与我使用DCs来绘制控件有关(我以前很少使用它们),但这只不过是一个骗局,我真的不知道我做错了什么 任何洞察都将被感激——我希望这是我看不到的非常明显的东西 控件的代码如下(有点长,抱歉):为什么我的wxpython程序在我关闭时会崩溃?,python,wxpython,wxwidgets,Python,Wxpython,Wxwidgets,我被难住了——希望另一双眼睛能够看到我在这里错过了什么 我已经创建了一个wxpython程序(Windows8.1上Python2.7.5下的wxpython2.8.12.1)。该程序的一部分是我创建的自定义控件,用于为用户窗体生成透明箭头形状的按钮 当我在表单中包含这些按钮时,它们的工作和外观完全符合预期;但是当我关闭顶层窗口时,程序崩溃了。我在Visual Studio中打开了它,它告诉我有一个损坏的堆,并说问题出在ntdll.dll中。确切的错误信息如下所示: python.exe中0x0
#ArrowButton.py
#
#类模块,用于箭头按钮控制。
#
#创建一个箭头形状的按钮,指向上(默认为AB_样式\向上箭头)或向下(AB_样式\向下箭头)。
#
#此对象在单击时生成wx.EVT_按钮事件。
#
导入wx
AB_样式_向上箭头=256
AB_样式_向下箭头=512
AB_最小_宽度=20
AB_最小_高度=12
AB_活动=0
AB_禁用=1
AB_=2
AB_按下_外侧=3
根3=1.7320508075
类箭头按钮(wx.Control):
def uuu init uuuu(self,parent,id=wx.id\u ANY,pos=wx.DefaultPosition,size=wx.DefaultSize,
style=AB\u style\u UPARROW,validator=wx.DefaultValidator,name=“ArrowButton”):
self.\uuuuu style\uuuuu=style&(AB\u style\u向上箭头+AB\u style\u向下箭头)
如果不是(自选式和AB式向上箭头或自选式和AB式向下箭头):
raise VALUERROR(“{}uuuuuuuu init\uuuuuu必须指定AB_STYLE\u向上箭头或AB_STYLE\u向下箭头作为其标志之一。”。格式(self.\uuuuuuu class\uuuuuuuu名称)
如果大小==wx.DefaultSize:
calcsize=(最小宽度,最小高度)
其他:
calcsize=大小
控件。初始化(self,parent=parent,id=id,pos=pos,size=calcsize,
style=(style&~(AB_style_向上箭头+AB_style_向下箭头)| wx.TRANSPARENT_WINDOW | wx.NO_BORDER),
验证器=验证器,名称=名称)
自我设置大小(calcsize)
self.SetSizeHints(*calcsize)
self.SetMinSize(calcsize)
self.\uuuu activebitmap\uuuu=None
self.\uuuu greyedbitmap\uuuuu=无
self.\uuuu按位图\uuuu=无
自身敏感性=无
自。uuu模式uuu=AB_激活
self.Bind(wx.EVT_LEFT_DOWN,self.u OnMouse_uu)
self.Bind(wx.EVT\u LEFT\u DCLICK,self.\u OnMouse\u)
self.Bind(wx.EVT\u大小,self.\u大小)
self.Bind(wx.EVT_擦除_背景,lambda事件:无)
self.Bind(wx.EVT_PAINT,self._OnPaint___)
self.uuu generateBitmap_uuuu()
定义生成映射(自):
size=self.GetClientSizeTuple()
Color=wx.SystemSettings.getcolor#这是缩短以下文本的帮助程序。
bcFace=颜色(wx.SYS\u Color\u 3DFACE)
bcLEdge=Color(wx.SYS\u Color\u 3DLIGHT)
bcDEdge=颜色(wx.SYS\u Color\u 3DSHADOW)
self.\uuuu-activebitmap\uuuu=wx.EmptyBitmap(大小[0],大小[1])
self.\uuuu greyedbitmap\uuuu=wx.EmptyBitmap(大小[0],大小[1])
self.\uuuu按位图\uuuu=wx.EmptyBitmap(大小[0],大小[1])
self.\uuuu灵敏度\uuuu=wx.EmptyBitmap(大小[0],大小[1])
memdc=wx.MemoryDC()
maskbitmap=wx.EmptyBitmap(大小[0],大小[1],深度=1)
memdc.SelectObject(maskbitmap)
memdc.立根接地(带白色刷)
memdc.Clear()
self.\uuuuu生成角度(memdc,(0,0,大小[0],大小[1]),wx.BLACK,wx.BLACK,wx.BLACK)
memdc.SelectObject(wx.NullBitmap)
mask=wx.mask(maskbitmap)
memdc.选择对象(自身灵敏度)
self.\uuuuu生成角度(memdc,(0,0,大小[0],大小[1]),wx.RED,wx.RED,无)
memdc.SelectObject(self.\uu activebitmap\uuuuuuuuuuuuuuuuuuuuu)
self.\uuuu生成器角度(memdc,(0,0,大小[0],大小[1]),bcFace,bcLEdge,bcDEdge)
memdc.SelectObject(self.\u greyedbitmap\u)
self.\uuuu GenerateTriangle\uuuuu(memdc,(0,0,大小[0],大小[1]),bcFace,bcLEdge,None)
memdc.SelectObject(self.\按位图)
self.\uuuu GenerateTriangle\uuuuu(memdc,(0,0,大小[0],大小[1]),bcFace,bcDEdge,bcLEdge)
memdc.SelectObject(wx.NullBitmap)
self.\uuuu活动位图\uuuuu.SetMask(掩码)
self.\uuuu greyedbitmap\uuuuu.SetMask(mask)
自。\按位图\设置掩码(掩码)
def u_GenerateTriangle u_;(自、直流、其中、面、明亮、阴影):
如果where[2]>((2*where[3])/ROOT3):#框比需要的宽。
top=其中[1]
底部=其中[1]+其中[3]-1
left=(其中[0]+(其中[2]/2))-int(其中[3]/ROOT3)
右=(其中[0]+(其中[2]/2))+int(其中[3]/ROOT3)
否则:#这个盒子比它需要的要高。
左=其中[0]
右=其中[0]+其中[2]-1
top=(其中[1]+(其中[3]/2))-int((其中[2]*ROOT3)/4)
底部=(其中[1]+(其中[3]/2))+int((其中[2]*ROOT3)/4)
直流立根刷(wx刷(面,wx实心))
dc.SetPen(wx.Pen(明亮,1,wx.SOLID))
如果为自。\ uuuuu样式\ uuuuu和AB\ uu样式\ uuu向上箭头:
dc.DrawPolygon(点=[((左+右)/2,顶部),(右,底部),
# ArrowButton.py
#
# Class module for ArrowButton control.
#
# Creates a button shaped like an arrow, pointing up (AB_STYLE_UPARROW, default) or down (AB_STYLE_DOWNARROW).
#
# This object generates wx.EVT_BUTTON events when clicked.
#
import wx
AB_STYLE_UPARROW = 256
AB_STYLE_DOWNARROW = 512
AB_MIN_WIDTH = 20
AB_MIN_HEIGHT = 12
AB_ACTIVE = 0
AB_DISABLED = 1
AB_PRESSED = 2
AB_PRESSED_OUTSIDE = 3
ROOT3 = 1.7320508075
class ArrowButton(wx.Control):
def __init__(self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.DefaultSize,
style = AB_STYLE_UPARROW, validator = wx.DefaultValidator, name = "ArrowButton"):
self.__style__ = style & (AB_STYLE_UPARROW + AB_STYLE_DOWNARROW)
if not (self.__style__ & AB_STYLE_UPARROW or self.__style__ & AB_STYLE_DOWNARROW):
raise ValueError("{} __init__ must specify AB_STYLE_UPARROW or AB_STYLE_DOWNARROW as one of its flags.".format(self.__class__.__name__))
if size == wx.DefaultSize:
calcsize = (AB_MIN_WIDTH, AB_MIN_HEIGHT)
else:
calcsize = size
wx.Control.__init__(self, parent = parent, id = id, pos = pos, size = calcsize,
style = (style & ~(AB_STYLE_UPARROW + AB_STYLE_DOWNARROW) | wx.TRANSPARENT_WINDOW | wx.NO_BORDER),
validator = validator, name = name)
self.SetSize(calcsize)
self.SetSizeHints(*calcsize)
self.SetMinSize(calcsize)
self.__activebitmap__ = None
self.__greyedbitmap__ = None
self.__pressedbitmap__ = None
self.__sensitivity__ = None
self.__mode__ = AB_ACTIVE
self.Bind(wx.EVT_LEFT_DOWN, self.__OnMouse__)
self.Bind(wx.EVT_LEFT_DCLICK, self.__OnMouse__)
self.Bind(wx.EVT_SIZE, self.__OnSize__)
self.Bind(wx.EVT_ERASE_BACKGROUND, lambda event: None)
self.Bind(wx.EVT_PAINT, self.__OnPaint__)
self.__GenerateBitmaps__()
def __GenerateBitmaps__(self):
size = self.GetClientSizeTuple()
Color = wx.SystemSettings.GetColour # This is a helper to shorten the text below.
bcFace = Color(wx.SYS_COLOUR_3DFACE)
bcLEdge = Color(wx.SYS_COLOUR_3DLIGHT)
bcDEdge = Color(wx.SYS_COLOUR_3DSHADOW)
self.__activebitmap__ = wx.EmptyBitmap(size[0], size[1])
self.__greyedbitmap__ = wx.EmptyBitmap(size[0], size[1])
self.__pressedbitmap__ = wx.EmptyBitmap(size[0], size[1])
self.__sensitivity__ = wx.EmptyBitmap(size[0], size[1])
memdc = wx.MemoryDC()
maskbitmap = wx.EmptyBitmap(size[0], size[1], depth = 1)
memdc.SelectObject(maskbitmap)
memdc.SetBackground(wx.WHITE_BRUSH)
memdc.Clear()
self.__GenerateTriangle__(memdc, (0, 0, size[0], size[1]), wx.BLACK, wx.BLACK, wx.BLACK)
memdc.SelectObject(wx.NullBitmap)
mask = wx.Mask(maskbitmap)
memdc.SelectObject(self.__sensitivity__)
self.__GenerateTriangle__(memdc, (0, 0, size[0], size[1]), wx.RED, wx.RED, None)
memdc.SelectObject(self.__activebitmap__)
self.__GenerateTriangle__(memdc, (0, 0, size[0], size[1]), bcFace, bcLEdge, bcDEdge)
memdc.SelectObject(self.__greyedbitmap__)
self.__GenerateTriangle__(memdc, (0, 0, size[0], size[1]), bcFace, bcLEdge, None)
memdc.SelectObject(self.__pressedbitmap__)
self.__GenerateTriangle__(memdc, (0, 0, size[0], size[1]), bcFace, bcDEdge, bcLEdge)
memdc.SelectObject(wx.NullBitmap)
self.__activebitmap__.SetMask(mask)
self.__greyedbitmap__.SetMask(mask)
self.__pressedbitmap__.SetMask(mask)
def __GenerateTriangle__(self, dc, where, face, bright, shadow):
if where[2] > ((2 * where[3]) / ROOT3): # The box is wider than it needs to be.
top = where[1]
bottom = where[1] + where[3] - 1
left = (where[0] + (where[2] / 2)) - int(where[3] / ROOT3)
right = (where[0] + (where[2] / 2)) + int(where[3] / ROOT3)
else: # The box is taller than it needs to be.
left = where[0]
right = where[0] + where[2] - 1
top = (where[1] + (where[3] / 2)) - int((where[2] * ROOT3) / 4)
bottom = (where[1] + (where[3] / 2)) + int((where[2] * ROOT3) / 4)
dc.SetBrush(wx.Brush(face, wx.SOLID))
dc.SetPen(wx.Pen(bright, 1, wx.SOLID))
if self.__style__ & AB_STYLE_UPARROW:
dc.DrawPolygon(points = [((left + right) / 2, top), (right, bottom), (left, bottom)])
if shadow:
dc.SetPen(wx.Pen(shadow, 1, wx.SOLID))
dc.DrawLine(left, bottom, right, bottom)
dc.DrawLine(right, bottom, (left + right) / 2, top)
else:
dc.DrawPolygon(points = [((left + right) / 2, bottom), (right, top), (left, top)])
if shadow:
dc.SetPen(wx.Pen(shadow, 1, wx.SOLID))
dc.DrawLine((left + right) / 2, bottom, right, top)
def DoGetBestSize(self):
return ((AB_MIN_WIDTH, AB_MIN_HEIGHT))
def Disable(self):
if self.__mode__ == AB_PRESSED or self.__mode__ == AB_PRESSED_OUTSIDE:
self.Unbind(wx.EVT_MOTION, self.__OnMouse__)
self.Unbind(wx.EVT_LEFT_UP, self.__OnMouse__)
elif self.__mode__ == AB_ACTIVE:
self.Unbind(wx.EVT_LEFT_DOWN)
self.Unbind(wx.EVT_LEFT_DCLICK)
self.__mode__ = AB_DISABLED
self.Refresh()
return wx.Control.Disable(self)
def Enable(self, enable = True):
if not enable:
return self.Disable()
self.Bind(wx.EVT_LEFT_DOWN, self.__OnMouse__)
self.Bind(wx.EVT_LEFT_DCLICK, self.__OnMouse__)
self.__mode__ = AB_ACTIVE
self.Refresh()
return wx.Control.Enable(self, enable = enable)
def __OnMouse__(self, event):
if self.__mode__ == AB_DISABLED:
event.Skip()
return
where = event.GetPositionTuple()
memdc = wx.MemoryDC()
memdc.SelectObject(self.__sensitivity__)
inbounds = (memdc.GetPixel(*where).Get()[0] <> 0)
if self.__mode__ == AB_PRESSED:
if event.LeftUp(): # The button was released.
self.__mode__ = AB_ACTIVE
self.Refresh()
self.Unbind(wx.EVT_MOTION)
self.Unbind(wx.EVT_LEFT_UP)
self.Unbind(wx.EVT_MOUSE_CAPTURE_LOST)
self.Bind(wx.EVT_LEFT_DOWN, self.__OnMouse__)
self.Bind(wx.EVT_LEFT_DCLICK, self.__OnMouse__)
self.ReleaseMouse()
newevent = wx.CommandEvent(commandType = wx.EVT_BUTTON.evtType[0], winid = self.GetId())
newevent.SetEventObject(self)
wx.PostEvent(self, newevent)
return
elif event.Dragging():
if not inbounds:
self.__mode__ = AB_PRESSED_OUTSIDE
self.Refresh()
elif self.__mode__ == AB_PRESSED_OUTSIDE:
if event.LeftUp():
self.__mode__ = AB_ACTIVE
self.Refresh()
self.Unbind(wx.EVT_MOTION)
self.Unbind(wx.EVT_LEFT_UP)
self.Unbind(wx.EVT_MOUSE_CAPTURE_LOST)
self.Bind(wx.EVT_LEFT_DOWN, self.__OnMouse__)
self.Bind(wx.EVT_LEFT_DCLICK, self.__OnMouse__)
self.ReleaseMouse()
elif event.Dragging():
if inbounds:
self.__mode__ = AB_PRESSED
self.Refresh()
else: # Assume the button is in ACTIVE
if event.LeftDown() | event.LeftDClick():
if inbounds:
self.__mode__ = AB_PRESSED
self.Refresh()
self.Unbind(wx.EVT_LEFT_DOWN)
self.Unbind(wx.EVT_LEFT_DCLICK)
self.Bind(wx.EVT_LEFT_UP, self.__OnMouse__)
self.Bind(wx.EVT_MOTION, self.__OnMouse__)
self.Bind(wx.EVT_MOUSE_CAPTURE_LOST, self.__OnMouseCaptureLost__)
self.CaptureMouse()
return
event.Skip()
def __OnMouseCaptureLost__(self, event):
if self.__mode__ == AB_PRESSED or self.__mode__ == AB_PRESSED_OUTSIDE:
self.__mode__ = AB_ACTIVE
self.Unbind(wx.EVT_MOTION)
self.Unbind(wx.EVT_LEFT_UP)
self.Unbind(wx.EVT_MOUSE_CAPTURE_LOST)
self.Bind(wx.EVT_LEFT_DOWN, self.__OnMouse__)
self.Bind(wx.EVT_LEFT_DCLICK, self.__OnMouse__)
self.Refresh()
def __OnSize__(self, event):
size = event.GetSize()
if size[0] < AB_MIN_WIDTH:
size[0] = AB_MIN_WIDTH
if size[1] < AB_MIN_HEIGHT:
size[1] = AB_MIN_HEIGHT
event.Skip()
self.__GenerateBitmaps__()
def __OnPaint__(self, event):
dc = wx.PaintDC(self)
if self.__mode__ == AB_DISABLED:
dc.DrawBitmap(self.__greyedbitmap__, 0, 0, True)
elif self.__mode__ == AB_PRESSED:
dc.DrawBitmap(self.__pressedbitmap__, 0, 0, True)
else: # The button is ACTIVE or PRESSED_OUTSIDE.
dc.DrawBitmap(self.__activebitmap__, 0, 0, True)
def __GenerateBitmaps__(self):
size = self.GetClientSizeTuple()
Color = wx.SystemSettings.GetColour # This is a helper to shorten the text below.
bcFace = Color(wx.SYS_COLOUR_3DFACE)
bcLEdge = Color(wx.SYS_COLOUR_3DLIGHT)
bcDEdge = Color(wx.SYS_COLOUR_3DSHADOW)
self.__activebitmap__ = wx.EmptyBitmap(size[0], size[1])
self.__greyedbitmap__ = wx.EmptyBitmap(size[0], size[1])
self.__pressedbitmap__ = wx.EmptyBitmap(size[0], size[1])
self.__sensitivity__ = wx.EmptyBitmap(size[0], size[1])
memdc = wx.MemoryDC()
maskbitmap = wx.EmptyBitmap(size[0], size[1], depth = 1)
memdc.SelectObject(maskbitmap)
memdc.SetBackground(wx.WHITE_BRUSH)
memdc.Clear()
self.__GenerateTriangle__(memdc, (0, 0, size[0], size[1]), wx.BLACK, wx.BLACK, wx.BLACK)
memdc.SelectObject(wx.NullBitmap)
abmmask = wx.Mask(maskbitmap) # <---
gbmmask = wx.Mask(maskbitmap) # <--- New code to make multiple masks.
pbmmask = wx.Mask(maskbitmap) # <---
memdc.SelectObject(self.__sensitivity__)
self.__GenerateTriangle__(memdc, (0, 0, size[0], size[1]), wx.RED, wx.RED, None)
memdc.SelectObject(self.__activebitmap__)
self.__GenerateTriangle__(memdc, (0, 0, size[0], size[1]), bcFace, bcLEdge, bcDEdge)
memdc.SelectObject(self.__greyedbitmap__)
self.__GenerateTriangle__(memdc, (0, 0, size[0], size[1]), bcFace, bcLEdge, None)
memdc.SelectObject(self.__pressedbitmap__)
self.__GenerateTriangle__(memdc, (0, 0, size[0], size[1]), bcFace, bcDEdge, bcLEdge)
memdc.SelectObject(wx.NullBitmap)
self.__activebitmap__.SetMask(abmmask) # <---
self.__greyedbitmap__.SetMask(gbmmask) # <--- Code edited to apply new masks.
self.__pressedbitmap__.SetMask(pbmmask) # <---