Tkinter:一次移动多个对象
Tkinter : Moving more than one object at once
我对 tkinter 还是很陌生,我正在尝试创建一个网络模拟器。我制作了一个更简单的程序版本,没有所有额外的网络内容。我已经被困在一个问题上很长一段时间了。我不知道如何制作它,所以当我尝试移动其中一个圆圈时,它会随之移动文本。我想在单击时删除文本,这样我就可以移动对象并在放下对象时重新创建文本,但我不确定如何执行此操作。当然欢迎任何其他想法。
这是我的图书馆:
from Tkinter import *
import time
#Window class for making different windows
class Window:
#Constructor
def __init__(self, window, colour="black", width=600, height=400):
#Set variables
self.width = width
self.height = height
self.colour = colour
#This dictionary is used to keep track of an item being dragged
self._drag_data = {"x": 0, "y": 0, "item": None}
#Create canvas
self.canvas = Canvas(window, bg=self.colour, height=self.height, width=self.width)
self.canvas.pack()
#Add bindings for clicking, dragging and releasing over any object with the "circledrag" tag
self.canvas.tag_bind("circledrag", "<ButtonPress-1>", self.OnCircleButtonPress)
self.canvas.tag_bind("circledrag", "<ButtonRelease-1>", self.OnCircleButtonRelease)
self.canvas.tag_bind("circledrag", "<B1-Motion>", self.OnCircleMotion)
#This is used to draw particle objects on the canvas, notice the tag that has been added as an attribute
def _create_circle(self, xcoord, ycoord, color):
self.canvas.create_oval(xcoord-25, ycoord-25, xcoord+25, ycoord+25, outline=color, fill=color, tags = "circledrag")
#This is used to draw text on top of the object on the canvas
def _create_text(self, xcoord, ycoord, text):
self.canvas.create_text(xcoord, ycoord, text = text, tags = ("circledrag", "text"))
#This uses the find_closest method to get store the x and y positions of the nearest item into the dictionary
def OnCircleButtonPress(self, event):
#print self.canvas.find_withtag("Current")
self.canvas.delete("text")
'''Begin 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
#This clears the dictionary once the mouse button has been released
def OnCircleButtonRelease(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
#This moves the item as it is being dragged around the screen
def OnCircleMotion(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
class DragCircle:
#Constructor
def __init__(self, window, width=100, height=100, colour="red", text = "test"):
self.window = window
self.circle = self.window._create_circle(width, height, colour)
self.circle_text = self.window._create_text(width, height, text)
这是主程序:
from Tkinter import *
import my_module2
#Make a window from my own class
window = Tk()
window.title("Drag & Drop")
#Create an instance of the window class
main_window = my_module2.Window(window)
#Create a circle object from the DragCircle class
circle = my_module2.DragCircle(main_window)
circle2 = my_module2.DragCircle(main_window, 200, 200, "green", "hello")
#Start the animation loop
window.mainloop()
我想知道如何同时移动两个对象。
每次创建 circle/text 对时,创建一个唯一标签并将该标签与圆圈和文本相关联。然后您可以使用这个独特的标签同时移动两个对象。
例如,将您的 DragCircle
构造函数修改为如下所示:
class DragCircle:
#Constructor
def __init__(self, window, width=100, height=100, colour="red", text = "test"):
self.window = window
tag = "circle-%d" % id(self)
self.circle = self.window._create_circle(width, height, colour, tag)
self.circle_text = self.window._create_text(width, height, text, tag)
接下来,修改_create_circle
和_create_text
函数来接受标签:
def _create_circle(self, xcoord, ycoord, color, tag):
id_=self.canvas.create_oval(xcoord-25, ycoord-25, xcoord+25, ycoord+25, outline=color, fill=color, tags = ("circledrag", tag))
#This is used to draw text on top of the object on the canvas
def _create_text(self, xcoord, ycoord, text, tag):
self.canvas.create_text(xcoord, ycoord, text = text, tags = ("circledrag", "text", tag))
最后,修改您的 OnCircleButtonPress
以获取标签并将其用于移动对象:
def OnCircleButtonPress(self, event):
...
item = self.canvas.find_closest(event.x, event.y)[0]
tags = self.canvas.gettags(item)
for tag in tags:
if tag.startswith("circle-"):
break
self._drag_data["item"] = tag
...
我的建议可能不是最好的
在构造函数中定义一个关联 table。
self.assoc = {}
添加方法:
def _create_circlewt(self, xcoord, ycoord, color, text):
tc = self.canvas.create_oval(xcoord-25, ycoord-25, xcoord+25, ycoord+25, outline=color, fill=color, tags = "circledrag")
tx = self.canvas.create_text(xcoord, ycoord, text = text)
self.assoc[tc] = tx
在 OnCircleMotion 中:添加行
self.canvas.move(self.assoc[self._drag_data["item"]], delta_x, delta_y)
就在另一条移动线之后
在 DragCircle class 构造函数中
注释您的定义并将调用添加到 _create_circlewt
self.circle_w_text = self.window._create_circlewt(width, height, colour,text)
它有效.. 我让你增强 canvas 移动以启用多个对象关联,如
self.assoc[CircleIndex].append(otherobjectIndex)
有一个解决方案
import tkinter as tk
import time
root = tk.Tk()
canvas = tk.Canvas(root)
canvas.pack()
canvas.create_oval(100, 105, 150, 150, tags="Bob", fill='light blue', outline='green')
canvas.create_oval(200, 205, 150, 150, tags="Bob", fill='light blue', outline='green')
for _ in range(50):
canvas.move("Bob", 5, 0)
canvas.update()
time.sleep(0.05)
root.mainloop()
我对 tkinter 还是很陌生,我正在尝试创建一个网络模拟器。我制作了一个更简单的程序版本,没有所有额外的网络内容。我已经被困在一个问题上很长一段时间了。我不知道如何制作它,所以当我尝试移动其中一个圆圈时,它会随之移动文本。我想在单击时删除文本,这样我就可以移动对象并在放下对象时重新创建文本,但我不确定如何执行此操作。当然欢迎任何其他想法。
这是我的图书馆:
from Tkinter import *
import time
#Window class for making different windows
class Window:
#Constructor
def __init__(self, window, colour="black", width=600, height=400):
#Set variables
self.width = width
self.height = height
self.colour = colour
#This dictionary is used to keep track of an item being dragged
self._drag_data = {"x": 0, "y": 0, "item": None}
#Create canvas
self.canvas = Canvas(window, bg=self.colour, height=self.height, width=self.width)
self.canvas.pack()
#Add bindings for clicking, dragging and releasing over any object with the "circledrag" tag
self.canvas.tag_bind("circledrag", "<ButtonPress-1>", self.OnCircleButtonPress)
self.canvas.tag_bind("circledrag", "<ButtonRelease-1>", self.OnCircleButtonRelease)
self.canvas.tag_bind("circledrag", "<B1-Motion>", self.OnCircleMotion)
#This is used to draw particle objects on the canvas, notice the tag that has been added as an attribute
def _create_circle(self, xcoord, ycoord, color):
self.canvas.create_oval(xcoord-25, ycoord-25, xcoord+25, ycoord+25, outline=color, fill=color, tags = "circledrag")
#This is used to draw text on top of the object on the canvas
def _create_text(self, xcoord, ycoord, text):
self.canvas.create_text(xcoord, ycoord, text = text, tags = ("circledrag", "text"))
#This uses the find_closest method to get store the x and y positions of the nearest item into the dictionary
def OnCircleButtonPress(self, event):
#print self.canvas.find_withtag("Current")
self.canvas.delete("text")
'''Begin 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
#This clears the dictionary once the mouse button has been released
def OnCircleButtonRelease(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
#This moves the item as it is being dragged around the screen
def OnCircleMotion(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
class DragCircle:
#Constructor
def __init__(self, window, width=100, height=100, colour="red", text = "test"):
self.window = window
self.circle = self.window._create_circle(width, height, colour)
self.circle_text = self.window._create_text(width, height, text)
这是主程序:
from Tkinter import *
import my_module2
#Make a window from my own class
window = Tk()
window.title("Drag & Drop")
#Create an instance of the window class
main_window = my_module2.Window(window)
#Create a circle object from the DragCircle class
circle = my_module2.DragCircle(main_window)
circle2 = my_module2.DragCircle(main_window, 200, 200, "green", "hello")
#Start the animation loop
window.mainloop()
我想知道如何同时移动两个对象。
每次创建 circle/text 对时,创建一个唯一标签并将该标签与圆圈和文本相关联。然后您可以使用这个独特的标签同时移动两个对象。
例如,将您的 DragCircle
构造函数修改为如下所示:
class DragCircle:
#Constructor
def __init__(self, window, width=100, height=100, colour="red", text = "test"):
self.window = window
tag = "circle-%d" % id(self)
self.circle = self.window._create_circle(width, height, colour, tag)
self.circle_text = self.window._create_text(width, height, text, tag)
接下来,修改_create_circle
和_create_text
函数来接受标签:
def _create_circle(self, xcoord, ycoord, color, tag):
id_=self.canvas.create_oval(xcoord-25, ycoord-25, xcoord+25, ycoord+25, outline=color, fill=color, tags = ("circledrag", tag))
#This is used to draw text on top of the object on the canvas
def _create_text(self, xcoord, ycoord, text, tag):
self.canvas.create_text(xcoord, ycoord, text = text, tags = ("circledrag", "text", tag))
最后,修改您的 OnCircleButtonPress
以获取标签并将其用于移动对象:
def OnCircleButtonPress(self, event):
...
item = self.canvas.find_closest(event.x, event.y)[0]
tags = self.canvas.gettags(item)
for tag in tags:
if tag.startswith("circle-"):
break
self._drag_data["item"] = tag
...
我的建议可能不是最好的
在构造函数中定义一个关联 table。
self.assoc = {}
添加方法:
def _create_circlewt(self, xcoord, ycoord, color, text):
tc = self.canvas.create_oval(xcoord-25, ycoord-25, xcoord+25, ycoord+25, outline=color, fill=color, tags = "circledrag")
tx = self.canvas.create_text(xcoord, ycoord, text = text)
self.assoc[tc] = tx
在 OnCircleMotion 中:添加行
self.canvas.move(self.assoc[self._drag_data["item"]], delta_x, delta_y)
就在另一条移动线之后
在 DragCircle class 构造函数中 注释您的定义并将调用添加到 _create_circlewt
self.circle_w_text = self.window._create_circlewt(width, height, colour,text)
它有效.. 我让你增强 canvas 移动以启用多个对象关联,如
self.assoc[CircleIndex].append(otherobjectIndex)
import tkinter as tk
import time
root = tk.Tk()
canvas = tk.Canvas(root)
canvas.pack()
canvas.create_oval(100, 105, 150, 150, tags="Bob", fill='light blue', outline='green')
canvas.create_oval(200, 205, 150, 150, tags="Bob", fill='light blue', outline='green')
for _ in range(50):
canvas.move("Bob", 5, 0)
canvas.update()
time.sleep(0.05)
root.mainloop()