2016-05-01 14 views
-2

ページアップキーが押されたときにPythonのカメのペンサイズを増加させるプログラムにします。TypeError:+ =: 'method'と 'int'のためのサポートされていないオペランドタイプ

#!/usr/bin/env python3 
import turtle 
wn=turtle.Screen() 
wn.title('Control using first letter of desired action') 
py=turtle.Turtle() 
py.color('blue') 
size=1 
def front(): 
    py.fd(90) 
def back(): 
    py.bk(90) 
def right(): 
    py.rt(45) 
def left(): 
    py.lt(45) 
def increasize(): 
    global size 
    while size>=1 and size<=20: 
     py.pensize+=1 
def decreasize(): 
    global size 
    while size>=1 and size<=20: 
     py.pensize-=1 
wn.onkey(front,'w') 
wn.onkey(back,'s') 
wn.onkey(right,'d') 
wn.onkey(left,'a') 
wn.onkey(increasize,'Prior') 
wn.onkey(decreasize,'Next') 
wn.listen() 
wn.mainloop() 

しかし、それはエラーを与える: 私は次のことを試してみました。完全なトレースバック:

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "c:\program files\python3\lib\tkinter\__init__.py", line 1549, in __call__ 
    return self.func(*args) 
    File "c:\program files\python3\lib\turtle.py", line 686, in eventfun 
    fun() 
    File "D:\Python\draw_straight_key.py", line 19, in increasize 
    py.pensize+=1 
TypeError: unsupported operand type(s) for +=: 'method' and 'int' 
Exception in Tkinter callback 
Traceback (most recent call last): 
    File "c:\program files\python3\lib\tkinter\__init__.py", line 1549, in __call__ 
    return self.func(*args) 
    File "c:\program files\python3\lib\turtle.py", line 686, in eventfun 
    fun() 
    File "D:\Python\draw_straight_key.py", line 23, in decreasize 
    py.pensize-=1 
TypeError: unsupported operand type(s) for -=: 'method' and 'int' 
+0

質問の本文にコードを入力してください。 – MattDMo

+0

エラーまたはトレースバックの**フルテキスト**も投稿してください。 – MattDMo

+0

pensizeはメソッドであり、変数ではありません。 [docs](https://docs.python.org/2/library/turtle.html#turtle.pensize)を見て、自分で修正してください。 – Natecat

答えて

0

あなたが呼び出す必要があります新しいサイズのメソッドをpensizeします。メソッド参照を使用すると、サイズは常にワンサイズ(20)になりたい場合を除き、その後、if文

にwhileループを変更、increasize

また
size += 1 
py.pensize(size) 

で、例えば

に追加することはできません

while size>=1 and size<=20: 
+0

ありがとう、ありがとう。 –

関連する問題