Thread: Turtle Graphics

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep 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()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    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 and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0
    Yeah it works for me I am running python 3.2.3.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    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.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0
    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.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    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.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0
    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!

IMN logo majestic logo threadwatch logo seochat tools logo