from Tkinter import*

okno=Tk()

okno.title('Pomoc please')

platno=Canvas()

platno.pack()

def volana_funkce(event):

    print event.widget

oval1=platno.create_oval(10,20,30,40,fill='red',tags='klik')

oval2=platno.create_oval(50,70,80,90,fill='blue',tags='klik')

platno.tag_bind('klik','<1>',volana_funkce)

mainloop()

how to get a name of widget if click on them?e.g i klik on oval1 and function volana_funkce will return oval1..
thanks

You have to set widget for name to come up. At the moment I don't quite know how, I don't have my notes along.

I would simply use lambda to extent the arguments passed to your callback function. I combined your code with an example I had ...

# display which widget was clicked

from Tkinter import *

root = Tk()

root.title('Click on ovals')

canvas1 = Canvas()
canvas1.pack()

def func1(event, widget_name):
    print widget_name

# use different tags
oval1 = canvas1.create_oval(10, 30, 40, 50, fill='red', tags='click1')
oval2 = canvas1.create_oval(50, 70, 80, 90, fill='blue', tags='click2')

# left mouse click = '<1>'
# use lambda to send more info to callback function
canvas1.tag_bind('click1', '<1>', (lambda e: func1(e, "oval1")))
canvas1.tag_bind('click2', '<1>', (lambda e: func1(e, "oval2")))

mainloop()

ï know this, but the object in canvas must have same tag.

SOLVED, my friend is very good pythoner and he know way:

from Tkinter import *

root = Tk()

root.title('Click on ovals')

canvas1 = Canvas()
canvas1.pack()

def func1(event):
    print canvas1.find_withtag(CURRENT)

# use different tags
oval1 = canvas1.create_oval(10, 30, 40, 50, fill='red', tags='click1')
oval2 = canvas1.create_oval(50, 70, 80, 90, fill='blue', tags='click2')

# left mouse click = '<1>'
# use lambda to send more info to callback function
canvas1.tag_bind('click1', '<1>', func1)
canvas1.tag_bind('click2', '<1>', func1)

mainloop()

Very nice, but I understood you wanted the actual name of the widget as the answer.

Very nice, but I understood you wanted the actual name of the widget as the answer.

than sorry

Actually, I like your friend's solution. You can easily expand it to bring up the widget's name:

# which widget has been clicked?
from Tkinter import *

root = Tk()
root.title('Click on ovals')

canvas1 = Canvas()
canvas1.pack()

def func1(event):
    print widget_list[canvas1.find_withtag(CURRENT)[0]-1]

oval1 = canvas1.create_oval(10, 30, 40, 50, fill='red', tags='click')
oval2 = canvas1.create_oval(50, 70, 80, 90, fill='blue', tags='click')
oval3 = canvas1.create_oval(90, 110, 120, 130, fill='green', tags='click')

widget_list = ['oval1', 'oval2', 'oval3']

# left mouse click = '<1>'
canvas1.tag_bind('click', '<1>', func1)

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.