Skip to main content
 首页 » 编程设计

python之拖动面板时如何清除wxpython框架内容

2025年05月04日14虾米姐

我有 3 个面板,我想对它们进行拖动。 问题是,当我拖拽一个时,会发生这种情况: http://img41.yfrog.com/img41/9043/soundlog.png http://img41.yfrog.com/img41/9043/soundlog.png

当面板不再存在时,如何刷新框架以显示其颜色?

这是我必须进行拖动的代码:

def onMouseMove(self, event): 
    (self.pointWidth, self.pointHeight) = event.GetPosition() 
    (self.width, self.height) = self.GetSizeTuple() 
    if (self.pointWidth>100 and self.pointWidth<(self.width-100) and self.pointHeight < 15) or self.parent.dragging: 
        self.SetCursor(wx.StockCursor(wx.CURSOR_SIZING)) 
 
        """implement dragging""" 
        if not event.Dragging(): 
            self.w = 0 
            self.h = 0 
            return 
        self.CaptureMouse() 
        if self.w == 0 and self.h == 0: 
            (self.w, self.h) = event.GetPosition() 
        else: 
            (posw, posh) = event.GetPosition() 
            displacement = self.h - posh 
            self.SetPosition( self.GetPosition() - (0, displacement)) 
    else: 
        self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW)) 
 
def onDraggingDown(self, event): 
    if self.pointWidth>100 and self.pointWidth<(self.width-100) and self.pointHeight < 15: 
        self.parent.dragging = 1 
        self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW)) 
        self.SetBackgroundColour('BLUE') 
        self.parent.SetTransparent(220) 
        self.Refresh() 
 
def onDraggingUp(self, event): 
    self.parent.dragging = 0 
    self.parent.SetTransparent(255) 
    self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW)) 

这是此事件的绑定(bind):

self.Bind(wx.EVT_MOTION, self.onMouseMove) 
self.Bind(wx.EVT_LEFT_DOWN, self.onDraggingDown) 
self.Bind(wx.EVT_LEFT_UP, self.onDraggingUp) 

有了这个,如果我点击面板的顶部,然后向下或向上移动,面板位置就会改变(我拖动面板)到鼠标的位置。

请您参考如下方法:

要在 self 每次重新定位时刷新父级,您可以添加

self.parent.Refresh() 

在 def onMouseMove 方法中对 self.SetPosition 的现有调用之后。现在,您仅在 def onDraggingDown 方法中刷新框架,即第一次单击并按住鼠标左键时,而不是每次按住该按钮时移动鼠标(即“拖动” Action 本身)。

我无法下载您的代码用于测试目的,因为您选择将代码上传到相当“垃圾邮件特殊”的网站——该网站不断用广告轰炸我,我没有明确的方法来进行下载,偶尔会提示它不支持我的机器(我使用 Mac 和 Google Chrome,某些地方的网站坚持使用 IE 或 Firefox 的 Windows...),等等。我相信您可以找到其他网站,更多比那个更有用,对于那些试图帮助你的人来说!-)