1 | n/a | #!/usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """ |
---|
4 | n/a | ---------------------------------------------- |
---|
5 | n/a | turtleDemo - Help |
---|
6 | n/a | ---------------------------------------------- |
---|
7 | n/a | |
---|
8 | n/a | This document has two sections: |
---|
9 | n/a | |
---|
10 | n/a | (1) How to use the demo viewer |
---|
11 | n/a | (2) How to add your own demos to the demo repository |
---|
12 | n/a | |
---|
13 | n/a | |
---|
14 | n/a | (1) How to use the demo viewer. |
---|
15 | n/a | |
---|
16 | n/a | Select a demoscript from the example menu. |
---|
17 | n/a | The (syntax colored) source code appears in the left |
---|
18 | n/a | source code window. IT CANNOT BE EDITED, but ONLY VIEWED! |
---|
19 | n/a | |
---|
20 | n/a | The demo viewer windows can be resized. The divider between text |
---|
21 | n/a | and canvas can be moved by grabbing it with the mouse. The text font |
---|
22 | n/a | size can be changed from the menu and with Control/Command '-'/'+'. |
---|
23 | n/a | It can also be changed on most systems with Control-mousewheel |
---|
24 | n/a | when the mouse is over the text. |
---|
25 | n/a | |
---|
26 | n/a | Press START button to start the demo. |
---|
27 | n/a | Stop execution by pressing the STOP button. |
---|
28 | n/a | Clear screen by pressing the CLEAR button. |
---|
29 | n/a | Restart by pressing the START button again. |
---|
30 | n/a | |
---|
31 | n/a | SPECIAL demos, such as clock.py are those which run EVENTDRIVEN. |
---|
32 | n/a | |
---|
33 | n/a | Press START button to start the demo. |
---|
34 | n/a | |
---|
35 | n/a | - Until the EVENTLOOP is entered everything works |
---|
36 | n/a | as in an ordinary demo script. |
---|
37 | n/a | |
---|
38 | n/a | - When the EVENTLOOP is entered, you control the |
---|
39 | n/a | application by using the mouse and/or keys (or it's |
---|
40 | n/a | controlled by some timer events) |
---|
41 | n/a | To stop it you can and must press the STOP button. |
---|
42 | n/a | |
---|
43 | n/a | While the EVENTLOOP is running, the examples menu is disabled. |
---|
44 | n/a | |
---|
45 | n/a | - Only after having pressed the STOP button, you may |
---|
46 | n/a | restart it or choose another example script. |
---|
47 | n/a | |
---|
48 | n/a | * * * * * * * * |
---|
49 | n/a | In some rare situations there may occur interferences/conflicts |
---|
50 | n/a | between events concerning the demo script and those concerning the |
---|
51 | n/a | demo-viewer. (They run in the same process.) Strange behaviour may be |
---|
52 | n/a | the consequence and in the worst case you must close and restart the |
---|
53 | n/a | viewer. |
---|
54 | n/a | * * * * * * * * |
---|
55 | n/a | |
---|
56 | n/a | |
---|
57 | n/a | (2) How to add your own demos to the demo repository |
---|
58 | n/a | |
---|
59 | n/a | - Place the file in the same directory as turtledemo/__main__.py |
---|
60 | n/a | IMPORTANT! When imported, the demo should not modify the system |
---|
61 | n/a | by calling functions in other modules, such as sys, tkinter, or |
---|
62 | n/a | turtle. Global variables should be initialized in main(). |
---|
63 | n/a | |
---|
64 | n/a | - The code must contain a main() function which will |
---|
65 | n/a | be executed by the viewer (see provided example scripts). |
---|
66 | n/a | It may return a string which will be displayed in the Label below |
---|
67 | n/a | the source code window (when execution has finished.) |
---|
68 | n/a | |
---|
69 | n/a | - In order to run mydemo.py by itself, such as during development, |
---|
70 | n/a | add the following at the end of the file: |
---|
71 | n/a | |
---|
72 | n/a | if __name__ == '__main__': |
---|
73 | n/a | main() |
---|
74 | n/a | mainloop() # keep window open |
---|
75 | n/a | |
---|
76 | n/a | python -m turtledemo.mydemo # will then run it |
---|
77 | n/a | |
---|
78 | n/a | - If the demo is EVENT DRIVEN, main must return the string |
---|
79 | n/a | "EVENTLOOP". This informs the demo viewer that the script is |
---|
80 | n/a | still running and must be stopped by the user! |
---|
81 | n/a | |
---|
82 | n/a | If an "EVENTLOOP" demo runs by itself, as with clock, which uses |
---|
83 | n/a | ontimer, or minimal_hanoi, which loops by recursion, then the |
---|
84 | n/a | code should catch the turtle.Terminator exception that will be |
---|
85 | n/a | raised when the user presses the STOP button. (Paint is not such |
---|
86 | n/a | a demo; it only acts in response to mouse clicks and movements.) |
---|
87 | n/a | """ |
---|
88 | n/a | import sys |
---|
89 | n/a | import os |
---|
90 | n/a | |
---|
91 | n/a | from tkinter import * |
---|
92 | n/a | from idlelib.colorizer import ColorDelegator, color_config |
---|
93 | n/a | from idlelib.percolator import Percolator |
---|
94 | n/a | from idlelib.textview import view_text |
---|
95 | n/a | from turtledemo import __doc__ as about_turtledemo |
---|
96 | n/a | |
---|
97 | n/a | import turtle |
---|
98 | n/a | |
---|
99 | n/a | demo_dir = os.path.dirname(os.path.abspath(__file__)) |
---|
100 | n/a | darwin = sys.platform == 'darwin' |
---|
101 | n/a | |
---|
102 | n/a | STARTUP = 1 |
---|
103 | n/a | READY = 2 |
---|
104 | n/a | RUNNING = 3 |
---|
105 | n/a | DONE = 4 |
---|
106 | n/a | EVENTDRIVEN = 5 |
---|
107 | n/a | |
---|
108 | n/a | menufont = ("Arial", 12, NORMAL) |
---|
109 | n/a | btnfont = ("Arial", 12, 'bold') |
---|
110 | n/a | txtfont = ['Lucida Console', 10, 'normal'] |
---|
111 | n/a | |
---|
112 | n/a | MINIMUM_FONT_SIZE = 6 |
---|
113 | n/a | MAXIMUM_FONT_SIZE = 100 |
---|
114 | n/a | font_sizes = [8, 9, 10, 11, 12, 14, 18, 20, 22, 24, 30] |
---|
115 | n/a | |
---|
116 | n/a | def getExampleEntries(): |
---|
117 | n/a | return [entry[:-3] for entry in os.listdir(demo_dir) if |
---|
118 | n/a | entry.endswith(".py") and entry[0] != '_'] |
---|
119 | n/a | |
---|
120 | n/a | help_entries = ( # (help_label, help_doc) |
---|
121 | n/a | ('Turtledemo help', __doc__), |
---|
122 | n/a | ('About turtledemo', about_turtledemo), |
---|
123 | n/a | ('About turtle module', turtle.__doc__), |
---|
124 | n/a | ) |
---|
125 | n/a | |
---|
126 | n/a | |
---|
127 | n/a | |
---|
128 | n/a | class DemoWindow(object): |
---|
129 | n/a | |
---|
130 | n/a | def __init__(self, filename=None): |
---|
131 | n/a | self.root = root = turtle._root = Tk() |
---|
132 | n/a | root.title('Python turtle-graphics examples') |
---|
133 | n/a | root.wm_protocol("WM_DELETE_WINDOW", self._destroy) |
---|
134 | n/a | |
---|
135 | n/a | if darwin: |
---|
136 | n/a | import subprocess |
---|
137 | n/a | # Make sure we are the currently activated OS X application |
---|
138 | n/a | # so that our menu bar appears. |
---|
139 | n/a | p = subprocess.Popen( |
---|
140 | n/a | [ |
---|
141 | n/a | 'osascript', |
---|
142 | n/a | '-e', 'tell application "System Events"', |
---|
143 | n/a | '-e', 'set frontmost of the first process whose ' |
---|
144 | n/a | 'unix id is {} to true'.format(os.getpid()), |
---|
145 | n/a | '-e', 'end tell', |
---|
146 | n/a | ], |
---|
147 | n/a | stderr=subprocess.DEVNULL, |
---|
148 | n/a | stdout=subprocess.DEVNULL,) |
---|
149 | n/a | |
---|
150 | n/a | root.grid_rowconfigure(0, weight=1) |
---|
151 | n/a | root.grid_columnconfigure(0, weight=1) |
---|
152 | n/a | root.grid_columnconfigure(1, minsize=90, weight=1) |
---|
153 | n/a | root.grid_columnconfigure(2, minsize=90, weight=1) |
---|
154 | n/a | root.grid_columnconfigure(3, minsize=90, weight=1) |
---|
155 | n/a | |
---|
156 | n/a | self.mBar = Menu(root, relief=RAISED, borderwidth=2) |
---|
157 | n/a | self.mBar.add_cascade(menu=self.makeLoadDemoMenu(self.mBar), |
---|
158 | n/a | label='Examples', underline=0) |
---|
159 | n/a | self.mBar.add_cascade(menu=self.makeFontMenu(self.mBar), |
---|
160 | n/a | label='Fontsize', underline=0) |
---|
161 | n/a | self.mBar.add_cascade(menu=self.makeHelpMenu(self.mBar), |
---|
162 | n/a | label='Help', underline=0) |
---|
163 | n/a | root['menu'] = self.mBar |
---|
164 | n/a | |
---|
165 | n/a | pane = PanedWindow(orient=HORIZONTAL, sashwidth=5, |
---|
166 | n/a | sashrelief=SOLID, bg='#ddd') |
---|
167 | n/a | pane.add(self.makeTextFrame(pane)) |
---|
168 | n/a | pane.add(self.makeGraphFrame(pane)) |
---|
169 | n/a | pane.grid(row=0, columnspan=4, sticky='news') |
---|
170 | n/a | |
---|
171 | n/a | self.output_lbl = Label(root, height= 1, text=" --- ", bg="#ddf", |
---|
172 | n/a | font=("Arial", 16, 'normal'), borderwidth=2, |
---|
173 | n/a | relief=RIDGE) |
---|
174 | n/a | self.start_btn = Button(root, text=" START ", font=btnfont, |
---|
175 | n/a | fg="white", disabledforeground = "#fed", |
---|
176 | n/a | command=self.startDemo) |
---|
177 | n/a | self.stop_btn = Button(root, text=" STOP ", font=btnfont, |
---|
178 | n/a | fg="white", disabledforeground = "#fed", |
---|
179 | n/a | command=self.stopIt) |
---|
180 | n/a | self.clear_btn = Button(root, text=" CLEAR ", font=btnfont, |
---|
181 | n/a | fg="white", disabledforeground="#fed", |
---|
182 | n/a | command = self.clearCanvas) |
---|
183 | n/a | self.output_lbl.grid(row=1, column=0, sticky='news', padx=(0,5)) |
---|
184 | n/a | self.start_btn.grid(row=1, column=1, sticky='ew') |
---|
185 | n/a | self.stop_btn.grid(row=1, column=2, sticky='ew') |
---|
186 | n/a | self.clear_btn.grid(row=1, column=3, sticky='ew') |
---|
187 | n/a | |
---|
188 | n/a | Percolator(self.text).insertfilter(ColorDelegator()) |
---|
189 | n/a | self.dirty = False |
---|
190 | n/a | self.exitflag = False |
---|
191 | n/a | if filename: |
---|
192 | n/a | self.loadfile(filename) |
---|
193 | n/a | self.configGUI(DISABLED, DISABLED, DISABLED, |
---|
194 | n/a | "Choose example from menu", "black") |
---|
195 | n/a | self.state = STARTUP |
---|
196 | n/a | |
---|
197 | n/a | |
---|
198 | n/a | def onResize(self, event): |
---|
199 | n/a | cwidth = self._canvas.winfo_width() |
---|
200 | n/a | cheight = self._canvas.winfo_height() |
---|
201 | n/a | self._canvas.xview_moveto(0.5*(self.canvwidth-cwidth)/self.canvwidth) |
---|
202 | n/a | self._canvas.yview_moveto(0.5*(self.canvheight-cheight)/self.canvheight) |
---|
203 | n/a | |
---|
204 | n/a | def makeTextFrame(self, root): |
---|
205 | n/a | self.text_frame = text_frame = Frame(root) |
---|
206 | n/a | self.text = text = Text(text_frame, name='text', padx=5, |
---|
207 | n/a | wrap='none', width=45) |
---|
208 | n/a | color_config(text) |
---|
209 | n/a | |
---|
210 | n/a | self.vbar = vbar = Scrollbar(text_frame, name='vbar') |
---|
211 | n/a | vbar['command'] = text.yview |
---|
212 | n/a | vbar.pack(side=LEFT, fill=Y) |
---|
213 | n/a | self.hbar = hbar = Scrollbar(text_frame, name='hbar', orient=HORIZONTAL) |
---|
214 | n/a | hbar['command'] = text.xview |
---|
215 | n/a | hbar.pack(side=BOTTOM, fill=X) |
---|
216 | n/a | text['yscrollcommand'] = vbar.set |
---|
217 | n/a | text['xscrollcommand'] = hbar.set |
---|
218 | n/a | |
---|
219 | n/a | text['font'] = tuple(txtfont) |
---|
220 | n/a | shortcut = 'Command' if darwin else 'Control' |
---|
221 | n/a | text.bind_all('<%s-minus>' % shortcut, self.decrease_size) |
---|
222 | n/a | text.bind_all('<%s-underscore>' % shortcut, self.decrease_size) |
---|
223 | n/a | text.bind_all('<%s-equal>' % shortcut, self.increase_size) |
---|
224 | n/a | text.bind_all('<%s-plus>' % shortcut, self.increase_size) |
---|
225 | n/a | text.bind('<Control-MouseWheel>', self.update_mousewheel) |
---|
226 | n/a | text.bind('<Control-Button-4>', self.increase_size) |
---|
227 | n/a | text.bind('<Control-Button-5>', self.decrease_size) |
---|
228 | n/a | |
---|
229 | n/a | text.pack(side=LEFT, fill=BOTH, expand=1) |
---|
230 | n/a | return text_frame |
---|
231 | n/a | |
---|
232 | n/a | def makeGraphFrame(self, root): |
---|
233 | n/a | turtle._Screen._root = root |
---|
234 | n/a | self.canvwidth = 1000 |
---|
235 | n/a | self.canvheight = 800 |
---|
236 | n/a | turtle._Screen._canvas = self._canvas = canvas = turtle.ScrolledCanvas( |
---|
237 | n/a | root, 800, 600, self.canvwidth, self.canvheight) |
---|
238 | n/a | canvas.adjustScrolls() |
---|
239 | n/a | canvas._rootwindow.bind('<Configure>', self.onResize) |
---|
240 | n/a | canvas._canvas['borderwidth'] = 0 |
---|
241 | n/a | |
---|
242 | n/a | self.screen = _s_ = turtle.Screen() |
---|
243 | n/a | turtle.TurtleScreen.__init__(_s_, _s_._canvas) |
---|
244 | n/a | self.scanvas = _s_._canvas |
---|
245 | n/a | turtle.RawTurtle.screens = [_s_] |
---|
246 | n/a | return canvas |
---|
247 | n/a | |
---|
248 | n/a | def set_txtsize(self, size): |
---|
249 | n/a | txtfont[1] = size |
---|
250 | n/a | self.text['font'] = tuple(txtfont) |
---|
251 | n/a | self.output_lbl['text'] = 'Font size %d' % size |
---|
252 | n/a | |
---|
253 | n/a | def decrease_size(self, dummy=None): |
---|
254 | n/a | self.set_txtsize(max(txtfont[1] - 1, MINIMUM_FONT_SIZE)) |
---|
255 | n/a | return 'break' |
---|
256 | n/a | |
---|
257 | n/a | def increase_size(self, dummy=None): |
---|
258 | n/a | self.set_txtsize(min(txtfont[1] + 1, MAXIMUM_FONT_SIZE)) |
---|
259 | n/a | return 'break' |
---|
260 | n/a | |
---|
261 | n/a | def update_mousewheel(self, event): |
---|
262 | n/a | # For wheel up, event.delte = 120 on Windows, -1 on darwin. |
---|
263 | n/a | # X-11 sends Control-Button-4 event instead. |
---|
264 | n/a | if (event.delta < 0) == (not darwin): |
---|
265 | n/a | return self.decrease_size() |
---|
266 | n/a | else: |
---|
267 | n/a | return self.increase_size() |
---|
268 | n/a | |
---|
269 | n/a | def configGUI(self, start, stop, clear, txt="", color="blue"): |
---|
270 | n/a | self.start_btn.config(state=start, |
---|
271 | n/a | bg="#d00" if start == NORMAL else "#fca") |
---|
272 | n/a | self.stop_btn.config(state=stop, |
---|
273 | n/a | bg="#d00" if stop == NORMAL else "#fca") |
---|
274 | n/a | self.clear_btn.config(state=clear, |
---|
275 | n/a | bg="#d00" if clear == NORMAL else"#fca") |
---|
276 | n/a | self.output_lbl.config(text=txt, fg=color) |
---|
277 | n/a | |
---|
278 | n/a | def makeLoadDemoMenu(self, master): |
---|
279 | n/a | menu = Menu(master) |
---|
280 | n/a | |
---|
281 | n/a | for entry in getExampleEntries(): |
---|
282 | n/a | def load(entry=entry): |
---|
283 | n/a | self.loadfile(entry) |
---|
284 | n/a | menu.add_command(label=entry, underline=0, |
---|
285 | n/a | font=menufont, command=load) |
---|
286 | n/a | return menu |
---|
287 | n/a | |
---|
288 | n/a | def makeFontMenu(self, master): |
---|
289 | n/a | menu = Menu(master) |
---|
290 | n/a | menu.add_command(label="Decrease (C-'-')", command=self.decrease_size, |
---|
291 | n/a | font=menufont) |
---|
292 | n/a | menu.add_command(label="Increase (C-'+')", command=self.increase_size, |
---|
293 | n/a | font=menufont) |
---|
294 | n/a | menu.add_separator() |
---|
295 | n/a | |
---|
296 | n/a | for size in font_sizes: |
---|
297 | n/a | def resize(size=size): |
---|
298 | n/a | self.set_txtsize(size) |
---|
299 | n/a | menu.add_command(label=str(size), underline=0, |
---|
300 | n/a | font=menufont, command=resize) |
---|
301 | n/a | return menu |
---|
302 | n/a | |
---|
303 | n/a | def makeHelpMenu(self, master): |
---|
304 | n/a | menu = Menu(master) |
---|
305 | n/a | |
---|
306 | n/a | for help_label, help_file in help_entries: |
---|
307 | n/a | def show(help_label=help_label, help_file=help_file): |
---|
308 | n/a | view_text(self.root, help_label, help_file) |
---|
309 | n/a | menu.add_command(label=help_label, font=menufont, command=show) |
---|
310 | n/a | return menu |
---|
311 | n/a | |
---|
312 | n/a | def refreshCanvas(self): |
---|
313 | n/a | if self.dirty: |
---|
314 | n/a | self.screen.clear() |
---|
315 | n/a | self.dirty=False |
---|
316 | n/a | |
---|
317 | n/a | def loadfile(self, filename): |
---|
318 | n/a | self.clearCanvas() |
---|
319 | n/a | turtle.TurtleScreen._RUNNING = False |
---|
320 | n/a | modname = 'turtledemo.' + filename |
---|
321 | n/a | __import__(modname) |
---|
322 | n/a | self.module = sys.modules[modname] |
---|
323 | n/a | with open(self.module.__file__, 'r') as f: |
---|
324 | n/a | chars = f.read() |
---|
325 | n/a | self.text.delete("1.0", "end") |
---|
326 | n/a | self.text.insert("1.0", chars) |
---|
327 | n/a | self.root.title(filename + " - a Python turtle graphics example") |
---|
328 | n/a | self.configGUI(NORMAL, DISABLED, DISABLED, |
---|
329 | n/a | "Press start button", "red") |
---|
330 | n/a | self.state = READY |
---|
331 | n/a | |
---|
332 | n/a | def startDemo(self): |
---|
333 | n/a | self.refreshCanvas() |
---|
334 | n/a | self.dirty = True |
---|
335 | n/a | turtle.TurtleScreen._RUNNING = True |
---|
336 | n/a | self.configGUI(DISABLED, NORMAL, DISABLED, |
---|
337 | n/a | "demo running...", "black") |
---|
338 | n/a | self.screen.clear() |
---|
339 | n/a | self.screen.mode("standard") |
---|
340 | n/a | self.state = RUNNING |
---|
341 | n/a | |
---|
342 | n/a | try: |
---|
343 | n/a | result = self.module.main() |
---|
344 | n/a | if result == "EVENTLOOP": |
---|
345 | n/a | self.state = EVENTDRIVEN |
---|
346 | n/a | else: |
---|
347 | n/a | self.state = DONE |
---|
348 | n/a | except turtle.Terminator: |
---|
349 | n/a | if self.root is None: |
---|
350 | n/a | return |
---|
351 | n/a | self.state = DONE |
---|
352 | n/a | result = "stopped!" |
---|
353 | n/a | if self.state == DONE: |
---|
354 | n/a | self.configGUI(NORMAL, DISABLED, NORMAL, |
---|
355 | n/a | result) |
---|
356 | n/a | elif self.state == EVENTDRIVEN: |
---|
357 | n/a | self.exitflag = True |
---|
358 | n/a | self.configGUI(DISABLED, NORMAL, DISABLED, |
---|
359 | n/a | "use mouse/keys or STOP", "red") |
---|
360 | n/a | |
---|
361 | n/a | def clearCanvas(self): |
---|
362 | n/a | self.refreshCanvas() |
---|
363 | n/a | self.screen._delete("all") |
---|
364 | n/a | self.scanvas.config(cursor="") |
---|
365 | n/a | self.configGUI(NORMAL, DISABLED, DISABLED) |
---|
366 | n/a | |
---|
367 | n/a | def stopIt(self): |
---|
368 | n/a | if self.exitflag: |
---|
369 | n/a | self.clearCanvas() |
---|
370 | n/a | self.exitflag = False |
---|
371 | n/a | self.configGUI(NORMAL, DISABLED, DISABLED, |
---|
372 | n/a | "STOPPED!", "red") |
---|
373 | n/a | turtle.TurtleScreen._RUNNING = False |
---|
374 | n/a | |
---|
375 | n/a | def _destroy(self): |
---|
376 | n/a | turtle.TurtleScreen._RUNNING = False |
---|
377 | n/a | self.root.destroy() |
---|
378 | n/a | self.root = None |
---|
379 | n/a | |
---|
380 | n/a | |
---|
381 | n/a | def main(): |
---|
382 | n/a | demo = DemoWindow() |
---|
383 | n/a | demo.root.mainloop() |
---|
384 | n/a | |
---|
385 | n/a | if __name__ == '__main__': |
---|
386 | n/a | main() |
---|