tags:

views:

55

answers:

1

I use python 2.6 under linux (SUSE Linux Enterprise Desktop 11 (x86_64)). I tested some very simple code :

import tkColorChooser
tkColorChooser.askcolor()

then if I click on cancel, I always get error like:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/lib-tk/tkColorChooser.py", line 62, in askcolor
    return Chooser(**options).show()
  File "/usr/lib64/python2.6/lib-tk/tkCommonDialog.py", line 50, in show
    s = self._fixresult(w, s)
  File "/usr/lib64/python2.6/lib-tk/tkColorChooser.py", line 48, in _fixresult
    r, g, b = widget.winfo_rgb(result)
  File "/usr/lib64/python2.6/lib-tk/Tkinter.py", line 786, in winfo_rgb
    self.tk.call('winfo', 'rgb', self._w, color))
_tkinter.TclError: unknown color name ""

I have more complicated code using tkColorChooser, which gives same error if I click on cancel in the color chooser dialog. I think I can catch the error. But is tkColorChooser designed to be like this? Is there any other neater way to cope with this problem? Thanks!

A: 

Looking at the version of tkColorChooser.py I have (Python 2.6.4, Win32), it should support the user pressing cancel (as do and should the other predefined dialogs): it is indeed supposed to return None when the results evals to False in a boolean context.

Therefore, something strange is happening.

edit: as I noted in the comments, it is indeed a bug that has been fixed in version 2.6.2.

RaphaelSP
The documentation states that if cancel is clicked, tkColorChooser returns None.
Hongbo Zhu
@Hongbo: You may have Python 2.6.1 or lower, something that looks much like your bug was fixed in 2.6.2 (I've just peeked into the sources). Have you tried upgrading ?
RaphaelSP
@RaphaelISP: I have Python 2.6.0.
Hongbo Zhu
@Hongbo: you have two choices, then: upgrade to Python >= 2.6.2 or surround calls to `tkColorChooser.askcolor()` with appropriate `try...except` statements.
RaphaelSP
@RaphaelSP: I guess I will stick to the try...except solution. Thanks a lot for your help.
Hongbo Zhu
@Hongbo: In this case, I suggest you catch only `TclError` and then re-raise the exception if its message does not match the description you provided, so that any other exception will not be eaten. Sorry you cannot upgrade...
RaphaelSP
@RaphaelSP: thanks for the reminder. Not being able to upgrade might be a good thing since users of my code may use lower version as well :-)
Hongbo Zhu