【问题标题】:Modifying code to drag strings修改代码拖动字符串
【发布时间】:2016-11-22 15:29:44
【问题描述】:

我一直在试图理解我从 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()

【问题讨论】:

  • 请将 sn-p 添加到您的问题中。
  • @ParvizKarimli 添加了代码。
  • 请具体说明您遇到的问题。当我们不知道您的经历时,很难为您提供帮助
  • “用户能够从两个列表中移动键值对并匹配它们,而不是椭圆形”是什么意思?
  • @ParvizKarimli 我希望用户能够从一个列表中拖动诸如“User”之类的字符串,并从另一个列表中拖动“Ryan”之类的字符串,并匹配它们。

标签: python-3.x tkinter


【解决方案1】:

这是您要找的吗:

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()



我改变了什么:
(编辑线:10、18、19、27、30 和 31)
-canvas 背景颜色从默认(白色)变为红色,以更好地识别其上的白色和黑色对象;
-self.canvas.create_ovalself.canvas.create_text 因为你想要字符串而不是椭圆;
-此外,删除了第二对坐标 (x+25, y+25),因为 create_text 只需要 一对坐标(create_oval 需要 两个),然后删除outline=color 因为文本对象没有 outline 选项,所以 Tkinter 返回 unknown option 错误;
- 最后,在将其从create_oval 更改为create_text 之后,我必须将text 选项mytext 添加到_create_token 函数(def _create_token(self, coord, color, mytext):)及其实例(“User”和“Ryan”) ) 到可移动对象:
self._create_token((100, 100), "white", "User") self._create_token((200, 100), "black", "Ryan").

【讨论】:

  • 是的,这很棒。如果您不介意,您能解释一下为什么我们需要create_text 中的tags="token" 吗?
  • 只是一个标签名。标签用于控制对象。标签是附加到项目的符号名称。标签是普通字符串,它们可以包含除空格以外的任何内容(只要它们看起来不像项目句柄)(来源:effbot.org/tkinterbook/canvas.htm)。我建议你阅读this
猜你喜欢
  • 2020-07-30
  • 2010-12-14
  • 2010-10-30
  • 2022-01-22
  • 2020-03-16
  • 2017-09-26
  • 2014-05-30
  • 2018-04-10
  • 2021-10-22
相关资源
最近更新 更多