Discussion:
Python-tkinter and canvas tags
Eric Brunel
2002-04-15 10:08:46 UTC
Permalink
JB wrote:
[snip]
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".
Errrr, maybe you forgot to do "from Tkinter import *" in your module?
CURRENT is just a constant defined in this module with the string value
'current', so you just have to do a "from Tkinter import *" to use it. You
may also just do an "import Tkinter" and use Tkinter.CURRENT instead of
just CURRENT. In other words, don't look for magic here: CURRENT is just a
constant variable like any other one, and getting a NameError using it just
means that you didn't do the right things to get it properly defined...

BTW, AFAIK, the method itemConfig on canvases does not exist; it's named
itemconfigure.

HTH
--
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
JB
2002-04-13 20:49:23 UTC
Permalink
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

Loading...