faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 1 people (100%) answered Yes

Entry

Displaying colors in X-Windows

Jul 5th, 2000 10:03
Nathan Wallace, Hans Nowak, Snippet 349, Randall Hopper


"""
Packages: gui.tkinter;operating_systems.unix
"""
"""
 |A friend of mine is running Python on a Sparc station.  I sent him some
 |code which uses a bunch of colors, and even though he's got a truecolor
 |display it looked screwy.  He ran xdpyinfo, and these are his results: 
 ...
 |  Depth: 8
 |  Visual Class: PseudoColor
 |
 |so I guess that means python took the first color visual type it
 |found... is there a way to force it to use my 24 bit visual?
Yes.  Frame and Toplevel widgets have visual and colormap resources which
can be set.  Here is some simple code that just deals with 8bpp pseudo and
24bpp true.
"""
from Tkinter import *   # added by PSST
root     = Tk()
root.wm_withdraw()
# Determine the depth, if non specified
if depth: assert depth in [ 8, 24 ]
else:     depth = root.winfo_depth()
# Just force 24-bit
depth = 24
if depth == 8: visual = "pseudocolor 8"
else:          visual = "truecolor  24"
main     = Toplevel( root, visual=visual )
"""
Stack your widgets under 'main' rather than 'root'.
To be more robust, pick a visual returned by:
  root.winfo_visualsavailable()
As you suggested, selecting the proper visual is the best solution (as
opposed to forcing the user to reconfigure the default depth of their X
server).
"""