Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 修改代码以拖动字符串_Python 3.x_Tkinter - Fatal编程技术网

Python 3.x 修改代码以拖动字符串

Python 3.x 修改代码以拖动字符串,python-3.x,tkinter,Python 3.x,Tkinter,我一直在努力理解布莱恩·奥克利的代码。目前,此代码允许用户使用tkinter拖动两个椭圆形。我希望能够修改这段代码,以便用户能够从两个列表中拖动字符串(键值对)并匹配它们,而不是椭圆。例如,我希望用户能够从一个列表中拖动一个字符串,如“user”,从另一个列表中拖动“Ryan”,并匹配它们。我将非常感谢任何关于如何修改代码的输入,以便用户能够拖动这两个字符串 import Tkinter as tk class SampleApp(tk.Tk): '''Illustrate how t

我一直在努力理解布莱恩·奥克利的代码。目前,此代码允许用户使用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()
将Tkinter作为tk导入
类SampleApp(tk.tk):
''演示如何在Tkinter画布上拖动项目''
定义初始化(self,*args,**kwargs):
tk.tk.\uuuuu初始化(self,*args,**kwargs)
#创建画布
self.canvas=tk.canvas(宽度=400,高度=400)
self.canvas.pack(fill=“both”,expand=True)
#此数据用于跟踪
#正在拖动的项目
self._drag_data={“x”:0,“y”:0,“item”:None}
#创建一对可移动对象
self.\u创建\u标记((100100),“白色”)
自我。创建令牌((200100),“黑色”)
#添加用于单击、拖动和释放的绑定
#任何带有“token”标记的对象
self.canvas.tag_bind(“token”,“”,self.OnTokenButtonPress)
self.canvas.tag_bind(“token”,“”,self.OnTokenButtonRelease)
self.canvas.tag_bind(“token”,“”,self.OnTokenMotion)
def_create_令牌(self、coord、color):
''在给定坐标以给定颜色创建标记''
(x,y)=坐标
自我。画布。创建椭圆(x-25,y-25,x+25,y+25,
轮廓=颜色,填充=颜色,标记=“标记”)
def OnTokenButton按(自身,事件):
“正在拖动对象”
#记录项目及其位置
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._drag_data[“item”]=无
自拖动数据[“x”]=0
自身数据[“y”]=0
def OnTokenMotion(自我、事件):
''处理对象的拖动''
#计算此对象移动了多少
delta_x=event.x-self._拖动_数据[“x”]
delta_y=event.y-self.\u拖动数据[“y”]
#将对象移动适当的量
self.canvas.move(self.\u拖动数据[“项”]、增量x、增量y)
#记录新位置
self._drag_data[“x”]=event.x
self._drag_data[“y”]=event.y
如果名称=“\uuuuu main\uuuuuuuu”:
app=SampleApp()
app.mainloop()

这就是你想要的:

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()
将tkinter作为tk导入
类SampleApp(tk.tk):
''演示如何在Tkinter画布上拖动项目''
定义初始化(self,*args,**kwargs):
tk.tk.\uuuuu初始化(self,*args,**kwargs)
#创建画布
self.canvas=tk.canvas(宽度=400,高度=400,背景为红色)
self.canvas.pack(fill=“both”,expand=True)
#此数据用于跟踪
#正在拖动的项目
self._drag_data={“x”:0,“y”:0,“item”:None}
#创建一对可移动对象
self.\u创建\u令牌((100100),“白色”,“用户”)
自我。创建令牌((200100),“黑色”,“Ryan”)
#添加用于单击、拖动和释放的绑定
#任何带有“token”标记的对象
self.canvas.tag_bind(“token”,“”,self.OnTokenButtonPress)
self.canvas.tag_bind(“token”,“”,self.OnTokenButtonRelease)
self.canvas.tag_bind(“token”,“”,self.OnTokenMotion)
定义创建令牌(self、coord、color、mytext):
''在给定坐标以给定颜色创建标记''
(x,y)=坐标
self.canvas.create_文本(x-25,y-25,
fill=color,tags=“token”,text=mytext)
def OnTokenButton按(自身,事件):
“正在拖动对象”
#记录项目及其位置
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._drag_data[“item”]=无
自拖动数据[“x”]=0
自身数据[“y”]=0
def OnTokenMotion(自我、事件):
''处理对象的拖动''
#计算此对象移动了多少
delta_x=event.x-self._拖动_数据[“x”]
delta_y=event.y-self.\u拖动数据[“y”]
#将对象移动适当的量
self.canvas.move(self.\u拖动数据[“项”]、增量x、增量y)
#记录新位置
self._drag_data[“x”]=event.x
self._drag_data[“y”]=event.y
如果名称=“\uuuuu main\uuuuuuuu”:
app=SampleApp()
app.mainloop()
?

我改变了什么:
(编辑行:10、18、19、27、30和31)
-画布背景颜色从默认(白色)改为红色,以更好地识别其上的白色和黑色对象
-
self.canvas.create_oval
self.canvas.create_text
,因为您需要字符串而不是椭圆
-另外,删除了第二对坐标(
x+25,y+25
),因为
create\u text
只需要一对坐标(
create\u oval
需要两个),并且删除了
outline=color
,因为文本对象没有
outline
选项,因此Tkinter返回一个
未知选项
错误
-最后,在将其从
create_oval
更改为
create_text
之后,我必须将
text
选项
mytext
添加到
\u create_token
函数(
def\u create_token(self、coord、color、mytext):
)中