Kaydet (Commit) 780028e1 authored tarafından Barry Warsaw's avatar Barry Warsaw

Change the way hex type-ins are displayed. The old way was way too

fragile.  Now the leading "0x" on hex numbers are displayed as labels
and the type-in entry fields just accept the hex digits.  Be sure to
strip off the "0x" string when displaying hex values too.

Also, de-string-module-ification, and other Python 2.x improvements.
üst ffa926d7
...@@ -12,10 +12,12 @@ color selection will be made on every change to the text field. Otherwise, ...@@ -12,10 +12,12 @@ color selection will be made on every change to the text field. Otherwise,
you must hit Return or Tab to select the color. you must hit Return or Tab to select the color.
""" """
from Tkinter import * import sys
import string
import re import re
from Tkinter import *
class TypeinViewer: class TypeinViewer:
def __init__(self, switchboard, master=None): def __init__(self, switchboard, master=None):
# non-gui ivars # non-gui ivars
...@@ -31,7 +33,12 @@ class TypeinViewer: ...@@ -31,7 +33,12 @@ class TypeinViewer:
# Red # Red
self.__xl = Label(self.__frame, text='Red:') self.__xl = Label(self.__frame, text='Red:')
self.__xl.grid(row=0, column=0, sticky=E) self.__xl.grid(row=0, column=0, sticky=E)
self.__x = Entry(self.__frame, width=4) subframe = Frame(self.__frame)
subframe.grid(row=0, column=1)
self.__xox = Label(subframe, text='0x')
self.__xox.grid(row=0, column=0, sticky=E)
self.__xox['font'] = 'courier'
self.__x = Entry(subframe, width=3)
self.__x.grid(row=0, column=1) self.__x.grid(row=0, column=1)
self.__x.bindtags(self.__x.bindtags() + ('Normalize', 'Update')) self.__x.bindtags(self.__x.bindtags() + ('Normalize', 'Update'))
self.__x.bind_class('Normalize', '<Key>', self.__normalize) self.__x.bind_class('Normalize', '<Key>', self.__normalize)
...@@ -39,14 +46,24 @@ class TypeinViewer: ...@@ -39,14 +46,24 @@ class TypeinViewer:
# Green # Green
self.__yl = Label(self.__frame, text='Green:') self.__yl = Label(self.__frame, text='Green:')
self.__yl.grid(row=1, column=0, sticky=E) self.__yl.grid(row=1, column=0, sticky=E)
self.__y = Entry(self.__frame, width=4) subframe = Frame(self.__frame)
self.__y.grid(row=1, column=1) subframe.grid(row=1, column=1)
self.__yox = Label(subframe, text='0x')
self.__yox.grid(row=0, column=0, sticky=E)
self.__yox['font'] = 'courier'
self.__y = Entry(subframe, width=3)
self.__y.grid(row=0, column=1)
self.__y.bindtags(self.__y.bindtags() + ('Normalize', 'Update')) self.__y.bindtags(self.__y.bindtags() + ('Normalize', 'Update'))
# Blue # Blue
self.__zl = Label(self.__frame, text='Blue:') self.__zl = Label(self.__frame, text='Blue:')
self.__zl.grid(row=2, column=0, sticky=E) self.__zl.grid(row=2, column=0, sticky=E)
self.__z = Entry(self.__frame, width=4) subframe = Frame(self.__frame)
self.__z.grid(row=2, column=1) subframe.grid(row=2, column=1)
self.__zox = Label(subframe, text='0x')
self.__zox.grid(row=0, column=0, sticky=E)
self.__zox['font'] = 'courier'
self.__z = Entry(subframe, width=3)
self.__z.grid(row=0, column=1)
self.__z.bindtags(self.__z.bindtags() + ('Normalize', 'Update')) self.__z.bindtags(self.__z.bindtags() + ('Normalize', 'Update'))
# Update while typing? # Update while typing?
self.__uwt = Checkbutton(self.__frame, self.__uwt = Checkbutton(self.__frame,
...@@ -62,6 +79,13 @@ class TypeinViewer: ...@@ -62,6 +79,13 @@ class TypeinViewer:
def __togglehex(self, event=None): def __togglehex(self, event=None):
red, green, blue = self.__sb.current_rgb() red, green, blue = self.__sb.current_rgb()
if self.__hexp.get():
label = '0x'
else:
label = ' '
self.__xox['text'] = label
self.__yox['text'] = label
self.__zox['text'] = label
self.update_yourself(red, green, blue) self.update_yourself(red, green, blue)
def __normalize(self, event=None): def __normalize(self, event=None):
...@@ -70,31 +94,27 @@ class TypeinViewer: ...@@ -70,31 +94,27 @@ class TypeinViewer:
icursor = ew.index(INSERT) icursor = ew.index(INSERT)
if contents and contents[0] in 'xX' and self.__hexp.get(): if contents and contents[0] in 'xX' and self.__hexp.get():
contents = '0' + contents contents = '0' + contents
# figure out what the contents value is in the current base # Figure out the contents in the current base.
try: try:
if self.__hexp.get(): if self.__hexp.get():
v = string.atoi(contents, 16) v = int(contents, 16)
else: else:
v = string.atoi(contents) v = int(contents)
except ValueError: except ValueError:
v = None v = None
# if value is not legal, delete the last character inserted and ring # If value is not legal, or empty, delete the last character inserted
# the bell # and ring the bell. Don't ring the bell if the field is empty (it'll
if v is None or v < 0 or v > 255: # just equal zero.
if v is None:
pass
elif v < 0 or v > 255:
i = ew.index(INSERT) i = ew.index(INSERT)
if event.char: if event.char:
contents = contents[:i-1] + contents[i:] contents = contents[:i-1] + contents[i:]
icursor = icursor-1 icursor -= 1
ew.bell() ew.bell()
elif self.__hexp.get(): elif self.__hexp.get():
# Special case: our contents were 0x0 and we just deleted the contents = hex(v)[2:]
# trailing 0. We want our contents to now be 0x and not 0x0.
if v == 0 and contents == '0':
contents = '0x'
icursor = END
ew.bell()
elif not (v == 0 and contents == '0x'):
contents = hex(v)
else: else:
contents = int(v) contents = int(v)
ew.delete(0, END) ew.delete(0, END)
...@@ -106,36 +126,21 @@ class TypeinViewer: ...@@ -106,36 +126,21 @@ class TypeinViewer:
self.__update(event) self.__update(event)
def __update(self, event=None): def __update(self, event=None):
redstr = self.__x.get() redstr = self.__x.get() or '0'
greenstr = self.__y.get() greenstr = self.__y.get() or '0'
bluestr = self.__z.get() bluestr = self.__z.get() or '0'
if self.__hexp.get(): if self.__hexp.get():
red = string.atoi(redstr, 16) base = 16
green = string.atoi(greenstr, 16)
blue = string.atoi(bluestr, 16)
else: else:
def intify(colorstr): base = 10
if colorstr == '': red, green, blue = [int(x, base) for x in (redstr, greenstr, bluestr)]
return 0
else:
return string.atoi(colorstr)
red, green, blue = map(intify, (redstr, greenstr, bluestr))
self.__sb.update_views(red, green, blue) self.__sb.update_views(red, green, blue)
def update_yourself(self, red, green, blue): def update_yourself(self, red, green, blue):
if self.__hexp.get(): if self.__hexp.get():
# Special case: our contents were 0x0 and we just deleted the sred, sgreen, sblue = [hex(x)[2:] for x in (red, green, blue)]
# trailing 0. We want our contents to now be 0x and not 0x0.
def hexify((color, widget)):
contents = widget.get()
if not (color == 0 and contents == '0x'):
return hex(color)
return contents
redstr, greenstr, bluestr = map(hexify, ((red, self.__x),
(green, self.__y),
(blue, self.__z)))
else: else:
redstr, greenstr, bluestr = red, green, blue sred, sgreen, sblue = red, green, blue
x, y, z = self.__x, self.__y, self.__z x, y, z = self.__x, self.__y, self.__z
xicursor = x.index(INSERT) xicursor = x.index(INSERT)
yicursor = y.index(INSERT) yicursor = y.index(INSERT)
...@@ -143,9 +148,9 @@ class TypeinViewer: ...@@ -143,9 +148,9 @@ class TypeinViewer:
x.delete(0, END) x.delete(0, END)
y.delete(0, END) y.delete(0, END)
z.delete(0, END) z.delete(0, END)
x.insert(0, redstr) x.insert(0, sred)
y.insert(0, greenstr) y.insert(0, sgreen)
z.insert(0, bluestr) z.insert(0, sblue)
x.icursor(xicursor) x.icursor(xicursor)
y.icursor(yicursor) y.icursor(yicursor)
z.icursor(zicursor) z.icursor(zicursor)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment