The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Turtle Graphics
Discuss Turtle Graphics in the Python Programming forum on Dev Shed. Turtle Graphics Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 5th, 2012, 11:35 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 16 m 19 sec
Reputation Power: 0
|
|
|
Turtle Graphics
Hello, I'm a freshman in college and I am taking programming I. We just started python so I have about half a semester of python education. My final assignment was to create a project with what we have learned. So my project is using turtle graphics and tkinter. I have already submitted the project, but I was going to post the code here and ask for ways to optimize it or maybe some tips for the future. Thanks for any tips!!
Code:
from turtle import *
from random import *
from tkinter import *
winX = 800
winY = 600
key1 = 1
speed = 5
k = 0
rb = 0
bg = 1
x = 0
turn = 0
ps = 3
setup(winX, winY)
title("Color")
bgcolor("gray")
showturtle()
pensize(ps)
colors = ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"]
scolor = "Red"
Controls = Tk()
Controls.title("Controls")
Controls.minsize(180, 100)
w = Label(Controls, text="Arrow Keys - Move")
w.pack()
w = Label(Controls, text="U - Pen Up")
w.pack()
w = Label(Controls, text="D - Pen Down")
w.pack()
w = Label(Controls, text="Z - Move to center")
w.pack()
w = Label(Controls, text="C - Clear")
w.pack()
w = Label(Controls, text="S - Stop")
w.pack()
w = Label(Controls, text="Q - Quit")
w.pack()
def stop():
global key1
key1 = 1
def rng():
global winX, winY
if xcor() < (-1 * (winX / 2)) + 10:
stop()
setheading(0)
if xcor() > (winX / 2) - 20:
stop()
setheading(180)
if ycor() < (-1 * (winY / 2)) + 10:
stop()
setheading(90)
if ycor() > (winY / 2) - 10:
stop()
setheading(270)
def selcol():
global scolor, k, rb
rb = 0
k += 1
if k > 5:
k = 0
scolor = colors[k]
but6["text"] = "Color:", scolor
def selcol2():
global scolor, k, rb
rb = 0
k -= 1
if k > 5:
k = 0
scolor = colors[k]
but6["text"] = "Color:", scolor
def rainbow():
global rb
if rb == 0:
rb = 1
but6["text"] = "Color: Rainbow"
else:
rb = 0
but6["text"] = "Color:", scolor
def k1():
global key1, speed, scolor, rb
stop()
key1 +=1
while key1 % 2 == 0:
if rb == 0:
pencolor(scolor)
forward(speed)
rng()
else:
pencolor(choice(colors))
forward(speed)
rng()
def k2():
stop()
global key1, speed
key1 += 1
while key1 % 2 == 0:
if rb == 0:
pencolor(scolor)
backward(speed)
rng()
else:
pencolor(choice(colors))
backward(speed)
rng()
def k3():
stop()
if turn % 2 == 1:
left(15)
else:
global key1
key1 += 1
while key1 % 2 == 0:
left(4)
def k4():
stop()
if turn % 2 == 1:
right(15)
else:
global key1
key1 += 1
while key1 % 2 == 0:
right(4)
def k5():
pu()
stop()
setpos(0,0)
pd()
def k6():
bye()
def k7():
stop()
def k8():
pu()
clear()
stop()
setpos(0,0)
pd()
def k9():
pu()
def k10():
pd()
def k11():
global speed
if speed < 10:
speed += 1
but3["text"] = "Speed:", speed
def k12():
global speed
if speed > 1:
speed -= 1
but3["text"] = "Speed:", speed
def k13():
global speed, rb, x
x += 1
while x % 2 == 1 and speed == 10 and rb == 1:
bgcolor(choice(colors))
shape("turtle")
bgcolor("gray")
shape("classic")
def k14():
global turn
turn += 1
if turn % 2 == 0:
but8["text"] = "Toggled Turn"
else:
but8["text"] = "Untoggled Turn"
def k15():
global ps
ps += 1
if ps > 15:
ps = 15
but11["text"] = "Size:", ps
pensize(ps)
def k16():
global ps
ps -= 1
if ps < 2:
ps = 1
but11["text"] = "Size:", ps
pensize(ps)
but1 = Button(Controls, text="+", command=k11)
but1.pack(side="right")
but2 = Button(Controls, text="-", command=k12)
but2.pack(side="left")
but4 = Button(Controls, text=">", command=selcol)
but4.pack(side="right")
but5 = Button(Controls, text="<", command=selcol2)
but5.pack(side="left")
but3 = Button(Controls, relief=FLAT)
but3["text"] = "Speed:", speed
but3.pack(side="bottom")
but6 = Button(Controls, relief=FLAT)
but6["text"] = "Color:", scolor
but6.pack(side="bottom")
but7 = Button(Controls, text = "Rainbow", command=rainbow)
but7.pack(side="bottom")
but8 = Button(Controls, text = "Toggled Turn", command=k14)
but8.pack(side="bottom")
but10 = Button(Controls, text = "+", command=k15)
but10.pack(side="right")
but9 = Button(Controls, text = "-", command=k16)
but9.pack(side="left")
but11 = Button(Controls, relief=FLAT)
but11["text"] = "Size:", ps
but11.pack(side="bottom")
onkey(k1, "Up")
onkey(k2, "Down")
onkey(k3, "Left")
onkey(k4, "Right")
onkey(k5, "z")
onkey(k6, "q")
onkey(k7, "s")
onkey(k8, "c")
onkey(k9, "u")
onkey(k10, "d")
onkey(k11, "=")
onkey(k12, "-")
onkey(k13, "r")
listen()
mainloop()
|

