4 Replies - 10021 Views - Last Post: 13 April 2012 - 08:30 PM Rate Topic: -----

#1 ruddiger7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-April 12

Drawing with arrow keys - turtle module.

Posted 11 April 2012 - 12:30 AM

Hello pythoneers. I am rather new to Python and am simply trying to make a little
program where i can merely draw on the screen using the arrow keys and turtle module. I have looked
on different tutorials and borrowed bits and pieces although i do not even know if
cturtle and turtle modules are the same. As you can probably tell this code is incomplete
and I would appreciate any help in getting it to function.

Thanks!


This is what I have so far

from turtle import * # Initiates turtle graphics
width(7) #sets etch a sketch width




class Draw:
    def Draw.__init__(self):
        super().__init__()
        pendown()
        self.color("black")
        self.pensize(5)
        self.speed(0)
        self.distance = 7
        self.onKey(self.fd,"Up")
        self.onKey(self.bw,"Down")
        self.onKey(self.left,"Left")
        self.onKey(self.right,"Right")
        self.onKey(self.reset ,"c")
        self.listen()
        self.main()
   

    def fwd(self):
        self.forward(self.distance)

    def bkwd(self):
        self.backward(self.distance)

    def left(self):
        self.left(self.distance)

    def right(self):
        self.right(self.distance)


    def main(self):
        cTurtle.mainloop()

This post has been edited by atraub: 11 April 2012 - 06:04 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Drawing with arrow keys - turtle module.

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Drawing with arrow keys - turtle module.

Posted 11 April 2012 - 06:09 PM

I believe the turtle module uses tkinter. In that case, you should be able to capture keypresses relatively easily. Here's an example I found for listening to key presses using tkinter, let me know if this helps:

    
# KeyLogger_tk2.py
# show a character key when pressed without using Enter key
# hide the Tkinter GUI window, only console shows
    try:
    # Python2
        import Tkinter as tk
    except ImportError:
    # Python3
        import tkinter as tk
    def key(event):
        """shows key or tk code for the key"""
        if event.keysym == 'Escape':
            root.destroy()
        if event.char == event.keysym:
            # normal number and letter characters
            print( 'Normal Key %r' % event.char )
        elif len(event.char) == 1:
            # charcters like []/.,><#$ also Return and ctrl/key
            print( 'Punctuation Key %r (%r)' % (event.keysym, event.char) )
        else:
            # f1 to f12, shift keys, caps lock, Home, End, Delete ...
            print( 'Special Key %r' % event.keysym )
root = tk.Tk()
print( "Press a key (Escape key to exit):" )
root.bind_all('<Key>', key)
# don't show the tk window
root.withdraw()
root.mainloop()


This post has been edited by atraub: 11 April 2012 - 06:14 PM

Was This Post Helpful? 0
  • +
  • -

#3 ruddiger7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-April 12

Re: Drawing with arrow keys - turtle module.

Posted 13 April 2012 - 02:07 AM

Not quite sure if it helped to be honest. I consulted documentation and fixed it up a bit.
class draw:
    def __init__(self, col, x, y):
        Turtle.__init__(self)
        self.color(col)
        self.shape("circle")
        self.goto(0, 0)
        pd()
        width(7)
    
        
        
def U():
    fd(8)
    onkey(U, "Up")
    listen()
    
def L():
    lt(8)
    onkey(L, "Left")
    listen()

def R():
    rt(8)
    onkey(R, "Right")
    listen()

def D():
    bk(8)
    onkey(D, "Down")
    listen()


I just still cant get it to draw is all. :(

This post has been edited by atraub: 13 April 2012 - 06:34 PM
Reason for edit:: added code tags

Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Drawing with arrow keys - turtle module.

Posted 13 April 2012 - 07:10 PM

Maybe this will help ;)
import turtle
wn = turtle.Screen()

def setup(col, x, y, w, s, shape): 
    turtle.up()
    turtle.goto(x,y)
    turtle.width(w)
    turtle.turtlesize(s)
    turtle.color(col)
    turtle.shape(shape)
    turtle.down()
    wn.onkey(up, "Up")
    wn.onkey(left, "Left")
    wn.onkey(right, "Right")
    wn.onkey(back, "Down")
    wn.onkey(quitTurtles, "Escape")
    wn.listen()
    wn.mainloop()
    
#Event handlers
def up():
    turtle.fd(45)

def left():
    turtle.lt(45)

def right():
    turtle.rt(45)

def back():
    turtle.bk(45)
 
def quitTurtles():
    wn.bye()
    
setup("blue",-200,200,2,2,"turtle")



The code is pretty self documenting, let me know if you have a question.

I did some extra stuff for fun
Spoiler

This post has been edited by atraub: 13 April 2012 - 07:58 PM

Was This Post Helpful? 1
  • +
  • -

#5 ruddiger7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-April 12

Re: Drawing with arrow keys - turtle module.

Posted 13 April 2012 - 08:30 PM

Thankyou goodsir. This is what I was looking to do.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1