2017-01-31 8 views
0

私はここに掲載されたサンプルスクリプト変更しようとしている:あなたがキャンバスの周りのオブジェクトをクリックしてドラッグすることができ、元のスクリプトどのようにボタンを使ってアイテムをキャンバス上でPythonで移動しますか?

board-drawing code to move an oval

を。 TypeError:button_move()は2つの引数(1が指定されています)をとります 2番目の引数は何かを理解できません。 - command=self.button_move - >def button_move(self, event)

しかしbindeventはそうあなたがこれを必要として同じ機能を実行

import Tkinter as tk 

class Example(tk.Frame): 
    '''Illustrate how to drag items on a Tkinter canvas''' 

    def __init__(self, parent): 
     tk.Frame.__init__(self, parent) 

     # create a canvas 
     self.canvas = tk.Canvas(width=400, height=400) 
     self.canvas.pack(fill="both", expand=True) 

     # this data is used to keep track of an 
     # item being dragged 
     self._drag_data = {"x": 0, "y": 0, "item": None} 

     # create a couple of movable objects 
     self._create_token((100, 100), "white") 
     self._create_token((200, 100), "black") 

     # add bindings for clicking, dragging and releasing over 
     # any object with the "token" tag 
     self.canvas.tag_bind("token", "<ButtonPress-1>", self.on_token_press) 
     self.canvas.tag_bind("token", "<ButtonRelease-1>", self.on_token_release) 
     self.canvas.tag_bind("token", "<B1-Motion>", self.on_token_motion) 
#----------------------------------------------------------------------------- 
     self.canvas.button2 = tk.Button(self.canvas, text="Button Test", 
           command=self.button_move) 
     self.canvas.button2.config(bg="cyan",fg="black") 
     self.canvas.button2.pack(side='top') 

     self.canvas.tag_bind("button2", "<ButtonPress-1>", self.button_move) 
#----------------------------------------------------------------------------- 
    def _create_token(self, coord, color): 
     '''Create a token at the given coordinate in the given color''' 
     (x,y) = coord 
     self.canvas.create_oval(x-25, y-25, x+25, y+25, 
           outline=color, fill=color, tags="token") 

    def on_token_press(self, event): 
     '''Begining drag of an object''' 
     # record the item and its location 
     self._drag_data["item"] = self.canvas.find_closest(event.x, event.y)[0] 
     self._drag_data["x"] = event.x 
     self._drag_data["y"] = event.y 

    def on_token_release(self, event): 
     '''End drag of an object''' 
     # reset the drag information 
     self._drag_data["item"] = None 
     self._drag_data["x"] = 0 
     self._drag_data["y"] = 0 

    def on_token_motion(self, event): 
     '''Handle dragging of an object''' 
     # compute how much the mouse has moved 
     delta_x = event.x - self._drag_data["x"] 
     delta_y = event.y - self._drag_data["y"] 
     # move the object the appropriate amount 
     self.canvas.move(self._drag_data["item"], delta_x, delta_y) 
     # record the new position 
     self._drag_data["x"] = event.x 
     self._drag_data["y"] = event.y 
#----------------------------------------------------------------------------- 
    def button_move(self, event): 
     '''Handle dragging of an object''' 
     # set movement amount 
     delta_x = 15 
     delta_y = 15 
     # move the object the appropriate amount 
     #self.canvas.move(self._drag_data["item"], delta_x, delta_y) 
     self.canvas.move(self._drag_data[1], delta_x, delta_y) 
     # record the new position 
     self._drag_data["x"] = event.x 
     self._drag_data["y"] = event.y 
#----------------------------------------------------------------------------- 
if __name__ == "__main__": 
    root = tk.Tk() 
    Example(root).pack(fill="both", expand=True) 
    root.mainloop() 

答えて

1

command=eventせずに機能を実行しますが、あなたはeventが期待する機能を割り当てます。ここでは

は、変更したスクリプトです機能は eventです。

策:イベントの使用のデフォルト値は、すなわちNone

