User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 422,560 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,694 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums

wxCanvas-Get current coordinates

Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,460
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 10
Solved Threads: 176
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: wxCanvas-Get current coordinates

  #2  
May 27th, 2007
This will give the mouse position within the whole scrolled canvas ...
  1. """
  2. # -*- coding: cp1250 -*-
  3. """
  4. # get coordinates of whole scrolled canvas
  5.  
  6. import wx
  7.  
  8. class Okno:
  9. def __init__(self,parent=None,id=wx.ID_ANY,title="Graf"):
  10. self.okno=wx.MDIChildFrame(parent,title="Graf",id=-1)
  11. self.okno.Maximize()
  12. self.okno.SetAutoLayout(True)
  13. self.okno.SetBackgroundColour("#FCFCFE")
  14. self.sizer = wx.FlexGridSizer(2,2,0,0)
  15. self.canvas = wx.ScrolledWindow(self.okno, id=wx.ID_ANY)
  16. self.canvas.EnableScrolling(True, True)
  17. self.canvas.SetScrollbars(20, 20, 1000/20, 1000/20)
  18. self.sizer.Add(self.canvas, 1, wx.EXPAND)
  19. self.sizer.AddGrowableRow(0, 1)
  20. self.sizer.AddGrowableCol(0, 1)
  21. self.okno.SetSizer(self.sizer)
  22. self.canvas.Bind(wx.EVT_PAINT, self.OnPaint)
  23.  
  24. # hook left mouse button event
  25. self.canvas.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
  26.  
  27. def OnPaint(self, event):
  28. pass
  29.  
  30. def OnLeftDown(self, event):
  31. """left mouse button is pressed"""
  32. device_pt = event.GetPosition() # tuple (x, y)
  33. # need to add current scrollbar value logic_pt to device_pt
  34. # if you want position relative to whole canvas
  35. logic_pt = self.canvas.CalcUnscrolledPosition(0, 0)
  36. #print device_pt, logic_pt
  37. adjusted_pt = (device_pt[0]+logic_pt[0], device_pt[1]+logic_pt[1])
  38. self.okno.SetTitle('LeftMouse = ' + str(adjusted_pt))
  39.  
  40. if __name__ == "__main__":
  41. okno = wx.App(0)
  42. parent=wx.MDIParentFrame(None,size=wx.Size(500,500))
  43. o=Okno(parent)
  44. parent.Show()
  45. okno.MainLoop()
Last edited by vegaseat : May 27th, 2007 at 12:45 pm. Reason: spelling
May 'the Google' be with you!
Reply With Quote  
All times are GMT -4. The time now is 1:19 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC