Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 5th, 2012, 11:35 PM
sean2012 sean2012 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 sean2012 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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()



Reply With Quote
  #2  
Old December 6th, 2012, 07:04 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,359 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 43 m 13 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old December 6th, 2012, 01:54 PM
sean2012 sean2012 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 sean2012 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old December 6th, 2012, 02:22 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,359 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 43 m 13 sec
Reputation Power: 383
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.

Reply With Quote
  #5  
Old December 6th, 2012, 11:52 PM
sean2012 sean2012 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 sean2012 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old December 7th, 2012, 12:08 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,359 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 43 m 13 sec
Reputation Power: 383
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.

Reply With Quote
  #7  
Old December 7th, 2012, 03:54 AM
sean2012 sean2012 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 sean2012 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Turtle Graphics

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap