|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import sys |
| 3 | +import os |
| 4 | + |
| 5 | +from tkinter import * |
| 6 | +from idlelib.Percolator import Percolator |
| 7 | +from idlelib.ColorDelegator import ColorDelegator |
| 8 | +from idlelib.textView import view_file # TextViewer |
| 9 | +from imp import reload |
| 10 | + |
| 11 | +import turtle |
| 12 | +import time |
| 13 | + |
| 14 | +demo_dir = os.path.dirname(os.path.abspath(__file__)) |
| 15 | + |
| 16 | +STARTUP = 1 |
| 17 | +READY = 2 |
| 18 | +RUNNING = 3 |
| 19 | +DONE = 4 |
| 20 | +EVENTDRIVEN = 5 |
| 21 | + |
| 22 | +menufont = ("Arial", 12, NORMAL) |
| 23 | +btnfont = ("Arial", 12, 'bold') |
| 24 | +txtfont = ('Lucida Console', 8, 'normal') |
| 25 | + |
| 26 | +def getExampleEntries(): |
| 27 | + return [entry[:-3] for entry in os.listdir(demo_dir) if |
| 28 | + entry.endswith(".py") and entry[0] != '_'] |
| 29 | + |
| 30 | +def showDemoHelp(): |
| 31 | + view_file(demo.root, "Help on turtleDemo", |
| 32 | + os.path.join(demo_dir, "demohelp.txt")) |
| 33 | + |
| 34 | +def showAboutDemo(): |
| 35 | + view_file(demo.root, "About turtleDemo", |
| 36 | + os.path.join(demo_dir, "about_turtledemo.txt")) |
| 37 | + |
| 38 | +def showAboutTurtle(): |
| 39 | + view_file(demo.root, "About the new turtle module.", |
| 40 | + os.path.join(demo_dir, "about_turtle.txt")) |
| 41 | + |
| 42 | +class DemoWindow(object): |
| 43 | + |
| 44 | + def __init__(self, filename=None): #, root=None): |
| 45 | + self.root = root = turtle._root = Tk() |
| 46 | + root.wm_protocol("WM_DELETE_WINDOW", self._destroy) |
| 47 | + |
| 48 | + ################# |
| 49 | + self.mBar = Frame(root, relief=RAISED, borderwidth=2) |
| 50 | + self.mBar.pack(fill=X) |
| 51 | + |
| 52 | + self.ExamplesBtn = self.makeLoadDemoMenu() |
| 53 | + self.OptionsBtn = self.makeHelpMenu() |
| 54 | + self.mBar.tk_menuBar(self.ExamplesBtn, self.OptionsBtn) #, QuitBtn) |
| 55 | + |
| 56 | + root.title('Python turtle-graphics examples') |
| 57 | + ################# |
| 58 | + self.left_frame = left_frame = Frame(root) |
| 59 | + self.text_frame = text_frame = Frame(left_frame) |
| 60 | + self.vbar = vbar =Scrollbar(text_frame, name='vbar') |
| 61 | + self.text = text = Text(text_frame, |
| 62 | + name='text', padx=5, wrap='none', |
| 63 | + width=45) |
| 64 | + vbar['command'] = text.yview |
| 65 | + vbar.pack(side=LEFT, fill=Y) |
| 66 | + ##################### |
| 67 | + self.hbar = hbar =Scrollbar(text_frame, name='hbar', orient=HORIZONTAL) |
| 68 | + hbar['command'] = text.xview |
| 69 | + hbar.pack(side=BOTTOM, fill=X) |
| 70 | + ##################### |
| 71 | + text['yscrollcommand'] = vbar.set |
| 72 | + text.config(font=txtfont) |
| 73 | + text.config(xscrollcommand=hbar.set) |
| 74 | + text.pack(side=LEFT, fill=Y, expand=1) |
| 75 | + ##################### |
| 76 | + self.output_lbl = Label(left_frame, height= 1,text=" --- ", bg = "#ddf", |
| 77 | + font = ("Arial", 16, 'normal')) |
| 78 | + self.output_lbl.pack(side=BOTTOM, expand=0, fill=X) |
| 79 | + ##################### |
| 80 | + text_frame.pack(side=LEFT, fill=BOTH, expand=0) |
| 81 | + left_frame.pack(side=LEFT, fill=BOTH, expand=0) |
| 82 | + self.graph_frame = g_frame = Frame(root) |
| 83 | + |
| 84 | + turtle._Screen._root = g_frame |
| 85 | + turtle._Screen._canvas = turtle.ScrolledCanvas(g_frame, 800, 600, 1000, 800) |
| 86 | + #xturtle.Screen._canvas.pack(expand=1, fill="both") |
| 87 | + self.screen = _s_ = turtle.Screen() |
| 88 | +##### |
| 89 | + turtle.TurtleScreen.__init__(_s_, _s_._canvas) |
| 90 | +##### |
| 91 | + self.scanvas = _s_._canvas |
| 92 | + #xturtle.RawTurtle.canvases = [self.scanvas] |
| 93 | + turtle.RawTurtle.screens = [_s_] |
| 94 | + |
| 95 | + self.scanvas.pack(side=TOP, fill=BOTH, expand=1) |
| 96 | + |
| 97 | + self.btn_frame = btn_frame = Frame(g_frame, height=100) |
| 98 | + self.start_btn = Button(btn_frame, text=" START ", font=btnfont, fg = "white", |
| 99 | + disabledforeground = "#fed", command=self.startDemo) |
| 100 | + self.start_btn.pack(side=LEFT, fill=X, expand=1) |
| 101 | + self.stop_btn = Button(btn_frame, text=" STOP ", font=btnfont, fg = "white", |
| 102 | + disabledforeground = "#fed", command = self.stopIt) |
| 103 | + self.stop_btn.pack(side=LEFT, fill=X, expand=1) |
| 104 | + self.clear_btn = Button(btn_frame, text=" CLEAR ", font=btnfont, fg = "white", |
| 105 | + disabledforeground = "#fed", command = self.clearCanvas) |
| 106 | + self.clear_btn.pack(side=LEFT, fill=X, expand=1) |
| 107 | + |
| 108 | + self.btn_frame.pack(side=TOP, fill=BOTH, expand=0) |
| 109 | + self.graph_frame.pack(side=TOP, fill=BOTH, expand=1) |
| 110 | + |
| 111 | + Percolator(text).insertfilter(ColorDelegator()) |
| 112 | + self.dirty = False |
| 113 | + self.exitflag = False |
| 114 | + if filename: |
| 115 | + self.loadfile(filename) |
| 116 | + self.configGUI(NORMAL, DISABLED, DISABLED, DISABLED, |
| 117 | + "Choose example from menu", "black") |
| 118 | + self.state = STARTUP |
| 119 | + |
| 120 | + def _destroy(self): |
| 121 | + self.root.destroy() |
| 122 | + sys.exit() |
| 123 | + |
| 124 | + def configGUI(self, menu, start, stop, clear, txt="", color="blue"): |
| 125 | + self.ExamplesBtn.config(state=menu) |
| 126 | + |
| 127 | + self.start_btn.config(state=start) |
| 128 | + if start == NORMAL: |
| 129 | + self.start_btn.config(bg="#d00") |
| 130 | + else: |
| 131 | + self.start_btn.config(bg="#fca") |
| 132 | + |
| 133 | + self.stop_btn.config(state=stop) |
| 134 | + if stop == NORMAL: |
| 135 | + self.stop_btn.config(bg="#d00") |
| 136 | + else: |
| 137 | + self.stop_btn.config(bg="#fca") |
| 138 | + self.clear_btn.config(state=clear) |
| 139 | + |
| 140 | + self.clear_btn.config(state=clear) |
| 141 | + if clear == NORMAL: |
| 142 | + self.clear_btn.config(bg="#d00") |
| 143 | + else: |
| 144 | + self.clear_btn.config(bg="#fca") |
| 145 | + |
| 146 | + self.output_lbl.config(text=txt, fg=color) |
| 147 | + |
| 148 | + |
| 149 | + def makeLoadDemoMenu(self): |
| 150 | + CmdBtn = Menubutton(self.mBar, text='Examples', underline=0, font=menufont) |
| 151 | + CmdBtn.pack(side=LEFT, padx="2m") |
| 152 | + CmdBtn.menu = Menu(CmdBtn) |
| 153 | + |
| 154 | + for entry in getExampleEntries(): |
| 155 | + def loadexample(x): |
| 156 | + def emit(): |
| 157 | + self.loadfile(x) |
| 158 | + return emit |
| 159 | + CmdBtn.menu.add_command(label=entry, underline=0, |
| 160 | + font=menufont, command=loadexample(entry)) |
| 161 | + |
| 162 | + CmdBtn['menu'] = CmdBtn.menu |
| 163 | + return CmdBtn |
| 164 | + |
| 165 | + def makeHelpMenu(self): |
| 166 | + CmdBtn = Menubutton(self.mBar, text='Help', underline=0, font=menufont) |
| 167 | + CmdBtn.pack(side=LEFT, padx='2m') |
| 168 | + CmdBtn.menu = Menu(CmdBtn) |
| 169 | + |
| 170 | + CmdBtn.menu.add_command(label='About turtle.py', font=menufont, |
| 171 | + command=showAboutTurtle) |
| 172 | + CmdBtn.menu.add_command(label='turtleDemo - Help', font=menufont, |
| 173 | + command=showDemoHelp) |
| 174 | + CmdBtn.menu.add_command(label='About turtleDemo', font=menufont, |
| 175 | + command=showAboutDemo) |
| 176 | + |
| 177 | + CmdBtn['menu'] = CmdBtn.menu |
| 178 | + return CmdBtn |
| 179 | + |
| 180 | + def refreshCanvas(self): |
| 181 | + if not self.dirty: return |
| 182 | + self.screen.clear() |
| 183 | + #self.screen.mode("standard") |
| 184 | + self.dirty=False |
| 185 | + |
| 186 | + def loadfile(self, filename): |
| 187 | + self.refreshCanvas() |
| 188 | + modname = 'turtledemo.' + filename |
| 189 | + __import__(modname) |
| 190 | + self.module = sys.modules[modname] |
| 191 | + with open(self.module.__file__, 'r') as f: |
| 192 | + chars = f.read() |
| 193 | + self.text.delete("1.0", "end") |
| 194 | + self.text.insert("1.0", chars) |
| 195 | + self.root.title(filename + " - a Python turtle graphics example") |
| 196 | + reload(self.module) |
| 197 | + self.configGUI(NORMAL, NORMAL, DISABLED, DISABLED, |
| 198 | + "Press start button", "red") |
| 199 | + self.state = READY |
| 200 | + |
| 201 | + def startDemo(self): |
| 202 | + self.refreshCanvas() |
| 203 | + self.dirty = True |
| 204 | + turtle.TurtleScreen._RUNNING = True |
| 205 | + self.configGUI(DISABLED, DISABLED, NORMAL, DISABLED, |
| 206 | + "demo running...", "black") |
| 207 | + self.screen.clear() |
| 208 | + self.screen.mode("standard") |
| 209 | + self.state = RUNNING |
| 210 | + |
| 211 | + try: |
| 212 | + result = self.module.main() |
| 213 | + if result == "EVENTLOOP": |
| 214 | + self.state = EVENTDRIVEN |
| 215 | + else: |
| 216 | + self.state = DONE |
| 217 | + except turtle.Terminator: |
| 218 | + self.state = DONE |
| 219 | + result = "stopped!" |
| 220 | + if self.state == DONE: |
| 221 | + self.configGUI(NORMAL, NORMAL, DISABLED, NORMAL, |
| 222 | + result) |
| 223 | + elif self.state == EVENTDRIVEN: |
| 224 | + self.exitflag = True |
| 225 | + self.configGUI(DISABLED, DISABLED, NORMAL, DISABLED, |
| 226 | + "use mouse/keys or STOP", "red") |
| 227 | + |
| 228 | + def clearCanvas(self): |
| 229 | + self.refreshCanvas() |
| 230 | + self.screen._delete("all") |
| 231 | + self.scanvas.config(cursor="") |
| 232 | + self.configGUI(NORMAL, NORMAL, DISABLED, DISABLED) |
| 233 | + |
| 234 | + def stopIt(self): |
| 235 | + if self.exitflag: |
| 236 | + self.clearCanvas() |
| 237 | + self.exitflag = False |
| 238 | + self.configGUI(NORMAL, NORMAL, DISABLED, DISABLED, |
| 239 | + "STOPPED!", "red") |
| 240 | + turtle.TurtleScreen._RUNNING = False |
| 241 | + #print "stopIT: exitflag = True" |
| 242 | + else: |
| 243 | + turtle.TurtleScreen._RUNNING = False |
| 244 | + #print "stopIt: exitflag = False" |
| 245 | + |
| 246 | +if __name__ == '__main__': |
| 247 | + demo = DemoWindow() |
| 248 | + RUN = True |
| 249 | + while RUN: |
| 250 | + try: |
| 251 | + #print("ENTERING mainloop") |
| 252 | + demo.root.mainloop() |
| 253 | + except AttributeError: |
| 254 | + #print("AttributeError!- WAIT A MOMENT!") |
| 255 | + time.sleep(0.3) |
| 256 | + print("GOING ON ..") |
| 257 | + demo.ckearCanvas() |
| 258 | + except TypeError: |
| 259 | + demo.screen._delete("all") |
| 260 | + #print("CRASH!!!- WAIT A MOMENT!") |
| 261 | + time.sleep(0.3) |
| 262 | + #print("GOING ON ..") |
| 263 | + demo.clearCanvas() |
| 264 | + except: |
| 265 | + print("BYE!") |
| 266 | + RUN = False |
0 commit comments