Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 1 | """Main Pynche (Pythonically Natural Color and Hue Editor) widget. |
Barry Warsaw | eb61fbd | 1998-10-02 16:06:27 +0000 | [diff] [blame] | 2 | |
| 3 | This window provides the basic decorations, primarily including the menubar. |
| 4 | It is used to bring up other windows. |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 5 | """ |
| 6 | |
| 7 | from Tkinter import * |
Barry Warsaw | a9b4558 | 1998-09-29 20:03:53 +0000 | [diff] [blame] | 8 | import tkMessageBox |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 9 | |
| 10 | # Milliseconds between interrupt checks |
| 11 | KEEPALIVE_TIMER = 500 |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 12 | |
Barry Warsaw | 552ac17 | 1998-02-17 22:25:23 +0000 | [diff] [blame] | 13 | |
| 14 | |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 15 | class PyncheWidget: |
Barry Warsaw | 86daeb7 | 1998-10-01 16:46:16 +0000 | [diff] [blame] | 16 | def __init__(self, version, switchboard): |
| 17 | self.__sb = switchboard |
| 18 | self.__version = version |
| 19 | self.__textwin = None |
Barry Warsaw | bd36d6e | 1998-10-02 16:05:48 +0000 | [diff] [blame] | 20 | self.__listwin = None |
Barry Warsaw | 9044b8e | 1998-10-05 21:14:46 +0000 | [diff] [blame] | 21 | self.__detailswin = None |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 22 | # |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 23 | # Is there already a default root for Tk, say because we're running |
| 24 | # under Guido's IDE? :-) Two conditions say no, either the import |
| 25 | # fails or _default_root is None. |
| 26 | tkroot = None |
| 27 | try: |
| 28 | from Tkinter import _default_root |
| 29 | tkroot = self.__tkroot = _default_root |
| 30 | except ImportError: |
| 31 | pass |
| 32 | if not tkroot: |
| 33 | tkroot = self.__tkroot = Tk(className='Pynche') |
| 34 | # but this isn't our top level widget, so make it invisible |
| 35 | tkroot.withdraw() |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 36 | # create the menubar |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 37 | menubar = self.__menubar = Menu(tkroot) |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 38 | # |
| 39 | # File menu |
| 40 | # |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 41 | filemenu = self.__filemenu = Menu(menubar, tearoff=0) |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 42 | filemenu.add_command(label='Quit', |
Barry Warsaw | e7f4a47 | 1998-10-06 19:50:33 +0000 | [diff] [blame] | 43 | command=self.__quit, |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 44 | accelerator='Alt-Q', |
| 45 | underline=0) |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 46 | # |
Barry Warsaw | 86daeb7 | 1998-10-01 16:46:16 +0000 | [diff] [blame] | 47 | # View menu |
| 48 | # |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 49 | viewmenu = Menu(menubar, tearoff=0) |
Barry Warsaw | 86daeb7 | 1998-10-01 16:46:16 +0000 | [diff] [blame] | 50 | viewmenu.add_command(label='Text Window...', |
| 51 | command=self.__popup_text, |
| 52 | underline=0) |
Barry Warsaw | bd36d6e | 1998-10-02 16:05:48 +0000 | [diff] [blame] | 53 | viewmenu.add_command(label='Color List Window...', |
| 54 | command=self.__popup_listwin, |
| 55 | underline=0) |
Barry Warsaw | 9044b8e | 1998-10-05 21:14:46 +0000 | [diff] [blame] | 56 | viewmenu.add_command(label='Details Window...', |
| 57 | command=self.__popup_details, |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 58 | underline=0) |
| 59 | # |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 60 | # Help menu |
| 61 | # |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 62 | helpmenu = Menu(menubar, name='help', tearoff=0) |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 63 | helpmenu.add_command(label='About...', |
| 64 | command=self.__popup_about, |
| 65 | underline=0) |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 66 | # |
| 67 | # Tie them all together |
| 68 | # |
| 69 | menubar.add_cascade(label='File', |
| 70 | menu=filemenu, |
| 71 | underline=0) |
| 72 | menubar.add_cascade(label='View', |
| 73 | menu=viewmenu, |
| 74 | underline=0) |
| 75 | menubar.add_cascade(label='Help', |
| 76 | menu=helpmenu, |
| 77 | underline=0) |
| 78 | |
| 79 | # now create the top level window |
| 80 | root = self.__root = Toplevel(tkroot, class_='Pynche', menu=menubar) |
Barry Warsaw | e7f4a47 | 1998-10-06 19:50:33 +0000 | [diff] [blame] | 81 | root.protocol('WM_DELETE_WINDOW', self.__quit) |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 82 | root.title('Pynche %s' % version) |
| 83 | root.iconname('Pynche') |
| 84 | root.tk.createtimerhandler(KEEPALIVE_TIMER, self.__keepalive) |
Barry Warsaw | e7f4a47 | 1998-10-06 19:50:33 +0000 | [diff] [blame] | 85 | root.bind('<Alt-q>', self.__quit) |
| 86 | root.bind('<Alt-Q>', self.__quit) |
| 87 | |
| 88 | def __quit(self, event=None): |
| 89 | self.__root.quit() |
Barry Warsaw | 552ac17 | 1998-02-17 22:25:23 +0000 | [diff] [blame] | 90 | |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 91 | def __keepalive(self): |
| 92 | # Exercise the Python interpreter regularly so keyboard interrupts get |
| 93 | # through. |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 94 | self.__tkroot.tk.createtimerhandler(KEEPALIVE_TIMER, self.__keepalive) |
Barry Warsaw | 552ac17 | 1998-02-17 22:25:23 +0000 | [diff] [blame] | 95 | |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 96 | def start(self): |
| 97 | self.__keepalive() |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 98 | self.__tkroot.mainloop() |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 99 | |
| 100 | def parent(self): |
| 101 | return self.__root |
Barry Warsaw | f67575d | 1998-03-10 00:17:01 +0000 | [diff] [blame] | 102 | |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 103 | def __popup_about(self, event=None): |
Barry Warsaw | ad3a67c | 1998-10-06 18:52:59 +0000 | [diff] [blame] | 104 | from pynche import __version__ |
| 105 | tkMessageBox.showinfo('About Pynche ' + __version__, |
Barry Warsaw | a9b4558 | 1998-09-29 20:03:53 +0000 | [diff] [blame] | 106 | '''\ |
Barry Warsaw | ad3a67c | 1998-10-06 18:52:59 +0000 | [diff] [blame] | 107 | Pynche %s |
| 108 | The PYthonically Natural |
| 109 | Color and Hue Editor |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 110 | |
Barry Warsaw | 3886e3a | 1998-10-06 20:49:20 +0000 | [diff] [blame^] | 111 | Copyright (C) 1998 CNRI |
Barry Warsaw | a9b4558 | 1998-09-29 20:03:53 +0000 | [diff] [blame] | 112 | All rights reserved |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 113 | |
Barry Warsaw | a9b4558 | 1998-09-29 20:03:53 +0000 | [diff] [blame] | 114 | For information about Pynche |
| 115 | contact: Barry A. Warsaw |
Barry Warsaw | ad3a67c | 1998-10-06 18:52:59 +0000 | [diff] [blame] | 116 | email: bwarsaw@python.org''' % __version__) |
Barry Warsaw | 86daeb7 | 1998-10-01 16:46:16 +0000 | [diff] [blame] | 117 | |
| 118 | def __popup_text(self, event=None): |
| 119 | if not self.__textwin: |
| 120 | from TextViewer import TextViewer |
| 121 | self.__textwin = TextViewer(self.__sb, self.__root) |
| 122 | self.__sb.add_view(self.__textwin) |
| 123 | self.__textwin.deiconify() |
Barry Warsaw | bd36d6e | 1998-10-02 16:05:48 +0000 | [diff] [blame] | 124 | |
| 125 | def __popup_listwin(self, event=None): |
| 126 | if not self.__listwin: |
| 127 | from ListViewer import ListViewer |
| 128 | self.__listwin = ListViewer(self.__sb, self.__root) |
| 129 | self.__sb.add_view(self.__listwin) |
| 130 | self.__listwin.deiconify() |
Barry Warsaw | 9044b8e | 1998-10-05 21:14:46 +0000 | [diff] [blame] | 131 | |
| 132 | def __popup_details(self, event=None): |
| 133 | if not self.__detailswin: |
| 134 | from DetailsViewer import DetailsViewer |
| 135 | self.__detailswin = DetailsViewer(self.__sb, self.__root) |
| 136 | self.__sb.add_view(self.__detailswin) |
| 137 | self.__detailswin.deiconify() |