Seite 1 von 1

wxPython: Ein Panel mit der Maus verschieben lassen

Verfasst: Montag 16. April 2007, 14:12
von gerold

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
"""
Demonstriert, wie man ein Panel mit der Maus verschieben lassen kann.
Dafür habe ich das wxPython-Beispiel "ShapedWindow" abgewandelt.
"""

import wx

wx.SetDefaultPyEncoding("iso-8859-15")


class MyFrame(wx.Frame):
    
    def __init__(
        self, parent = None, id = -1, title = "Example", size = wx.Size(550, 420)
    ):
        
        wx.Frame.__init__(self, parent, id, title, size = size)
        
        self.mp_delta = (0,0)
        
        panel = wx.Panel(self)
        
        movable_panel = wx.Panel(panel, size = (100, 100))
        movable_panel.SetBackgroundColour("green")
        self.movable_panel = movable_panel
        
        movable_panel.Bind(wx.EVT_LEFT_DOWN, self.on_mp_left_down)
        movable_panel.Bind(wx.EVT_LEFT_UP, self.on_mp_left_up)
        movable_panel.Bind(wx.EVT_MOTION, self.on_mp_motion)
    
    
    def on_mp_left_down(self, evt):
        self.movable_panel.CaptureMouse()
        x, y = self.movable_panel.ClientToScreen(evt.GetPosition())
        originx, originy = self.movable_panel.GetPosition()
        dx = x - originx
        dy = y - originy
        self.mp_delta = ((dx, dy))


    def on_mp_left_up(self, evt):
        if self.movable_panel.HasCapture():
            self.movable_panel.ReleaseMouse()


    def on_mp_motion(self, evt):
        if evt.Dragging() and evt.LeftIsDown():
            x, y = self.movable_panel.ClientToScreen(evt.GetPosition())
            fp = (x - self.mp_delta[0], y - self.mp_delta[1])
            self.movable_panel.Move(fp)


def main():
    """Testing"""
    
    app = wx.PySimpleApp()
    f = MyFrame()
    f.Center()
    f.Show()
    app.MainLoop()


if __name__ == "__main__":
    main()
lg
Gerold
:-)