[wxPython-users] Snapping windows

Mike Rooney mxr at qvii.com
Thu Mar 29 07:07:14 PDT 2007


Eli Golovinsky wrote:
> I'm trying to make a frame snap to the edges of the screen like 
> Winamp's and Skype's windows do.
>
> I've done it using Win32 API (see attached example), but I would like 
> to do it with clean wx code if that's possible. I thought I could use 
> the EVT_MOVING event, but I can't seem to find a way to do that.
>
> In Win32 you can just change the message details, i.e. the position to 
> which the window is moving, to the edge of the screen. I can find no 
> way   to do that with EVT_MOVING. There's no Veto and SetPosition on 
> the event doesn't do anything. Am I missing something?
>   
Maybe I am missing something but why are you limiting yourself to 
methods on the event, when you have access to the frame? For example I 
have implemented a very basic snapping Frame which snaps to the left. 
Does this not accomplish what you want?

import wx

class SnapMixin:
    def __init__(self, snap=20):
        self.snap = snap
        self.Bind(wx.EVT_MOVE, self.OnMove)
       
    def OnMove(self, event):
        if event.Position[0] < self.snap:
            self.SetPosition((0, -1))
           
        event.Skip()

class SnapFrame(wx.Frame, SnapMixin):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)
        SnapMixin.__init__(self)
        panel = wx.Panel(self)
        panel.SetSizer(wx.BoxSizer(wx.VERTICAL))
        text = wx.StaticText(panel, -1, "Drag the frame near the edges 
of your screen...")
        panel.GetSizer().Add(text, flag=wx.ALL|wx.ALIGN_CENTER, border=10)

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = SnapFrame(None)
    frame.Show()
    app.MainLoop()




More information about the wxpython-users mailing list