Hi all. I have a very simple app, but I don't know how to get position of the circles. Here's the code:

from Tkinter import *

master = Tk()

global w

def dotToDot1(event):
    global w
    circles = 0
    x = event.x
    y = event.y
    c1 = w.create_oval(x, y, x+10, y+10)
    c2 = w.create_oval(x, y, x+10, y+10)
    l = w.create_line(c1.x, c1.y, c2.x, c2.y)
       
w = Canvas(master, width=200, height=100)
w.bind("<Button->", dotToDot1)
w.pack()
       
mainloop()

How do I get c1 x, y and c2 x, y?
It always says:
'int' object has no attribute 'x'

Please help!

Let me rewrite your code a little to illustrate this ...

from Tkinter import *

master = Tk()

def draw_circle(event):
    x1 = event.x
    y1 = event.y
    x2 = x1 + 10
    y2 = y1 + 10
    # note that c1 is simply a numeric id
    c1 = cv.create_oval(x1, y1, x2, y2)
    # calculate center coordinates of bounding rectangle/oval
    xc = x1 + (x2 - x1)/2
    yc = y1 + (y2 - y1)/2
    print xc, yc
       
# note that cv will be global here
cv = Canvas(master, width=200, height=100)
cv.bind("<Button-1>", draw_circle)
cv.pack()
       
mainloop()

well, everything is OK but... the starting point of the line is ALWAYS at the same place:

from Tkinter import *

master = Tk()

cx = 0
cy = 0

def draw_circle2(event):
    x1 = event.x
    y1 = event.y
    x2 = x1+10
    y2 = y1+10
    c2 = w.create_oval(x1, y1, x2, y2)
    cx2 = x1 + (x1-x2)/2
    cy2 = y1 + (y1-y2)/2
    print cx2, cy2
    l = w.create_line(cx+10, cy+10, cx2+10, cy2+10)

def draw_circle1(event):
    x1 = event.x
    y1 = event.y
    x2 = x1 + 10
    y2 = y1 + 10
    c1 = w.create_oval(x1,y1,x2,y2)
    cx = x1 + (x1-x2)/2
    cy = y1 + (y1-y2)/2
    print cx, cy
    draw_circle2(event)
       
w = Canvas(master, width=200, height=100)
w.bind("<Button-1>", draw_circle1)
w.pack()
       
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.