File: pyedit-products/unzipped/docetc/examples/RunCode-examples/pickcolor-test.py

# Another GUI test: make 3 Tks that all share same stdout (Capture window).
# Kill by closing all 3 Tks, or closing the run's output Capture window.

#!/usr/bin/python
"""
===============================================================================
pickcolor.py (frigcal utility script)
Select a color, display it on a button, and show its #RRGGBB hex-string value
for copy/paste into frigcal config files, [tT]kinter keywords, or webpage tags.
Use Ctrl-C/Ctrl-V to copy/paste (or your platform's equivalents), and resize
the GUI's window for a larger view of the selected color.  Borrowed from the
book "Programming Python, 4th Edition", with a slightly extended GUI here.
===============================================================================
"""

import sys
if sys.version[0] == '2':                   # for Python 3.X or 2.X
    from Tkinter import *
    from tkColorChooser import askcolor
elif sys.version[0] == '3':
    from tkinter import *
    from tkinter.colorchooser import askcolor

def setBgColor(push):
    (triple, hexstr) = askcolor()
    if hexstr:
        print(hexstr)
        push.config(bg=hexstr)
        show.set("#RRGGBB string = '" + hexstr + "'")

for i in range(3): #int(sys.argv[1])):

  root = Tk()
  root.title('pickcolor')

  if sys.platform.startswith('darwin'):
    # Mac OS X Tk doesn't do bg color on buttons
    push = Label(root, text='Press to Select Color')
    push.bind('<Button-1>', lambda event, p=push: setBgColor(p))   # loop var
  else: 
    # but Windows and Linux do
    push = Button(root, text='Press to Select Color')
    push.config(command=lambda p=push: setBgColor(p))              # loop var

  push.config(height=5, width=30, font=('times', 20, 'bold'))
  push.pack(expand=YES, fill=BOTH)

  show = StringVar()
  show.set('(hex string appears here)')
  Entry(root, textvariable=show,
      justify=CENTER, font='bold').pack(fill=X)

Label(root, text=str(sys.argv)).pack()   # on last made
root.mainloop()



[Home page] Books Code Blog Python Author Training Search ©M.Lutz