tkinter的密钥不工作(“Enter”和“Escape”)

时间:2017-01-31 14:35:19

标签: python python-3.x events turtle-graphics

我在tkinter上为“Enter”和“Escape”获得了糟糕的kysym。输入的想法是当用户按下Enter键时它会在python程序中创建一个新行,当用户按下escape时它会关闭程序我是否用代码搞砸了什么?

这是错误:

Traceback (most recent call last):
  File "C:\Users\User\Desktop\python\turtle\helloworld.py", line 166, in <module>
    onkey(bye, "KP_Escape")
  File "<string>", line 1, in onkey
  File "C:\Python32\lib\turtle.py", line 1393, in onkey
    self._onkeyrelease(fun, key)
  File "C:\Python32\lib\turtle.py", line 687, in _onkeyrelease
    self.cv.bind("<KeyRelease-%s>" % key, eventfun)
  File "C:\Python32\lib\turtle.py", line 416, in bind
    self._canvas.bind(*args, **kwargs)
  File "C:\Python32\lib\tkinter\__init__.py", line 977, in bind
    return self._bind(('bind', self._w), sequence, func, add)
  File "C:\Python32\lib\tkinter\__init__.py", line 932, in _bind
    self.tk.call(what + (sequence, cmd))
_tkinter.TclError: bad event type or keysym "KP_Escape"

这是我的代码:

from turtle import *

speed(10)

# function variables
space_width = int(input("What do you want the space width to be from 1 - 100: "))
letter_height = 50
letter_width = 30
pen_color = input("What color do you want: ")
pen_width = input("What number width doyou want from 1-10: ")


def move_turtle():
    # Go to location for H
    penup ()
    goto(-200, 100)
    pendown()

def move_space():
    penup()
    forward(space_width)
    pendown()

def draw_H():
    # Draw H
    left(90)
    forward(letter_height)
    forward(-letter_height/2)
    right(90)
    forward(letter_height/2)
    left(90)
    forward(letter_height/2)
    forward(-letter_height)
    right(90)
    move_space()



def draw_E():
    # Draw E
    left(90)
    forward(letter_height)

    right(90)
    forward(letter_width)
    forward(-letter_width)

    right(90)
    forward(letter_height / 2)
    left(90)
    forward(letter_width)
    forward(-letter_width)

    right(90)
    forward(letter_height / 2)
    left(90)
    forward(letter_width)
    move_space()

def draw_L():
    # Draw L
    left(90)
    forward(letter_height)
    forward(-letter_height)

    right(90)
    forward(letter_width)
    move_space()

def draw_L2():
    # Draw L
    left(90)
    forward(100)
    forward(-100)

    right(90)
    forward(50)

def draw_O():
    # Draw O
    forward(letter_width)
    left(90)
    forward(letter_height)
    left(90)
    forward(letter_width)
    left(90)
    forward(letter_height)
    left(90)
    forward(letter_width)
    move_space()

def draw_newline():
    # this function will pick up the turtle and move it to a second line
    penup()
    goto(-200, ycor() -100)
    pendown()
def draw_W():
    # We will now draw the W
    right(-90)
    forward(letter_height)
    backward(letter_height)
    left(-90)
    forward(letter_width/2)
    right(-90)
    forward(letter_height)
    backward(letter_height)
    left(-90)
    forward(letter_width/2)
    right(-90)
    forward(letter_height)
    backward(letter_height)
    left(-90)
    move_space()

def draw_R():
    # We will now draw R
    left(90)
    forward(letter_height)
    right(90)
    forward(letter_width/2)
    right(90)
    forward(letter_width/2)
    right(90)
    forward(letter_width/2)
    left(140)
    forward(letter_height)
    left(40)
    move_space()
def HELLO():
    color(pen_color)
    width(pen_width)
    move_turtle()
    draw_H()
    draw_E()
    draw_L()
    draw_L()
    draw_O()

def WORLD():
    draw_newline()
    draw_W()
    draw_O()
    draw_R()
    draw_L()
    draw_O()

#for i in range(10):
#    HELLO()
#    WORLD()
#    width(i)
#    right(i)

move_turtle()

color(pen_color)

listen()

onkey(draw_H, "h")
onkey(draw_E, "e")
onkey(draw_L, "l")
onkey(draw_O, "o")
onkey(draw_W, "w")
onkey(draw_R, "r")
onkey(draw_newline, "KP_Enter")
onkey(bye, "KP_Escape")

exitonclick()

1 个答案:

答案 0 :(得分:1)

而不是:

onkey(draw_newline, "KP_Enter")
onkey(bye, "KP_Escape")

执行:

onkey(draw_newline, "Return")
onkey(bye, "Escape")

案件很重要。在将这些密钥名称发送到tkinter之前,Turtle会将这些密钥名称包装在适当的Tk语法中。