RSS Forums RSS
Please support our Python advertiser: Programming Forums
Views: 1127 | Replies: 2
Reply
Join Date: Mar 2006
Posts: 56
Reputation: Blujacker is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Blujacker Blujacker is offline Offline
Junior Poster in Training

Get position on Canvas

  #1  
Apr 29th, 2007
Hi

I am trying to get the mouse position on my Canvas
# -*- coding: cp1250 -*-
import wx
class Okno:
    def __init__(self,parent):
        self.okno=wx.MDIChildFrame(parent,title=u"Náhled situace",id=-1)
        self.okno.Maximize()
        self.okno.SetAutoLayout(True)
        self.okno.SetBackgroundColour("#FCFCFE")
        self.sizer = wx.FlexGridSizer(2,2,0,0)

        # Add canvas
        self.platno = wx.ScrolledWindow(self.okno, id=wx.ID_ANY)
        self.platno.EnableScrolling(True, True)
        self.sirka = 10000
        self.vyska = 10000
        self.platno.SetScrollbars(20, 20, self.sirka/20, self.vyska/20)
        self.sizer.Add(self.platno, 1, wx.EXPAND)

        self.sizer.Add((0,0))
        self.sizer.Add((0,0))
        self.sizer.Add((0,0))
        self.sizer.AddGrowableRow(0, 1)
        self.sizer.AddGrowableCol(0, 1)
        self.platno.Bind(wx.EVT_MOTION, self.Pohyb)
        self.okno.SetSizer(self.sizer)
    def Pohyb(self, akce):
        print akce.GetPosition()#doesnt work correctly
if __name__ == "__main__":
    okno = wx.App(0)
    parent=wx.MDIParentFrame(None,size=wx.Size(500,500))
    Okno(parent)
    parent.Show()
    okno.MainLoop()
if i move the mouse, i will see coordinates of mouse not in canvas but in window. I have to get canvas coordinates. Is there some way how to solve it?

Thanks!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: Get position on Canvas

  #2  
Apr 29th, 2007
if i move the mouse, i will see coordinates of mouse not in canvas but in window.

Do you mean that you want the logical coordinates instead of the device coordinates?

If I'm reading you right, when the window is scrolled either vertically or horizontally, you want the mouse coordinates to reflect where you are logically on the canvas, rather than where you are physically on the device, ano?

See if this works for you:

  1. # -*- coding: cp1250 -*-
  2. import wx
  3. class Okno:
  4. def __init__(self,parent):
  5. self.okno=wx.MDIChildFrame(parent,title=u"Náhled situace",id=-1)
  6. self.okno.Maximize()
  7. self.okno.SetAutoLayout(True)
  8. self.okno.SetBackgroundColour("#FCFCFE")
  9. self.sizer = wx.FlexGridSizer(2,2,0,0)
  10.  
  11. # Add canvas
  12. self.platno = wx.ScrolledWindow(self.okno, id=wx.ID_ANY)
  13. self.platno.EnableScrolling(True, True)
  14. self.sirka = 10000
  15. self.vyska = 10000
  16. self.platno.SetScrollbars(20, 20, self.sirka/20, self.vyska/20)
  17. self.sizer.Add(self.platno, 1, wx.EXPAND)
  18.  
  19. self.sizer.Add((0,0))
  20. self.sizer.Add((0,0))
  21. self.sizer.Add((0,0))
  22. self.sizer.AddGrowableRow(0, 1)
  23. self.sizer.AddGrowableCol(0, 1)
  24. self.platno.Bind(wx.EVT_MOTION, self.Pohyb)
  25. self.okno.SetSizer(self.sizer)
  26. def Pohyb(self, akce):
  27. pos = akce.GetPosition()
  28. print pos, #doesnt work correctly
  29. x,y = pos
  30. print self.platno.CalcUnscrolledPosition(x,y) # <<<
  31.  
  32. if __name__ == "__main__":
  33. okno = wx.App(0)
  34. parent=wx.MDIParentFrame(None,size=wx.Size(500,500))
  35. Okno(parent)
  36. parent.Show()
  37. okno.MainLoop()
Reply With Quote  
Join Date: Mar 2006
Posts: 56
Reputation: Blujacker is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Blujacker Blujacker is offline Offline
Junior Poster in Training

Re: Get position on Canvas

  #3  
Apr 30th, 2007
ow, it works fine! thanks for fast answer!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:22 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC