Barry Warsaw | 4638c5b | 1998-10-02 16:20:14 +0000 | [diff] [blame] | 1 | """TextViewer class. |
| 2 | |
| 3 | The TextViewer allows you to see how the selected color would affect various |
| 4 | characteristics of a Tk text widget. This is an output viewer only. |
| 5 | |
| 6 | In the top part of the window is a standard text widget with some sample text |
| 7 | in it. You are free to edit this text in any way you want (TBD: allow you to |
| 8 | change font characteristics). If you want changes in other viewers to update |
| 9 | text characteristics, turn on Track color changes. |
| 10 | |
| 11 | To select which characteristic tracks the change, select one of the radio |
| 12 | buttons in the window below. Text foreground and background affect the text |
| 13 | in the window above. The Selection is what you see when you click the middle |
| 14 | button and drag it through some text. The Insertion is the insertion cursor |
| 15 | in the text window (which only has a background). |
| 16 | """ |
| 17 | |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 18 | import sys |
| 19 | from Tkinter import * |
| 20 | from pynche import __version__ |
| 21 | import ColorDB |
| 22 | |
| 23 | class TextViewer: |
| 24 | def __init__(self, switchboard, parent=None): |
| 25 | self.__sb = switchboard |
| 26 | root = self.__root = Toplevel(parent, class_='Pynche') |
| 27 | root.protocol('WM_DELETE_WINDOW', self.__withdraw) |
| 28 | root.title('Pynche %s' % __version__) |
| 29 | root.iconname('Pynche Text Window') |
| 30 | root.bind('<Alt-q>', self.__quit) |
| 31 | root.bind('<Alt-Q>', self.__quit) |
Barry Warsaw | 964fbbb | 1998-10-05 21:15:08 +0000 | [diff] [blame^] | 32 | root.bind('<Alt-w>', self.__withdraw) |
| 33 | root.bind('<Alt-W>', self.__withdraw) |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 34 | # |
| 35 | # create the text widget |
| 36 | # |
| 37 | self.__text = Text(root, relief=SUNKEN, |
| 38 | background='black', |
| 39 | foreground='white', |
| 40 | width=35, height=15) |
| 41 | self.__text.pack() |
| 42 | self.__text.insert(0.0, '''\ |
| 43 | Insert some stuff here and play |
| 44 | with the buttons below to see |
| 45 | how the colors interact in |
| 46 | textual displays.''') |
| 47 | # |
| 48 | # variables |
| 49 | self.__trackp = BooleanVar() |
| 50 | self.__trackp.set(0) |
| 51 | self.__which = IntVar() |
Barry Warsaw | 4638c5b | 1998-10-02 16:20:14 +0000 | [diff] [blame] | 52 | self.__which.set(0) |
Barry Warsaw | f9c6ddc | 1998-10-01 16:45:32 +0000 | [diff] [blame] | 53 | # |
| 54 | # track toggle |
| 55 | self.__t = Checkbutton(root, text='Track color changes', |
| 56 | variable=self.__trackp, |
| 57 | relief=GROOVE) |
| 58 | self.__t.pack(fill=X, expand=YES) |
| 59 | frame = self.__frame = Frame(root) |
| 60 | frame.pack() |
| 61 | # |
| 62 | # labels |
| 63 | self.__labels = [] |
| 64 | row = 2 |
| 65 | for text in ('Text:', 'Selection:', 'Insertion:'): |
| 66 | l = Label(frame, text=text) |
| 67 | l.grid(row=row, column=0, sticky=E) |
| 68 | self.__labels.append(l) |
| 69 | row = row + 1 |
| 70 | col = 1 |
| 71 | for text in ('Foreground', 'Background'): |
| 72 | l = Label(frame, text=text) |
| 73 | l.grid(row=1, column=col) |
| 74 | self.__labels.append(l) |
| 75 | col = col + 1 |
| 76 | # |
| 77 | # radios |
| 78 | self.__radios = [] |
| 79 | val = 0 |
| 80 | for col in (1, 2): |
| 81 | for row in (2, 3, 4): |
| 82 | # there is no insertforeground option |
| 83 | if row==4 and col==1: |
| 84 | continue |
| 85 | r = Radiobutton(frame, variable=self.__which, |
| 86 | value=(row-2)*2 + col-1) |
| 87 | r.grid(row=row, column=col) |
| 88 | self.__radios.append(r) |
| 89 | |
| 90 | def __quit(self, event=None): |
| 91 | sys.exit(0) |
| 92 | |
| 93 | def __withdraw(self, event=None): |
| 94 | self.__root.withdraw() |
| 95 | |
| 96 | def deiconify(self, event=None): |
| 97 | self.__root.deiconify() |
| 98 | |
| 99 | def __forceupdate(self, event=None): |
| 100 | self.__sb.update_views_current() |
| 101 | |
| 102 | def update_yourself(self, red, green, blue): |
| 103 | colorname = ColorDB.triplet_to_rrggbb((red, green, blue)) |
| 104 | which = self.__which.get() |
| 105 | if which == 0: |
| 106 | self.__text.configure(foreground=colorname) |
| 107 | elif which == 1: |
| 108 | self.__text.configure(background=colorname) |
| 109 | elif which == 2: |
| 110 | self.__text.configure(selectforeground=colorname) |
| 111 | elif which == 3: |
| 112 | self.__text.configure(selectbackground=colorname) |
| 113 | elif which == 5: |
| 114 | self.__text.configure(insertbackground=colorname) |