Python-tkinter and canvas tags

JB jbwellsiv at yahoo.com
Sat Apr 13 16:49:23 EDT 2002


Alright. I finally decided to give learning python a go this week and am
working on a rather simple gui app with Tkinter.

I have an odd problem and my hope is that someone can clear it up for me.

I have a main class (cfMain.py) that will serve as the container for the gui
portion of the application. cfMain.py defines a canvas widget on which I
will draw a number of shapes, each shape representing a different element in
a T1/T3 circuit.

A network element in the circuit is represented by a square. I've created
another class called gSNC.py to contain the gui logic for these network
elements.

Basically, when a user presses a button on the main app, a square is added
to the canvas. The gSNC constructor is called by an button event handler in
cfMain.py, passing a reference to the canvas widget. gSNC then uses this
canvas widget to render the square with the create_polygon method.

The problem arises when I try to handle any mouse entered/exited events. I
want the square to change color when a mouse enters or exits. If I move
everything (create_polygon call included) into cfMain.py and bind the event
handlers there, I'm able to change the color by using something like:

self.theCanvas.itemconfig(CURRENT,
fill=cfMain.SNCHLColor)

However, I'd like the event-handling methods to be defined in the class
gSNC.py, so I bind the events in the following manner:

# create a polygon

tmp = self.canvasRef.create_polygon(...omitted...)

self.canvasRef.tag_bind(tmp,
"<Any-Enter>", self.handleEnter)

handleEnter is defined as:

def handleEnter(self, event):
self.canvasRef.itemConfig(CURRENT,
fill=self.HLColor)

When I run it this way, the events are definitely being handled, but I get
an error stating that "NameError: Global name 'CURRENT' is not defined".

I've been trying to jog my memory of Tk's handling of canvas tags and
looking through references, but nothing has explained why this might be
happening yet. It may be a misunderstanding on my part, but I assumed that
since canvasRef is a reference the to canvas widget in cfMain.py, the
CURRENT tag should automatically be assigned to whatever is under the mouse
(as it is when I contain all polygon creation under the cfMain class). It
seems intuitive that you'd want to have this functionality, rather than
grouping all code that draws anything on a canvas in one class.

Can anyone clear my head?

Also, I've been trying to find info on how Tkinter handles event propagation
between classes. If anyone could point me in the right direction, I'd
appreciate it.

Thanks!
John



More information about the Python-list mailing list