여기에 게시 된 예제 스크립트를 수정하려고합니다.
원래 스크립트를 사용하면 캔버스 주위에서 개체를 클릭하고 끌 수 있습니다. 버튼이 첫 번째 객체를 이동하도록 변경했지만 버튼 오류가 발생합니다. TypeError : button_move ()는 정확히 2 개의 인수를 취합니다 (1이 주어짐). 두 번째 인수가 무엇인지 이해할 수 없습니다.
수정 된 스크립트는 다음과 같습니다.
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()
command=
이 실행없이 작동 event
하지만 할당 기능을하는 예상하는 event
- command=self.button_move
->def button_move(self, event)
그러나 bind
에서 동일한 기능을 실행 event
하므로 event
.
솔루션 : 이벤트에 기본값을 사용하십시오 .None
def button_move(self, event=None):
하지만 당신은 사용할 수 없습니다 event.x
, event.y
당신이 무엇을 실행하는 경우이 기능에command=
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다