DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   wxCanvas-Get current coordinates (http://www.daniweb.com/forums/thread79372.html)

Blujacker May 27th, 2007 12:03 pm
wxCanvas-Get current coordinates
 
Hi, this is my code:
# -*- coding: cp1250 -*-
import  wx
class Okno:
    def __init__(self,parent=None,id=wx.ID_ANY,title="Graf"):
        self.okno=wx.MDIChildFrame(parent,title="Graf",id=-1)
        self.okno.Maximize()
        self.okno.SetAutoLayout(True)
        self.okno.SetBackgroundColour("#FCFCFE")     
        self.sizer = wx.FlexGridSizer(2,2,0,0)
        self.canvas = wx.ScrolledWindow(self.okno, id=wx.ID_ANY)
        self.canvas.EnableScrolling(True, True)       
        self.canvas.SetScrollbars(20, 20, 1000/20, 1000/20)
        self.sizer.Add(self.canvas, 1, wx.EXPAND)
        self.sizer.AddGrowableRow(0, 1)
        self.sizer.AddGrowableCol(0, 1) 
        self.okno.SetSizer(self.sizer)
        self.canvas.Bind(wx.EVT_PAINT, self.OnPaint)       
   
    def OnPaint(self, event):
        pass
if __name__ == "__main__":
    okno = wx.App(0)
    parent=wx.MDIParentFrame(None,size=wx.Size(500,500))
    o=Okno(parent)
    parent.Show()
    okno.MainLoop()
As you can see, its scrolled canvas and i need to get current coordinates of part of canvas which i can see (sory my english). For exapmle: If i run my code, that left up corner is (0,0) and down right corner is (500,500).

Thanks!

vegaseat May 27th, 2007 12:24 pm
Re: wxCanvas-Get current coordinates
 
This will give the mouse position within the whole scrolled canvas ...
"""
# -*- coding: cp1250 -*-
"""
# get coordinates of whole scrolled canvas
 
import  wx
 
class Okno:
    def __init__(self,parent=None,id=wx.ID_ANY,title="Graf"):
        self.okno=wx.MDIChildFrame(parent,title="Graf",id=-1)
        self.okno.Maximize()
        self.okno.SetAutoLayout(True)
        self.okno.SetBackgroundColour("#FCFCFE")     
        self.sizer = wx.FlexGridSizer(2,2,0,0)
        self.canvas = wx.ScrolledWindow(self.okno, id=wx.ID_ANY)
        self.canvas.EnableScrolling(True, True)       
        self.canvas.SetScrollbars(20, 20, 1000/20, 1000/20)
        self.sizer.Add(self.canvas, 1, wx.EXPAND)
        self.sizer.AddGrowableRow(0, 1)
        self.sizer.AddGrowableCol(0, 1) 
        self.okno.SetSizer(self.sizer)
        self.canvas.Bind(wx.EVT_PAINT, self.OnPaint)
 
        # hook left mouse button event
        self.canvas.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
 
    def OnPaint(self, event):
        pass
   
    def OnLeftDown(self, event):
        """left mouse button is pressed"""
        device_pt = event.GetPosition()  # tuple (x, y)
        # need to add current scrollbar value logic_pt to device_pt
        # if you want position relative to whole canvas
        logic_pt = self.canvas.CalcUnscrolledPosition(0, 0)
        #print device_pt, logic_pt
        adjusted_pt = (device_pt[0]+logic_pt[0], device_pt[1]+logic_pt[1])
        self.okno.SetTitle('LeftMouse = ' + str(adjusted_pt))
 
if __name__ == "__main__":
    okno = wx.App(0)
    parent=wx.MDIParentFrame(None,size=wx.Size(500,500))
    o=Okno(parent)
    parent.Show()
    okno.MainLoop()

Blujacker May 27th, 2007 1:16 pm
Re: wxCanvas-Get current coordinates
 
Thanks for fast answer, but i need something else. I dont know how to say it in english, so i painted it:
http://www.blujacker.wz.cz/coords.jpg

Blujacker May 28th, 2007 10:59 am
Re: wxCanvas-Get current coordinates
 
Lol, solved... i am really noob. The solution is:
up_left_corner=self.canvas.CalcUnscrolledPosition(0, 0)
dow_right_corner=self.canvas.CalcUnscrolledPosition(width_canvas, height_canvas)


All times are GMT -4. The time now is 3:40 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC