2010-03-11 18:33:25 -04:00
|
|
|
#!/usr/bin/env python
|
2008-06-04 03:29:55 -03:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
|
|
|
from Tkinter import *
|
|
|
|
from idlelib.Percolator import Percolator
|
|
|
|
from idlelib.ColorDelegator import ColorDelegator
|
2014-06-22 03:32:26 -03:00
|
|
|
from idlelib.textView import view_file
|
2008-06-04 03:29:55 -03:00
|
|
|
|
|
|
|
import turtle
|
|
|
|
import time
|
|
|
|
|
2014-06-22 03:32:26 -03:00
|
|
|
demo_dir = os.getcwd()
|
|
|
|
if "turtleDemo.py" not in os.listdir(demo_dir):
|
|
|
|
print "Directory of turtleDemo must be current working directory!"
|
|
|
|
print "But in your case this is", demo_dir
|
|
|
|
sys.exit()
|
|
|
|
|
2008-06-04 03:29:55 -03:00
|
|
|
STARTUP = 1
|
|
|
|
READY = 2
|
|
|
|
RUNNING = 3
|
|
|
|
DONE = 4
|
|
|
|
EVENTDRIVEN = 5
|
|
|
|
|
|
|
|
menufont = ("Arial", 12, NORMAL)
|
|
|
|
btnfont = ("Arial", 12, 'bold')
|
|
|
|
txtfont = ('Lucida Console', 8, 'normal')
|
|
|
|
|
|
|
|
def getExampleEntries():
|
2014-06-22 03:32:26 -03:00
|
|
|
entries1 = [entry for entry in os.listdir(demo_dir) if
|
2008-06-04 03:29:55 -03:00
|
|
|
entry.startswith("tdemo_") and
|
|
|
|
not entry.endswith(".pyc")]
|
|
|
|
entries2 = []
|
|
|
|
for entry in entries1:
|
|
|
|
if entry.endswith(".py"):
|
|
|
|
entries2.append(entry)
|
|
|
|
else:
|
2014-06-22 03:32:26 -03:00
|
|
|
path = os.path.join(demo_dir, entry)
|
2008-06-04 03:29:55 -03:00
|
|
|
sys.path.append(path)
|
|
|
|
subdir = [entry]
|
|
|
|
scripts = [script for script in os.listdir(path) if
|
|
|
|
script.startswith("tdemo_") and
|
|
|
|
script.endswith(".py")]
|
|
|
|
entries2.append(subdir+scripts)
|
|
|
|
return entries2
|
|
|
|
|
|
|
|
def showDemoHelp():
|
2014-06-22 03:32:26 -03:00
|
|
|
view_file(demo.root, "Help on turtleDemo",
|
|
|
|
os.path.join(demo_dir, "demohelp.txt"))
|
2008-06-04 03:29:55 -03:00
|
|
|
|
|
|
|
def showAboutDemo():
|
2014-06-22 03:32:26 -03:00
|
|
|
view_file(demo.root, "About turtleDemo",
|
|
|
|
os.path.join(demo_dir, "about_turtledemo.txt"))
|
2008-06-04 03:29:55 -03:00
|
|
|
|
|
|
|
def showAboutTurtle():
|
2014-06-22 03:32:26 -03:00
|
|
|
view_file(demo.root, "About the new turtle module.",
|
|
|
|
os.path.join(demo_dir, "about_turtle.txt"))
|
2008-06-04 03:29:55 -03:00
|
|
|
|
|
|
|
class DemoWindow(object):
|
|
|
|
|
2014-07-23 16:01:07 -03:00
|
|
|
def __init__(self, filename=None):
|
2008-06-04 03:29:55 -03:00
|
|
|
self.root = root = turtle._root = Tk()
|
2014-07-23 16:01:07 -03:00
|
|
|
root.title('Python turtle-graphics examples')
|
2008-06-04 03:29:55 -03:00
|
|
|
root.wm_protocol("WM_DELETE_WINDOW", self._destroy)
|
|
|
|
|
2014-07-23 16:01:07 -03:00
|
|
|
root.grid_rowconfigure(1, weight=1)
|
|
|
|
root.grid_columnconfigure(0, weight=1)
|
|
|
|
root.grid_columnconfigure(1, minsize=90, weight=1)
|
|
|
|
root.grid_columnconfigure(2, minsize=90, weight=1)
|
|
|
|
root.grid_columnconfigure(3, minsize=90, weight=1)
|
2008-06-04 03:29:55 -03:00
|
|
|
|
2014-07-23 16:01:07 -03:00
|
|
|
self.mBar = Frame(root, relief=RAISED, borderwidth=2)
|
2008-06-04 03:29:55 -03:00
|
|
|
self.ExamplesBtn = self.makeLoadDemoMenu()
|
|
|
|
self.OptionsBtn = self.makeHelpMenu()
|
2014-07-23 16:01:07 -03:00
|
|
|
self.mBar.tk_menuBar(self.ExamplesBtn, self.OptionsBtn)
|
|
|
|
self.mBar.grid(row=0, columnspan=4, sticky='news')
|
|
|
|
|
|
|
|
pane = PanedWindow(orient=HORIZONTAL, sashwidth=5,
|
|
|
|
sashrelief=SOLID, bg='#ddd')
|
|
|
|
pane.add(self.makeTextFrame(pane))
|
|
|
|
pane.add(self.makeGraphFrame(pane))
|
|
|
|
pane.grid(row=1, columnspan=4, sticky='news')
|
|
|
|
|
|
|
|
self.output_lbl = Label(root, height= 1, text=" --- ", bg="#ddf",
|
|
|
|
font=("Arial", 16, 'normal'), borderwidth=2,
|
|
|
|
relief=RIDGE)
|
|
|
|
self.start_btn = Button(root, text=" START ", font=btnfont,
|
|
|
|
fg="white", disabledforeground = "#fed",
|
|
|
|
command=self.startDemo)
|
|
|
|
self.stop_btn = Button(root, text=" STOP ", font=btnfont,
|
|
|
|
fg="white", disabledforeground = "#fed",
|
|
|
|
command=self.stopIt)
|
|
|
|
self.clear_btn = Button(root, text=" CLEAR ", font=btnfont,
|
|
|
|
fg="white", disabledforeground="#fed",
|
|
|
|
command = self.clearCanvas)
|
|
|
|
self.output_lbl.grid(row=2, column=0, sticky='news', padx=(0,5))
|
|
|
|
self.start_btn.grid(row=2, column=1, sticky='ew')
|
|
|
|
self.stop_btn.grid(row=2, column=2, sticky='ew')
|
|
|
|
self.clear_btn.grid(row=2, column=3, sticky='ew')
|
|
|
|
|
|
|
|
Percolator(self.text).insertfilter(ColorDelegator())
|
|
|
|
self.dirty = False
|
|
|
|
self.exitflag = False
|
|
|
|
if filename:
|
|
|
|
self.loadfile(filename)
|
|
|
|
self.configGUI(NORMAL, DISABLED, DISABLED, DISABLED,
|
|
|
|
"Choose example from menu", "black")
|
|
|
|
self.state = STARTUP
|
2008-06-04 03:29:55 -03:00
|
|
|
|
2014-07-23 16:01:07 -03:00
|
|
|
|
|
|
|
def onResize(self, event):
|
|
|
|
cwidth = self._canvas.winfo_width()
|
|
|
|
cheight = self._canvas.winfo_height()
|
|
|
|
self._canvas.xview_moveto(0.5*(self.canvwidth-cwidth)/self.canvwidth)
|
|
|
|
self._canvas.yview_moveto(0.5*(self.canvheight-cheight)/self.canvheight)
|
|
|
|
|
|
|
|
def makeTextFrame(self, root):
|
|
|
|
self.text_frame = text_frame = Frame(root)
|
|
|
|
self.text = text = Text(text_frame, name='text', padx=5,
|
|
|
|
wrap='none', width=45)
|
|
|
|
|
|
|
|
self.vbar = vbar = Scrollbar(text_frame, name='vbar')
|
2008-06-04 03:29:55 -03:00
|
|
|
vbar['command'] = text.yview
|
|
|
|
vbar.pack(side=LEFT, fill=Y)
|
2014-07-23 16:01:07 -03:00
|
|
|
self.hbar = hbar = Scrollbar(text_frame, name='hbar', orient=HORIZONTAL)
|
2008-06-04 03:29:55 -03:00
|
|
|
hbar['command'] = text.xview
|
|
|
|
hbar.pack(side=BOTTOM, fill=X)
|
2014-07-23 16:01:07 -03:00
|
|
|
|
|
|
|
text['font'] = txtfont
|
2008-06-04 03:29:55 -03:00
|
|
|
text['yscrollcommand'] = vbar.set
|
2014-07-23 16:01:07 -03:00
|
|
|
text['xscrollcommand'] = hbar.set
|
|
|
|
text.pack(side=LEFT, fill=BOTH, expand=1)
|
|
|
|
return text_frame
|
|
|
|
|
|
|
|
def makeGraphFrame(self, root):
|
|
|
|
turtle._Screen._root = root
|
|
|
|
self.canvwidth = 1000
|
|
|
|
self.canvheight = 800
|
|
|
|
turtle._Screen._canvas = self._canvas = canvas = turtle.ScrolledCanvas(
|
|
|
|
root, 800, 600, self.canvwidth, self.canvheight)
|
|
|
|
canvas.adjustScrolls()
|
|
|
|
canvas._rootwindow.bind('<Configure>', self.onResize)
|
|
|
|
canvas._canvas['borderwidth'] = 0
|
|
|
|
|
2008-06-04 03:29:55 -03:00
|
|
|
self.screen = _s_ = turtle.Screen()
|
2009-09-28 15:29:28 -03:00
|
|
|
turtle.TurtleScreen.__init__(_s_, _s_._canvas)
|
2008-06-04 03:29:55 -03:00
|
|
|
self.scanvas = _s_._canvas
|
|
|
|
turtle.RawTurtle.screens = [_s_]
|
2014-07-23 16:01:07 -03:00
|
|
|
return canvas
|
2008-06-04 03:29:55 -03:00
|
|
|
|
|
|
|
def configGUI(self, menu, start, stop, clear, txt="", color="blue"):
|
|
|
|
self.ExamplesBtn.config(state=menu)
|
|
|
|
|
|
|
|
self.start_btn.config(state=start)
|
|
|
|
if start==NORMAL:
|
|
|
|
self.start_btn.config(bg="#d00")
|
|
|
|
else:
|
|
|
|
self.start_btn.config(bg="#fca")
|
|
|
|
|
|
|
|
self.stop_btn.config(state=stop)
|
|
|
|
if stop==NORMAL:
|
|
|
|
self.stop_btn.config(bg="#d00")
|
|
|
|
else:
|
|
|
|
self.stop_btn.config(bg="#fca")
|
|
|
|
self.clear_btn.config(state=clear)
|
|
|
|
|
|
|
|
self.clear_btn.config(state=clear)
|
|
|
|
if clear==NORMAL:
|
|
|
|
self.clear_btn.config(bg="#d00")
|
|
|
|
else:
|
|
|
|
self.clear_btn.config(bg="#fca")
|
|
|
|
|
|
|
|
self.output_lbl.config(text=txt, fg=color)
|
|
|
|
|
|
|
|
def makeLoadDemoMenu(self):
|
2014-07-23 16:01:07 -03:00
|
|
|
CmdBtn = Menubutton(self.mBar, text='Examples',
|
|
|
|
underline=0, font=menufont)
|
2008-06-04 03:29:55 -03:00
|
|
|
CmdBtn.pack(side=LEFT, padx="2m")
|
|
|
|
CmdBtn.menu = Menu(CmdBtn)
|
|
|
|
|
|
|
|
for entry in getExampleEntries():
|
|
|
|
def loadexample(x):
|
|
|
|
def emit():
|
|
|
|
self.loadfile(x)
|
|
|
|
return emit
|
|
|
|
if isinstance(entry,str):
|
2014-07-23 16:01:07 -03:00
|
|
|
CmdBtn.menu.add_command(label=entry[6:-3], underline=0,
|
|
|
|
font=menufont,
|
2008-06-04 03:29:55 -03:00
|
|
|
command=loadexample(entry))
|
|
|
|
else:
|
|
|
|
_dir, entries = entry[0], entry[1:]
|
|
|
|
CmdBtn.menu.choices = Menu(CmdBtn.menu)
|
|
|
|
for e in entries:
|
2014-07-23 16:01:07 -03:00
|
|
|
CmdBtn.menu.choices.add_command(
|
|
|
|
label=e[6:-3], underline=0, font=menufont,
|
|
|
|
command = loadexample(os.path.join(_dir,e)))
|
|
|
|
CmdBtn.menu.add_cascade(
|
|
|
|
label=_dir[6:], menu = CmdBtn.menu.choices, font=menufont)
|
2008-06-04 03:29:55 -03:00
|
|
|
|
|
|
|
CmdBtn['menu'] = CmdBtn.menu
|
|
|
|
return CmdBtn
|
|
|
|
|
|
|
|
|
|
|
|
def makeHelpMenu(self):
|
|
|
|
CmdBtn = Menubutton(self.mBar, text='Help', underline=0, font = menufont)
|
|
|
|
CmdBtn.pack(side=LEFT, padx='2m')
|
|
|
|
CmdBtn.menu = Menu(CmdBtn)
|
|
|
|
|
2014-07-23 16:01:07 -03:00
|
|
|
CmdBtn.menu.add_command(label='About turtle.py', font=menufont,
|
|
|
|
command=showAboutTurtle)
|
|
|
|
CmdBtn.menu.add_command(label='turtleDemo - Help', font=menufont,
|
|
|
|
command=showDemoHelp)
|
|
|
|
CmdBtn.menu.add_command(label='About turtleDemo', font=menufont,
|
|
|
|
command=showAboutDemo)
|
2008-06-04 03:29:55 -03:00
|
|
|
|
|
|
|
CmdBtn['menu'] = CmdBtn.menu
|
|
|
|
return CmdBtn
|
|
|
|
|
|
|
|
def refreshCanvas(self):
|
|
|
|
if not self.dirty: return
|
|
|
|
self.screen.clear()
|
|
|
|
self.dirty=False
|
|
|
|
|
|
|
|
def loadfile(self,filename):
|
|
|
|
self.refreshCanvas()
|
|
|
|
if os.path.exists(filename) and not os.path.isdir(filename):
|
|
|
|
# load and display file text
|
|
|
|
f = open(filename,'r')
|
|
|
|
chars = f.read()
|
|
|
|
f.close()
|
|
|
|
self.text.delete("1.0", "end")
|
|
|
|
self.text.insert("1.0",chars)
|
|
|
|
direc, fname = os.path.split(filename)
|
2010-05-24 18:37:54 -03:00
|
|
|
self.root.title(fname[6:-3]+" - a Python turtle graphics example")
|
2008-06-04 03:29:55 -03:00
|
|
|
self.module = __import__(fname[:-3])
|
|
|
|
reload(self.module)
|
|
|
|
self.configGUI(NORMAL, NORMAL, DISABLED, DISABLED,
|
|
|
|
"Press start button", "red")
|
|
|
|
self.state = READY
|
|
|
|
|
|
|
|
def startDemo(self):
|
|
|
|
self.refreshCanvas()
|
|
|
|
self.dirty = True
|
|
|
|
turtle.TurtleScreen._RUNNING = True
|
|
|
|
self.configGUI(DISABLED, DISABLED, NORMAL, DISABLED,
|
|
|
|
"demo running...", "black")
|
|
|
|
self.screen.clear()
|
|
|
|
self.screen.mode("standard")
|
|
|
|
self.state = RUNNING
|
|
|
|
|
|
|
|
try:
|
|
|
|
result = self.module.main()
|
|
|
|
if result == "EVENTLOOP":
|
|
|
|
self.state = EVENTDRIVEN
|
|
|
|
else:
|
|
|
|
self.state = DONE
|
|
|
|
except turtle.Terminator:
|
|
|
|
self.state = DONE
|
|
|
|
result = "stopped!"
|
|
|
|
if self.state == DONE:
|
|
|
|
self.configGUI(NORMAL, NORMAL, DISABLED, NORMAL,
|
|
|
|
result)
|
|
|
|
elif self.state == EVENTDRIVEN:
|
|
|
|
self.exitflag = True
|
|
|
|
self.configGUI(DISABLED, DISABLED, NORMAL, DISABLED,
|
|
|
|
"use mouse/keys or STOP", "red")
|
|
|
|
|
|
|
|
def clearCanvas(self):
|
|
|
|
self.refreshCanvas()
|
|
|
|
self.scanvas.config(cursor="")
|
|
|
|
self.configGUI(NORMAL, NORMAL, DISABLED, DISABLED)
|
|
|
|
|
|
|
|
def stopIt(self):
|
|
|
|
if self.exitflag:
|
|
|
|
self.clearCanvas()
|
|
|
|
self.exitflag = False
|
|
|
|
self.configGUI(NORMAL, NORMAL, DISABLED, DISABLED,
|
|
|
|
"STOPPED!", "red")
|
|
|
|
turtle.TurtleScreen._RUNNING = False
|
|
|
|
else:
|
|
|
|
turtle.TurtleScreen._RUNNING = False
|
2014-07-23 16:01:07 -03:00
|
|
|
|
|
|
|
def _destroy(self):
|
|
|
|
self.root.destroy()
|
|
|
|
sys.exit()
|
2008-06-04 03:29:55 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
demo = DemoWindow()
|
|
|
|
RUN = True
|
|
|
|
while RUN:
|
|
|
|
try:
|
|
|
|
print "ENTERING mainloop"
|
|
|
|
demo.root.mainloop()
|
|
|
|
except AttributeError:
|
|
|
|
print "CRASH!!!- WAIT A MOMENT!"
|
|
|
|
time.sleep(0.3)
|
|
|
|
print "GOING ON .."
|
|
|
|
demo.refreshCanvas()
|
|
|
|
## time.sleep(1)
|
|
|
|
except:
|
|
|
|
RUN = FALSE
|