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.
wxPython 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)
self.SetBackgroundColour('Goldenrod')
self.SetCursor(wx.StockCursor(wx.CURSOR_PENCIL))
# 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)
frame.Show(True)
app.MainLoop()
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.