473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

oval

I tested the following code and wanted to get the message of "oval2
got hit" if I click the red one. But I always got "oval1 got hit".
from Tkinter import *
root=Tk()
canvas=Canvas(root,width=100,height=100)
canvas.pack()
a=canvas.create_oval(10,10,20,20,tags='oval1',fill ='blue')
b=canvas.create_oval(50,50,80,80,tags='oval2',fill ='red')
def myEvent(event):
if a:
print "oval1 got hit!"
else:
print "oval2 got hit"
canvas.tag_bind('oval1','<Button>',func=myEvent)
canvas.tag_bind('oval2','<Button>',func=myEvent)
root.mainloop()
Dec 4 '05 #1
7 2006
Ben Bush wrote:
I tested the following code and wanted to get the message of "oval2
got hit" if I click the red one. But I always got "oval1 got hit".
from Tkinter import *
root=Tk()
canvas=Canvas(root,width=100,height=100)
canvas.pack()
a=canvas.create_oval(10,10,20,20,tags='oval1',fill ='blue')
b=canvas.create_oval(50,50,80,80,tags='oval2',fill ='red')
def myEvent(event):
if a:


Here is your problem. a is a name, bound to some value. So - it is true,
as python semantics are that way. It would not be true if it was e.g.

False, [], {}, None, ""
What you want instead is something like

if event.source == a:
...

Please note that I don't know what event actually looks like in Tkinter,
so check the docs what actually gets passed to you.
Regards,

Diez
Dec 4 '05 #2
On 12/4/05, Diez B. Roggisch <de***@nospam.web.de> wrote:
Ben Bush wrote:
I tested the following code and wanted to get the message of "oval2
got hit" if I click the red one. But I always got "oval1 got hit".
from Tkinter import *
root=Tk()
canvas=Canvas(root,width=100,height=100)
canvas.pack()
a=canvas.create_oval(10,10,20,20,tags='oval1',fill ='blue')
b=canvas.create_oval(50,50,80,80,tags='oval2',fill ='red')
def myEvent(event):
if a:


Here is your problem. a is a name, bound to some value. So - it is true,
as python semantics are that way. It would not be true if it was e.g.

False, [], {}, None, ""
What you want instead is something like

if event.source == a:
...

Please note that I don't know what event actually looks like in Tkinter,
so check the docs what actually gets passed to you.


got AttributeError: Event instance has no attribute 'source'
Dec 4 '05 #3
Ben Bush wrote:
On 12/4/05, Diez B. Roggisch <de***@nospam.web.de> wrote:
Ben Bush wrote:
> I tested the following code and wanted to get the message of "oval2
> got hit" if I click the red one. But I always got "oval1 got hit".
> from Tkinter import *
> root=Tk()
> canvas=Canvas(root,width=100,height=100)
> canvas.pack()
> a=canvas.create_oval(10,10,20,20,tags='oval1',fill ='blue')
> b=canvas.create_oval(50,50,80,80,tags='oval2',fill ='red')
> def myEvent(event):
> if a:


Here is your problem. a is a name, bound to some value. So - it is true,
as python semantics are that way. It would not be true if it was e.g.

False, [], {}, None, ""
What you want instead is something like

if event.source == a:
...

Please note that I don't know what event actually looks like in Tkinter,
so check the docs what actually gets passed to you.


got AttributeError: Event instance has no attribute 'source'


Note that Diez said /something/ /like/ /event.source/. The source is
actually called widget -- but that doesn't help as it denotes the canvas as
a whole, not the individual shape.

The following should work if you want one handler for all shapes:

def handler(event):
print event.widget.gettags("current")[0], "got hit"
canvas.tag_bind('oval1', '<Button>', handler)
canvas.tag_bind('oval2', '<Button>', handler)

I prefer one handler per shape:

def make_handler(message):
def handler(event):
print message
return handler

canvas.tag_bind('oval1', '<Button>', make_handler("oval 1 got hit"))
canvas.tag_bind('oval2', '<Button>', make_handler("oval 2 got hit"))

Peter

Dec 4 '05 #4
>>
What you want instead is something like

if event.source == a:
...

Please note that I don't know what event actually looks like in Tkinter,
so check the docs what actually gets passed to you.

got AttributeError: Event instance has no attribute 'source'


As I said: I don'k _know_ how event actually looks like,a s I'm not a
Tkinter-expert, but I've had plenty of projects involving other
GUI-Toolkits, which work all the same in this manner. After all the
whole purposs of the event parameter is to inform you about the cause
for that event - in this case the pressing of a MB on a specific canvas
element.

But if you'd take it on you to consult the documentation as I asked you
to do, I'm pretty sure you find the proper attribute.
Regards,

Diez
Dec 4 '05 #5
On 12/4/05, Diez B. Roggisch <de***@nospam.web.de> wrote:

What you want instead is something like

if event.source == a:
...

Please note that I don't know what event actually looks like in Tkinter,
so check the docs what actually gets passed to you.

got AttributeError: Event instance has no attribute 'source'


As I said: I don'k _know_ how event actually looks like,a s I'm not a
Tkinter-expert, but I've had plenty of projects involving other
GUI-Toolkits, which work all the same in this manner. After all the
whole purposs of the event parameter is to inform you about the cause
for that event - in this case the pressing of a MB on a specific canvas
element.

But if you'd take it on you to consult the documentation as I asked you
to do, I'm pretty sure you find the proper attribute.
Regards,

Diez
--
http://mail.python.org/mailman/listinfo/python-list

thanks anyway. The python manual for event is very brief and it is
hard for me to understand.
--
Ben Bush
Dec 5 '05 #6
On 12/4/05, Peter Otten <__*******@web.de> wrote:
Ben Bush wrote:
On 12/4/05, Diez B. Roggisch <de***@nospam.web.de> wrote:
Ben Bush wrote:
> I tested the following code and wanted to get the message of "oval2
> got hit" if I click the red one. But I always got "oval1 got hit".
> from Tkinter import *
> root=Tk()
> canvas=Canvas(root,width=100,height=100)
> canvas.pack()
> a=canvas.create_oval(10,10,20,20,tags='oval1',fill ='blue')
> b=canvas.create_oval(50,50,80,80,tags='oval2',fill ='red')
> def myEvent(event):
> if a:

Here is your problem. a is a name, bound to some value. So - it is true,
as python semantics are that way. It would not be true if it was e.g.

False, [], {}, None, ""
What you want instead is something like

if event.source == a:
...

Please note that I don't know what event actually looks like in Tkinter,
so check the docs what actually gets passed to you.


got AttributeError: Event instance has no attribute 'source'


Note that Diez said /something/ /like/ /event.source/. The source is
actually called widget -- but that doesn't help as it denotes the canvas as
a whole, not the individual shape.

The following should work if you want one handler for all shapes:

def handler(event):
print event.widget.gettags("current")[0], "got hit"
canvas.tag_bind('oval1', '<Button>', handler)
canvas.tag_bind('oval2', '<Button>', handler)

I prefer one handler per shape:

def make_handler(message):
def handler(event):
print message
return handler

canvas.tag_bind('oval1', '<Button>', make_handler("oval 1 got hit"))
canvas.tag_bind('oval2', '<Button>', make_handler("oval 2 got hit"))

Peter

--
http://mail.python.org/mailman/listinfo/python-list

Peter,
It works. Thanks!
is there any good material to read if I want to improve my
understanding of working on interactive ways of dealing with shapes
on the TKinter?
--
Ben Bush
Dec 5 '05 #7
Ben Bush wrote:
is there any good material to read if I want to improve my
understanding of working on interactive ways of dealing with shapes
on the TKinter?


See http://wiki.python.org/moin/TkInter for a list of references.
For me John Shipman's Tkinter Reference is normally sufficient.

Peter

Dec 5 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Robert Spoons | last post by:
Can you look over this code, preferably try it, and comment? I believe the 'extend' function below will allow you to use full 'class inheritance' in javascript, but I would like to verify it. ...
0
by: Sahmiac | last post by:
Hello Everyone, I'm sure this question has been asked a million times, but I'm going crazy trying to get my xml validation to work. I've spent the day reading msdn, the dot net documentation and...
15
by: Bernard Bourée | last post by:
I have the following code: Dim mVar As New Variable() mVar.NomVal = "Test" ==> this line return a NomVal ="Test" mVar.oVal = 10D ====> but this one return a oVal= NOTHING !!!! What...
4
by: =?Utf-8?B?Ym9va2VyQG1ndA==?= | last post by:
Ok, I inherited some code written in vb that is part of a web application. My overall objective is to be able to take multiple names from a "LastName" text box and use those names in my SQL query...
0
by: vabsjava | last post by:
Hello Everybody..... I want to use command button which is oval in shape..... how to convert the button shaped in rectangle to oval.... please me in this regard... Thank You
4
by: John Kotuby | last post by:
Cleveland? Hi all, I am writing an ASP.NET 2.0 application in VB.NET. My boss doesn't like the fact that I still use either rectangular or the default browser buttons on my pages. He says it...
1
by: nish88 | last post by:
Hi!! Actually,i am doing a simulation and i want a dot (small oval) to pop up when i click(by mouse) on the canvas. can anyone please help me or if possible give me the piece of codes to perform...
9
by: parkho | last post by:
hi, I have a form which I want to draw 4 Ovals with the same center point but with different directions so it looks like a star. I used the circle command which I can only get two Ovals on the...
3
by: potupaul | last post by:
hii... plz help me out ,how to create rounded or oval buttons using c# in VS 2005.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.