1

I'm writing an introduction to Python for pupils of about 15 years old. I would like to let them do graphical and cute "things" with the Turtle module, with as few difficulties as possible. I currently try to use random colors, and the simplest way would be to use random.choice() on a list of color names - as Tkinter, on top of which is built Turtle, knows many symbolic color names (http://wiki.tcl.tk/37701 or http://www.tcl.tk/man/tcl8.3/TkCmd/colors.htm). But I can't figure how to find an object, module or sub-module where all these aliases are defined. Does someone has an idea ? Thanks !

natsirt
  • 127
  • 5
  • Please read carefully my question : I do know this page, but it only gives me the name of all colors, not a way to easily randomly pick one of these names. Or I missed something in this page... – natsirt Jul 13 '17 at 13:43
  • Anyway, I could use this link https://wiki.tcl.tk/16166 and copy-paste all the color names in a giant string and break it at spaces, but my pupils are great beginners, this is not a way I can nor want to guide them on... yet. – natsirt Jul 13 '17 at 13:47
  • Not the way you wanted but at least someone hard-coded names in a list.[Colour chart for Tkinter and Tix Using Python](https://stackoverflow.com/questions/4969543/colour-chart-for-tkinter-and-tix-using-python) – Lafexlos Jul 13 '17 at 13:50
  • Also, if you don't care about color names, just create a random hex and use it. [Generating a Random Hex Color in Python](https://stackoverflow.com/questions/13998901/generating-a-random-hex-color-in-python) – Lafexlos Jul 13 '17 at 13:53
  • Thanks, that's a workaround... but for great beginners it's still too much. These evil color names must be defined in a class or a subclass or a module constant ! Internally, Tkinter (or Tcl/Tk) must use RGB representation (at least under an hex string). Or not ? And I don't want to use randomly generated hex string. It's also too difficult for their level and it is beyond my scope. I'm a math teacher, I now must teach Python but keep my steps in a mathematical context which does not involve hexadecimal numeration ;-) – natsirt Jul 13 '17 at 13:54
  • This might be cool to use: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/tkColorChooser.html – mauve Jul 13 '17 at 13:54
  • The colors are not in a constant defined in a module accessible to python. – Bryan Oakley Jul 13 '17 at 13:57
  • **@mauve :** thanks but no, it isn't. I want my pupils to be able to create an arbitraty number of turtles, each visually differents by their colors. I do know I can randomly generate hex string, or even rgb triplet, but I don't want to dive into color generation. That's not my point... – natsirt Jul 13 '17 at 14:00
  • **@Bryan Oakley :** I figured out they are not at all in Tcl nor Tk sources (just grep-ed them !) But where are these aliases defined ? – natsirt Jul 13 '17 at 14:02
  • You can go to your python folder and search for one those color names. Maybe "SlateGrey" or some rare names that you know it can be used in Python. Then you'll find some txt files. Named `namedcolors.txt`, `rgb.txt` etc. under Python36\Tools folder. Most likely python uses those. – Lafexlos Jul 13 '17 at 14:04
  • Thanks. I'm under a GNU/Linux OS, and tried to grep with no success on honeydew, which also seemed very specific to me. May be my goal is unreachable... – natsirt Jul 13 '17 at 14:08

2 Answers2

1

These color names are not in a module that you can access from a script.

On systems that use the X windowing system (eg: linux), you can find the color names in a file named rgb.txt (eg: /etc/X11/rgb.txt or /usr/lib/X11/rgb.txt) which is where tkinter gets the values from.

For Windows and OSX systems you'll have to create your own list of color names. You can get the official list of supported color names from the tcl/tk man pages: http://www.tcl.tk/man/tcl8.6/TkCmd/colors.htm

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • In windows(at least for me - win7 x64), there is also a file called `rgb.txt`. That is under `...\Python\Python36\Tools\pynche\X`. – Lafexlos Jul 13 '17 at 15:16
  • Thanks for the info. No way for me, thus. One cannot win every time ! – natsirt Jul 13 '17 at 15:33
1

Here is a program that will list them all for you:

https://github.com/novel-yet-trivial/TkNamedColors

Novel
  • 13,406
  • 2
  • 25
  • 41