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