2017-01-14 9 views
0

私のコードは、エラーメッセージが表示されてから離れて何も起こらないとき、私がしようとすると、キーの1つを押すまで、茶色の四角が空白のウィンドウに表示されます。何か案は?私は、右矢印キーを押しPython/Tkinterイベント引数の問題

from tkinter import * 

x, y, a, b = 50, 50, 100, 100 
d = None 

vel_dic = { 
    "Left": ("left", -4, 0), 
    "Right": ("right", 4, 0), 
    "Down": ("down", 0, 4), 
    "Up": ("up", 0, -4)} 
class Sprite: 
    def __init__(self): 
     self.move 
     self.x, self.y, self.a, self.b = 50, 50, 100, 100 
     self.d = 0 
     self.canvas = Canvas(tk, height = 600, width = 600) 
     self.canvas.grid(row=0, column=0, sticky = W) 
     self.coord = [self.x, self.y, self.a, self.b] 
     self.shape = self.canvas.create_rectangle(*self.coord, outline = "#cc9900", fill = "#cc9900") 
    def move(): 
     if self.direction != 0: 
      self.canvas.move(self.rect, self.xv, self.yv) 
     tk.after(33, move) 
    def on_keypress(event): 
     self.direction, self.xv, self.yv = vel_dic[event.keysym] 
    def on_keyrelease(event): 
     self.direction = 0 

tk = Tk() 
tk.geometry("600x600") 

sprite1 = Sprite() 

tk.bind_all('<KeyPress>', sprite1.on_keypress) 
tk.bind_all('<KeyRelease>', sprite1.on_keyrelease) 

エラーメッセージ:

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\idlelib\run.py", line 137, in main 
    seq, request = rpc.request_queue.get(block=True, timeout=0.05) 
    File "C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\queue.py", line 172, in get 
    raise Empty 
queue.Empty 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__ 
    return self.func(*args) 
TypeError: on_keypress() takes 1 positional argument but 2 were given 
Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\idlelib\run.py", line 137, in main 
    seq, request = rpc.request_queue.get(block=True, timeout=0.05) 
    File "C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\queue.py", line 172, in get 
    raise Empty 
queue.Empty 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__ 
    return self.func(*args) 
TypeError: on_keyrelease() takes 1 positional argument but 2 were given 

答えて

1

オブジェクトの内部で機能するように呼び出すとき、self(オブジェクトのインスタンス)は、最初の引数として送信されます。最も一般的な方法として、staticmethodの方法で元に戻すことができますが、その場合はそれを探す必要はありません。

エラーは、このselfパラメータと通常のeventパラメータをインタープリタが送信したが、そのメソッドは1つのパラメータしか取得できないため、処理できないことを示します。

すべての機能を確認してくださいは、他のパラメータに加えて、最初のパラメータとしてself(またはあなたがinstのように選択した任意の名前)を取得します。

def on_keyrelease(self, event): 

同じことがmoveon_keypressのために行きます。

+0

ありがとうございました。しかし、コードはまだ四角形を移動しません。あなたは問題を見ることができますか? –

+0

クラスがtkinter 'Frame'または' Widget'を継承せず、プロパティ名が壊れています。 oop tkinterのこのスレッドを見てください - http://stackoverflow.com/a/17470842/6575931 – Uriel

+0

実際には '' move() ''メソッドを実際に呼び出すことはありませんので、もちろん移動しません。また、変数の名前を '' self.d''と '' self.direction''、 '' self.shape''、 '' self.rect''という名前に統一していません。 – jasonharper