2017-01-31 48 views
0

インターネット上でチェックされていて、この問題の明確な解決策はありませんでした。私は "Enter"と "Escape"のためにtkinterに悪いキッズムを得ています。入力のアイデアは、ユーザーが入力を押すとPythonプログラムに新しい行が作成され、ユーザーがエスケープを押すとプログラムが終了します。助けてください。ここKeysymのtkinterが鳴っていない( "Enter"と "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

の代わりに:

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

行います

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

ケースが重要です。タートルは、これらのキー名を適切なTk構文でラップしてからtkinterに送信します。

関連する問題