def button_move(self, event=None): 

しかし、あなたはここでcommand=

+0

を「イベントを=なし」キーではありません。私はまた、 "button_move"関数で "self._drag_data [" x "] = event.x"と "self._drag_data [" y "] = event.y"をコメントアウトしました。 –

0

で何を実行する場合は、この機能ではevent.xevent.yを使用することはできませんが更新されますfurasのアドバイスを使用してコード:

import Tkinter as tk 

class Example(tk.Frame): 
    '''Illustrate how to drag items on a Tkinter canvas''' 

    def __init__(self, parent): 
     tk.Frame.__init__(self, parent) 

     # create a canvas 
     self.canvas = tk.Canvas(width=400, height=400) 
     self.canvas.pack(fill="both", expand=True) 

     # this data is used to keep track of an 
     # item being dragged 
     self._drag_data = {"x": 0, "y": 0, "item": None} 

     # create a couple of movable objects 
     self._create_token((100, 100), "white") 
     self._create_token((200, 100), "black") 

     # add bindings for clicking, dragging and releasing over 
     # any object with the "token" tag 
     self.canvas.tag_bind("token", "<ButtonPress-1>", self.on_token_press) 
     self.canvas.tag_bind("token", "<ButtonRelease-1>", self.on_token_release) 
     self.canvas.tag_bind("token", "<B1-Motion>", self.on_token_motion) 
#----------------------------------------------------------------------------- 
     self.canvas.button2 = tk.Button(self.canvas, text="Button Test", 
           command=self.button_move) 
     self.canvas.button2.config(bg="cyan",fg="black") 
     self.canvas.button2.pack(side='top') 

     self.canvas.tag_bind("button2", "<ButtonPress-1>", self.button_move) 
#----------------------------------------------------------------------------- 
    def _create_token(self, coord, color): 
     '''Create a token at the given coordinate in the given color''' 
     (x,y) = coord 
     self.canvas.create_oval(x-25, y-25, x+25, y+25, 
           outline=color, fill=color, tags="token") 

    def on_token_press(self, event): 
     '''Begining drag of an object''' 
     # record the item and its location 
     self._drag_data["item"] = self.canvas.find_closest(event.x, event.y)[0] 
     self._drag_data["x"] = event.x 
     self._drag_data["y"] = event.y 

    def on_token_release(self, event): 
     '''End drag of an object''' 
     # reset the drag information 
     self._drag_data["item"] = None 
     self._drag_data["x"] = 0 
     self._drag_data["y"] = 0 

    def on_token_motion(self, event): 
     '''Handle dragging of an object''' 
     # compute how much the mouse has moved 
     delta_x = event.x - self._drag_data["x"] 
     delta_y = event.y - self._drag_data["y"] 
     # move the object the appropriate amount 
     self.canvas.move(self._drag_data["item"], delta_x, delta_y) 
     # record the new position 
     self._drag_data["x"] = event.x 
     self._drag_data["y"] = event.y 
#----------------------------------------------------------------------------- 
    def button_move(self, event=None): 
     '''Handle dragging of an object''' 
     # set movement amount 
     delta_x = 15 
     delta_y = 15 
     # move the object the appropriate amount 
     #self.canvas.move(self._drag_data["item"], delta_x, delta_y) 
     #self.canvas.move(self._drag_data[1], delta_x, delta_y) 
     self.canvas.move(1, delta_x, delta_y) # moves item 1, when cursor is over token 
     # record the new position 
     #self._drag_data["x"] = event.x 
     #self._drag_data["y"] = event.y 
#----------------------------------------------------------------------------- 
if __name__ == "__main__": 
    root = tk.Tk() 
    Example(root).pack(fill="both", expand=True) 
    root.mainloop() 
+0

よく質問を編集し、問題の新しいコードを追加しました。 – furas

0

そのようなボタンのcommandとしてbutton_move()としてコールバックを把握、それを呼び出してたときに、それに与えられた唯一の引数は、あなたがこのようにそれを呼び出した場合と同様に、self次のとおりです。

