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!

Recommended Answers

All 2 Replies

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:

# -*- 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):
        pos = akce.GetPosition()
        print pos, #doesnt work correctly
        x,y = pos
        print self.platno.CalcUnscrolledPosition(x,y)  # <<<
        
if __name__ == "__main__":
    okno = wx.App(0)
    parent=wx.MDIParentFrame(None,size=wx.Size(500,500))
    Okno(parent)
    parent.Show()
    okno.MainLoop()

ow, it works fine! thanks for fast answer!

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.