tkinter blues (greens, reds, ...)

Ron Adam rrr at ronadam.com
Fri Oct 28 14:49:45 EDT 2005



Steve Holden wrote:

> Sean McIlroy wrote:
> 
>> hi all
>>
>> i recently wrote a script that implements a puzzle. the interface
>> mostly consists of a bunch of colored disks on a tkinter canvas. the
>> problem is that the disks change their colors in ways other than the
>> way they're supposed to. it certainly isn't just a bug in my script,
>> since i can sometimes change the color of certain disks just by taking
>> focus off of the window and then bringing it back again! does this
>> sound like some known bug in tkinter? and if so, is there a recommended
>> way of working around it? if it matters, i'm using python 2.3 under
>> windows 95. any advice will be much appreciated.
>>
> It sounds to me much more like a bug in your script, to me at least. 
> Change of focus generates windowing events in much the same way as 
> clicking a button or hitting a key does, so I don't understand why you 
> think that "just [by] taking the focus off the window and bringing it 
> back again" shouldn't change anything.
> 
> For more specific insights we'd need to see some code, but sometimes 
> just changing your own focus from "Tkinter has a bug" to "my code has a 
> bug" is enough to help one find out what the problem really is. If you 
> have a soft toy I'd recommend you sit it down somewhere and explain to 
> it in great detail exactly why it can't be a bug in your program. You 
> may find you discover the error with no further assistance.
> 
> If not, fire the toy and ask again :-)
> 
> regards
>  Steve

To add to Steve's humorous perosonificatious techniques.  You should 
probably check that you aren't inadvertently using some sort of object 
id or window handle as a color value.  As long as the object you use 
returns an integer you won't get an error message, but instead get 
different colors when the canvas object is updated.  Like when changing 
the focus.

Another place to look is where you may be adding or converting rgb color 
values.

This function convert decimal rgb values to a hex rgb string that 
tkinter expects.

     def rgb(red, green, blue):
         """ Convert RGB value of 0 to 255 to
             hex Tkinter color string.
         """
         return '#%02x%02x%02x' % (red, green, blue)

Cheers,
    Ron




More information about the Python-list mailing list