self.button_move() 

しかしbutton_move()event引数を期待するので、あなたが得ます次のエラー:

TypeError: button_move() takes exactly 2 arguments (1 given). 

だからbutton_move()定義からeventパラメータは最初のステップになります取り除きます。また

eventbutton_move()スコープにはならないであろうから、あなたは次の2行削除する必要があります。

self._drag_data["x"] = event.x 
self._drag_data["y"] = event.y 

最後に、あなたがself.canvas.move()ラインの順に移動したいキャンバスオブジェクトからIDを必要とします働く私は、最後にクリックされたキャンバスオブジェクトからidがself._drag_data["item"]内に保存されるように、あなたは、on_token_release()からライン

self._drag_data["item"] = None 

を削除示唆しています。 canvas.move()に電話をかける前に、self._drag_data["item"]がNoneでないかどうかを確認するだけです。すべてのこれらの変更を適用する

は、次のコードを生成:追加

import Tkinter as tk 

class Example(tk.Frame): 
    '''Illustrate how to drag items on a Tkinter canvas''' 

    def __init__(self, parent): 
     tk.Frame.__init__(self, parent) 

     # create a canvas 
     self.canvas = tk.Canvas(width=400, height=400) 
     self.canvas.pack(fill="both", expand=True) 

     # this data is used to keep track of an 
     # item being dragged 
     self._drag_data = {"x": 0, "y": 0, "item": None} 

     # create a couple of movable objects 
     self._create_token((100, 100), "white") 
     self._create_token((200, 100), "black") 

     # add bindings for clicking, dragging and releasing over 
     # any object with the "token" tag 
     self.canvas.tag_bind("token", "<ButtonPress-1>", self.on_token_press) 
     self.canvas.tag_bind("token", "<ButtonRelease-1>", self.on_token_release) 
     self.canvas.tag_bind("token", "<B1-Motion>", self.on_token_motion) 

     self.canvas.button2 = tk.Button(self.canvas, text="Button Test", 
           command=self.button_move) 
     self.canvas.button2.config(bg="cyan",fg="black") 
     self.canvas.button2.pack(side='top') 

     self.canvas.tag_bind("button2", "<ButtonPress-1>", self.button_move) 

    def _create_token(self, coord, color): 
     '''Create a token at the given coordinate in the given color''' 
     (x,y) = coord 
     self.canvas.create_oval(x-25, y-25, x+25, y+25, 
           outline=color, fill=color, tags="token") 

    def on_token_press(self, event): 
     '''Begining drag of an object''' 
     # record the item and its location 
     self._drag_data["item"] = self.canvas.find_closest(event.x, event.y)[0] 
     self._drag_data["x"] = event.x 
     self._drag_data["y"] = event.y 

    def on_token_release(self, event): 
     '''End drag of an object''' 
     # reset the drag information 
     self._drag_data["x"] = 0 
     self._drag_data["y"] = 0 

    def on_token_motion(self, event): 
     '''Handle dragging of an object''' 
     # compute how much the mouse has moved 
     delta_x = event.x - self._drag_data["x"] 
     delta_y = event.y - self._drag_data["y"] 
     # move the object the appropriate amount 
     self.canvas.move(self._drag_data["item"], delta_x, delta_y) 
     # record the new position 
     self._drag_data["x"] = event.x 
     self._drag_data["y"] = event.y 

    def button_move(self): 
     if not self._drag_data["item"]: 
      return 

     '''Handle dragging of an object''' 
     # set movement amount 
     delta_x = 15 
     delta_y = 15 
     # move the object the appropriate amount 
     #self.canvas.move(self._drag_data["item"], delta_x, delta_y) 
     self.canvas.move(self._drag_data["item"], delta_x, delta_y) 

if __name__ == "__main__": 
    root = tk.Tk() 
    Example(root).pack(fill="both", expand=True) 
    root.mainloop() 
関連する問題