December 6th, 2012, 07:04 AM
|
 |
Contributing User
|
|
|
|
Are you sure the program works?
Code:
$ python3 p.py
Traceback (most recent call last):
File "p.py", line 259, in <module>
onkey(k11, "=")
File "<string>", line 1, in onkey
File "/usr/lib/python3.2/turtle.py", line 1387, in onkey
self._onkeyrelease(fun, key)
File "/usr/lib/python3.2/turtle.py", line 687, in _onkeyrelease
self.cv.bind("<KeyRelease-%s>" % key, eventfun)
File "/usr/lib/python3.2/turtle.py", line 416, in bind
self._canvas.bind(*args, **kwargs)
File "/usr/lib/python3.2/tkinter/__init__.py", line 995, in bind
return self._bind(('bind', self._w), sequence, func, add)
File "/usr/lib/python3.2/tkinter/__init__.py", line 950, in _bind
self.tk.call(what + (sequence, cmd))
_tkinter.TclError: bad event type or keysym "="
__________________
[code] Code tags[/code] are essential for python code!
|

December 6th, 2012, 01:54 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 16 m 19 sec
Reputation Power: 0
|
|
|
Yeah it works for me I am running python 3.2.3.
|

December 6th, 2012, 02:22 PM
|
 |
Contributing User
|
|
|
|
|
Copy and paste your code as it displays in this forum into a new file, invoke python3 on that file, and see if you have similar troubles.
|

December 6th, 2012, 11:52 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 16 m 19 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by b49P23TIvg Copy and paste your code as it displays in this forum into a new file, invoke python3 on that file, and see if you have similar troubles. |
Yeah copied and pasted from the forum into a new python document. Works on my laptop running python 3.3 and at my school running 3.2.3.
|

December 7th, 2012, 12:08 AM
|
 |
Contributing User
|
|
|
|
|
Strange. The canvas displays for an instant before the error occurs. Anyway, I like the etch-a-sketch keyboard control idea. Definitely has fun appeal.
|

December 7th, 2012, 03:54 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 16 m 19 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by b49P23TIvg Strange. The canvas displays for an instant before the error occurs. Anyway, I like the etch-a-sketch keyboard control idea. Definitely has fun appeal. |
Thank You! And could the error be from your keyboard? I'm not a pro debugger but I saw in the error you posted this (k11, "=") maybe if you changed "=" to another key?
Update:
So I tested this code on Ubuntu 12.10 and I received the same error that you did. To fix it change the equals sign in line 174 to another key and it should work fine!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|