Start New Discussion within our Software Development Community

Using a wxPython GUI program to find the position of the mouse when clicked within the frame/window. Also sets the color of the frame and changes the cursor to a pencil. The title bar of the frame displays the coordinates of the mouse position.

# get the position of the mouse when clicked, wxPython
# tested with Python24 and wxPython25     vegaseat     16oct2005

import wx

class MyFrame(wx.Frame):
    """create a color frame, inherits from wx.Frame"""
    def __init__(self, parent):
        # -1 is the default ID
        wx.Frame.__init__(self, parent, -1, "Click for mouse position", size=(400,300),
                         style=wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
        # hook some mouse events
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
    def OnLeftDown(self, event):
        """left mouse button is pressed"""
        pt = event.GetPosition()  # position tuple
        print pt
        self.SetTitle('LeftMouse = ' + str(pt))
    def OnRightDown(self, event):
        """right mouse button is pressed"""
        pt = event.GetPosition()
        print pt
        self.SetTitle('RightMouse = ' + str(pt))

app = wx.PySimpleApp()
frame = MyFrame(None)
The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.