Thread: Tkinter Canvas

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

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Tkinter Canvas


    Hello I am trying to make a simple program that will move the turtle specified by the keyboard input and I would like to have a list of controls on the right. This is my code so far
    Code:
    from turtle import *
    import tkinter
    
    key1 = 1
    
    Controls = tkinter.Tk()
    Controls.title("Color")
    Controls.minsize(100, 100)
    cv = tkinter.Canvas(Controls, bg = "gray")
    cv.pack(side = tkinter.LEFT)
    t = RawTurtle(cv)
    t.pensize(3)
    
    w = tkinter.Label(Controls, text="Arrow Keys - Move")
    w.pack()
    
    def k1():
        global key1
        key1 +=1
        while key1 % 2 == 0:
            t.pencolor(random.choice(colors))
            t.forward(5)
    
    onkey(k1, "Up")
    
    listen()
    mainloop()
    I don't think I am implementing the Canvas correctly.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    451
    Rep Power
    32
    Your code modified to print which key was pressed, Up, Down, etc. And a tutorial on the Canvas module
    Code:
    def to_key(event):
        print event.keysym
    
    key1 = 1
    
    Controls = tkinter.Tk()
    Controls.title("Color")
    Controls.minsize(100, 100)
    Controls.bind("<KeyPress>", to_key)
    #cv = tkinter.Canvas(Controls, bg = "gray")
    #cv.pack(side = tkinter.LEFT)
    #t = RawTurtle(cv)
    #t.pensize(3)
    
    w = tkinter.Label(Controls, text="Arrow Keys - Move")
    w.pack()
    
    def k1():
        global key1
        key1 +=1
        while key1 % 2 == 0:
            t.pencolor(random.choice(colors))
            t.forward(5)
    
    #onkey(k1, "Up")
    
    #listen()
    Controls.mainloop()

IMN logo majestic logo threadwatch logo seochat tools logo