2016-07-19 48 views
-2

我一直在试图理解我从Bryan Oakley here遇到的代码。目前,该代码允许用户使用tkinter拖动两个椭圆。我希望能够修改此代码,以便用户不用椭圆就可以从两个列表中拖动字符串(键值对)并匹配它们。例如,我希望用户能够从一个列表中拖拽一个字符串,如“User”,并从另一个列表中拖拽“Ryan”并匹配它们。我将不胜感激任何关于如何修改代码的输入,以便用户能够拖动这两个字符串。修改代码以拖动字符串

import Tkinter as tk 

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

    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 

     # 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 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.OnTokenButtonPress) 
     self.canvas.tag_bind("token", "<ButtonRelease-1>", self.OnTokenButtonRelease) 
     self.canvas.tag_bind("token", "<B1-Motion>", self.OnTokenMotion) 

    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 OnTokenButtonPress(self, event): 
     '''Being 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 OnTokenButtonRelease(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 OnTokenMotion(self, event): 
     '''Handle dragging of an object''' 
     # compute how much this object 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 

if __name__ == "__main__": 
    app = SampleApp() 
    app.mainloop() 
+0

请添加片段到您的问题。 –

+0

@ParvizKarimli添加了代码。 – Neil

+1

请确切指出您遇到什么问题。当我们不知道您正在经历什么时,很难为您提供帮助 –

回答

2

这是你在找什么:

import tkinter as tk 

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

    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 

     # create a canvas 
     self.canvas = tk.Canvas(width=400, height=400, bg='red') 
     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 movable objects 
     self._create_token((100, 100), "white", "User") 
     self._create_token((200, 100), "black", "Ryan") 

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

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

    def OnTokenButtonPress(self, event): 
     '''Being 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 OnTokenButtonRelease(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 OnTokenMotion(self, event): 
     '''Handle dragging of an object''' 
     # compute how much this object 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 

if __name__ == "__main__": 
    app = SampleApp() 
    app.mainloop() 



我改变什么:
(EDITED LINES:10,18,19,27,30和31)从默认(白色)
-canvas背景颜色为红色,以确定在其上更好的白色和黑色的物体;
- self.canvas.create_ovalself.canvas.create_text因为你想要字符串而不是椭圆形;
- 同样,取出第二对坐标(x+25, y+25)因为create_text需要只有一对夫妇坐标(​​对他们的要求),并删除outline=color因为文本对象没有outline选项,因此Tkinter的返回一个unknown option错误;
-and最后,从​​将其更改为create_text后,我不得不把text选项mytext添加到_create_token功能(def _create_token(self, coord, color, mytext):)及其实例(“用户” &“瑞安”)到可移动的物体:
self._create_token((100, 100), "white", "User") self._create_token((200, 100), "black", "Ryan")

+0

是的,这太好了。如果你不介意,你能解释为什么我们需要'create_text'中的tags =“token”吗? – Neil

+0

这只是一个标签名称。标签用于控制对象。 标签是附加到物品的符号名称。标签是普通的字符串,它们可以包含除空白之外的任何东西(只要它们看起来不像项目句柄)(来源:http://effbot.org/tkinterbook/canvas.htm)。 我建议你阅读[this](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/canvas-tags.html)。 –