- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在开发一个网络项目,我希望能够创建多个节点并在它们之间添加连接。我已经完成了这一切,但现在我坚持尝试在 move 对象时使线条随对象 move 。我为我创建的每个对象创建了字典,这些字典都存储在一个列表中。字典存储该项目、该项目具有的连接器数量,以及它链接到该项目的连接器。
如何才能使线条在 move 对象时随对象一起 move ?
这是我的库代码:
#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: ahilton
#
# Created: 18/12/2014
# Copyright: (c) ahilton 2014
# Licence: <your licence>
#-------------------------------------------------------------------------------
#used for validation
#will make sure that the program will run on different versions of python
try:
# Python2
from Tkinter import *
except ImportError:
# Python3
from tkinter import *
#imports pre-defined libraries that are installed in tkinter
import random
#______________________________________________________________________________________________________________________________________________________________________
#creates a variable used for a count
count = 1
IPvar = 256
f = 0
ConnectCount = 1
a = 0
temp = 0
temp2 = 0
itemsconnectors = []
itemlist = []
LinkedItems = []
#______________________________________________________________________________________________________________________________________________________________________
class Window:
#______________________________________________________________________________________________________________________________________________________________________
def __init__(self, window, colour = 'White', width = 1440, height = 720):
#window variables
self.colour = colour
self.screenwidth = width
self.screenheight = height
#create the panes
self.MainCanvas = Canvas(window, background = self.colour, width = self.screenwidth, height = self.screenheight)
self.MainCanvas.pack()
#This dictionary is used to keep track of an item being dragged
self._drag_data = {"x": 0, "y": 0, "item": None}
#This dictionary is used to keep track of an item having it's state changed
self._state_change = {"item": None, "tag": "PCchange1", "colour": "Black"}
#This dictionary is used to keep track of the items being connected
self._connector = {"connector": None, "item": None, "item2": None, "x": 0, "y": 0, "x2": 0, "y2": 0, "tag": "False"}
self._object_data = []
#Add bindings for clicking, dragging and releasing over any object with the "PCdrag" tag
self.MainCanvas.tag_bind("PCdrag", "<ButtonPress-1>", self.OnButtonPress)
self.MainCanvas.tag_bind("PCdrag", "<ButtonRelease-1>", self.OnButtonRelease)
self.MainCanvas.tag_bind("PCdrag", "<B1-Motion>", self.OnMotion)
self.MainCanvas.tag_bind("ColourSwitch", "<ButtonPress-3>", self.ColourChange)
#self.MainCanvas.tag_bind("ColourSwitch", "<ButtonRelease-3>", self.ColourChangeRelease)
#self.MainCanvas.tag_bind("PCchange", "<B3-Motion>", self.ColourChange)
self.MainCanvas.tag_bind("text", "<Return>", self.OnEnter)
#______________________________________________________________________________________________________________________________________________________________________
def OnEnter(self, event):
print 'hi'
#______________________________________________________________________________________________________________________________________________________________________
def ChangeClick(self):
#This will change the binding of the mouse while the connector process is active
self.MainCanvas.tag_bind("PCdrag", "<ButtonPress-1>", self.LocateObject)
self.MainCanvas.tag_bind("PCdrag", "<ButtonRelease-1>", self.LocateObject2)
self.MainCanvas.tag_bind("PCdrag", "<B1-Motion>", self.Ignore)
#this is a temporary function to get rid of the OnMotion function which is usually called when the item is dragged
def Ignore(self, event):
a = 1
#print "Ignore"
#______________________________________________________________________________________________________________________________________________________________________
#This is used to draw the computer on the canvas, notice the tag that has been added as an attribute
def CreateComputer(self, x_start, y_start, color, tag):
#count = count + 1
#self._state_change["count"] = self._state_change["count"] + 1
#Creates a list of tags that are going to be added on to each computer object
#The second tag is defined elsewhere in the program as it changes depending on the state of the computer
#tag = ["PCdrag", self._state_change["count"], self._state_change["tag"], "ColourSwitch", self._connector["tag"]]
if temp <= temp2:
self._object_data.append(dict(item=count, CoCount=0))
#creates the object and takes properties from the DragItem class
self.MainCanvas.create_rectangle(x_start-40, y_start-25, x_start+40, y_start+25, outline=color, fill=color, tags = ("PCdrag", self._state_change["tag"], "ColourSwitch", self._connector["tag"], tag))
itemlist.append(self._state_change)
#print itemlist
#______________________________________________________________________________________________________________________________________________________________________
#This is used to draw text on top of the object on the canvas
def CreateText(self, x_start, y_start, Node, tag):
global count
global f
global IPvar
global temp
global temp2
IP = "192.168.0."
if f == 0:
temp2 = Node - 1
#IPvar = (IPvar - Node) + 1
#temp = IPvar
temp = 1
f = 1
else:
f = 1
if temp <= temp2:
self.MainCanvas.create_text(x_start, y_start, text = (IP, temp), fill = 'white', tag = ('PCdrag','text', tag))
count = count + 1
temp = temp + 1
else:
print 'fail'
#______________________________________________________________________________________________________________________________________________________________________
#this function will be called before the line is created. it sets all the variables for the line
def CreateLine(self):
global ConnectCount
#sets the four variables that will determine the coords of the line
xstart = self._connector["x"]
ystart = self._connector["y"]
xend = self._connector["x2"]
yend = self._connector["y2"]
#this will reset the _connector dictionary
self._connector["x"] = 0
self._connector["y"] = 0
self._connector["x2"] = 0
self._connector["y2"] = 0
#this will set the tag bindings back to their original ones to allow you to drag the item again
self.MainCanvas.tag_bind("PCdrag", "<ButtonPress-1>", self.OnButtonPress)
self.MainCanvas.tag_bind("PCdrag", "<ButtonRelease-1>", self.OnButtonRelease)
self.MainCanvas.tag_bind("PCdrag", "<B1-Motion>", self.OnMotion)
#print self._object_data
#tag = ["Linedrag", self._connector("item"), self._connector("item2")]
#this will create the line and put it on the main canvas
self.MainCanvas.create_line(xstart, ystart, xend , yend, tags = ("Linedrag", ConnectCount), width = 5, smooth = 1)
self.MainCanvas.tag_raise("PCdrag")
ConnectCount = ConnectCount + 1
#______________________________________________________________________________________________________________________________________________________________________
def LocateObject(self, event):
global ConnectCount
#sets the key "item" in the _connector dictionary to be equal to the object closest to the mouse
#sets the key "x" and the key "y" to be equal to the x and y coords of the object
self._connector["item"] = self.MainCanvas.find_closest(event.x, event.y)[0]
if self._connector["item"] % 2 == 0:
temp = self._connector["item"] / 2
for d in range(len(self._object_data)):
if self._object_data[d]["item"] == temp:
self._object_data[d]["CoCount"] = self._object_data[d]["CoCount"] + 1
self._object_data[d]["Connect" + str(self._object_data[d]["CoCount"])] = ConnectCount
self._connector["item"] = self._connector["item"] - 1
else:
self._connector["item"] = self._connector["item"] + 1
temp = self._connector["item"] / 2
for d in range(len(self._object_data)):
if self._object_data[d]["item"] == temp:
self._object_data[d]["CoCount"] = self._object_data[d]["CoCount"] + 1
self._object_data[d]["Connect" + str(self._object_data[d]["CoCount"])] = ConnectCount
self._connector["item"] = self._connector["item"] - 1
#print self._object_data, 'test1'
self._connector["x"] = self.MainCanvas.coords(self._connector["item"])[0] + 40
self._connector["y"] = self.MainCanvas.coords(self._connector["item"])[1] + 25
#______________________________________________________________________________________________________________________________________________________________________
def LocateObject2(self, event):
global ConnectCount
#sets the key "item" in the _connector dictionary to be equal to the object closest to the mouse
#sets the key "x2" and the key "y2" to be equal to the x and y coords of the object
#self._connector["connector"] = self.MainCanvas.find_closest(event.x, event.y)
self._connector["item2"] = self.MainCanvas.find_closest(event.x, event.y)[0]
if self._connector["item2"] % 2 == 0:
temp = self._connector["item2"] / 2
for d in range(len(self._object_data)):
if self._object_data[d]["item"] == temp:
self._object_data[d]["CoCount"] = self._object_data[d]["CoCount"] + 1
self._object_data[d]["Connect" + str(self._object_data[d]["CoCount"])] = ConnectCount
self._connector["item2"] = self._connector["item2"] - 1
else:
self._connector["item2"] = self._connector["item2"] + 1
temp = self._connector["item2"] / 2
for d in range(len(self._object_data)):
if self._object_data[d]["item"] == temp:
self._object_data[d]["CoCount"] = self._object_data[d]["CoCount"] + 1
self._object_data[d]["Connect" + str(self._object_data[d]["CoCount"])] = ConnectCount
self._connector["item2"] = self._connector["item2"] - 1
#print self._object_data, 'test2'
self._connector["x2"] = self.MainCanvas.coords(self._connector["item2"])[2] - 40
self._connector["y2"] = self.MainCanvas.coords(self._connector["item2"])[3] - 25
#an if statement to check whether the items are the same or if there is no item selected
if self._connector["item"] != self._connector["item2"]:
self._connector["tag"] = "True"
#sets the tags of the objects again to compensate for the tag change in the if statements previously
#tag = ["PCdrag", self._state_change["count"], self._state_change["tag"], "ColourSwitch", self._connector["tag"]]
#tag = ["PCdrag", self._state_change["tag"], "ColourSwitch", self._connector["tag"]]
#configures the items to change the tags of the items accordingly
#self.MainCanvas.itemconfig(self._connector["item"], tags = tag)
#self.MainCanvas.itemconfig(self._connector["item2"], tags = tag)
self.CreateLine()
self._connector["item"] = None
self._connector["connector"] = None
self._connector["item2"] = None
self._connector["x"] = 0
self._connector["y"] = 0
self._connector["x2"] = 0
self._connector["y2"] = 0
elif self._connector["item"] and self._connector["item2"] == 0:
self.Fail()
else:
self.Fail()
#______________________________________________________________________________________________________________________________________________________________________
#function that is called if the connector cannot be made
def Fail(self):
print 'Fail'
self._connector = {"connector": None, "item": None, "item2": None, "x": 0, "y": 0, "x2": 0, "y2": 0, "tag": "False"}
#this will set the tag bindings back to their original ones to allow you to drag the item again
self.MainCanvas.tag_bind("PCdrag", "<ButtonPress-1>", self.OnButtonPress)
self.MainCanvas.tag_bind("PCdrag", "<ButtonRelease-1>", self.OnButtonRelease)
self.MainCanvas.tag_bind("PCdrag", "<B1-Motion>", self.OnMotion)
#______________________________________________________________________________________________________________________________________________________________________
#This uses the find_closest method to get store the x and y positions of the nearest item into the dictionary
def OnButtonPress(self, event):
global itemsconnectors
global a
def Search(a):
for d in range(len(self._object_data)):
print 'hi1'
print self._object_data
if a in self._object_data[d]:
print 'hi'
## def Search():
## for d in range(len(a)):
## if 15 in a[d]["CoCount"]:
## print 'hi'
## else:
## print 'fail'
p = 0
item = self.MainCanvas.find_closest(event.x, event.y)[0]
tags = self.MainCanvas.gettags(item)
for tag in tags:
if tag.startswith("circle-"):
break
self._drag_data["item"] = tag
'''Begin drag of an object'''
# record the item and its location
#self._drag_data["item"] = self.MainCanvas.find_closest(event.x, event.y)[0]
self._drag_data["x"] = event.x
self._drag_data["y"] = event.y
if item % 2 == 0:
item = item / 2
#runs for loop for the amount of nodes on screen
for d in range(len(self._object_data)):
#checks to find the current items dictionary
if self._object_data[d]["item"] == item:
#checks if the item has a connector
if self._object_data[d]["CoCount"] > 0:
#sets the amount of connectors the object has
connectors = len(self._object_data[d]) - 2
#runs a loop for the amount of connectors it has
for i in range(connectors):
#sets a to be equal to the current connector in the loop
a = self._object_data[d]["Connect" + str(i + 1)]
#prints the coords of the current connector
print self.MainCanvas.coords(len(itemlist) + a)
#appends that connector to a list
itemsconnectors.append(a)
print itemsconnectors
#runs loop for the amount of nodes on screen
for o in range(len(self._object_data)):
#LinkedItems.append(Search(self._object_data[d], a))
#checks if each node is connected to current connector
LinkedItems.append(Search(a))
else:
print 'No Connectors'
#print LinkedItems
#print itemlist
#print list(self.MainCanvas.gettags(self.MainCanvas.find_closest(event.x, event.y)))
#______________________________________________________________________________________________________________________________________________________________________
#This clears the dictionary once the mouse button has been released
def OnButtonRelease(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 OnMotion(self, event):
b = len(self._object_data) + a
'''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.MainCanvas.move(self._drag_data["item"], delta_x, delta_y)
#self.MainCanvas.coords(b, 10, 10 ,100 ,100)
# record the new position
self._drag_data["x"] = event.x
self._drag_data["y"] = event.y
#______________________________________________________________________________________________________________________________________________________________________
def Delete_Object(self):
global IPvar
temp = IPvar
#deletes all the objects on the canvas
self.MainCanvas.delete('all')
#This dictionary is used to keep track of an item being dragged
self._drag_data = {"x": 0, "y": 0, "item": None}
#This dictionary is used to keep track of an item having it's state changed
self._state_change = {"item": None, "tag": "PCchange1", "colour": "Black"}
#This dictionary is used to keep track of the items being connected
self._connector = {"connector": None, "item": None, "item2": None, "x": 0, "y": 0, "x2": 0, "y2": 0, "tag": "False"}
self._data_store = {"item": None, "tag": self._state_change["tag"]}
#______________________________________________________________________________________________________________________________________________________________________
def ColourChange(self, event):
#sets the key "item" in the _state_change dictionaryto be equal to the object closest to the mouse
self._state_change["item"] = self.MainCanvas.find_closest(event.x, event.y)[0]
#a set of if statements to check what the current object colour is and change it depending on its colour
if self._state_change["colour"] == "Black":
self._state_change["colour"] = "Red"
#changes the tag of the object to reflect what colour it is
#this will then be used to set the PC to be either a sender, receiver or a router
self._state_change["tag"] = "PCchange2"
elif self._state_change["colour"] == "Red":
self._state_change["colour"] = "Blue"
self._state_change["tag"] = "PCchange3"
elif self._state_change["colour"] == "Blue":
self._state_change["colour"] = "Black"
self._state_change["tag"] = "PCchange1"
#an else for error checking. incase none of the if statements work
else:
print "Error"
#sets the tags of the objects again to compensate for the tag change in the if statements previously
#tag = ["PCdrag", self._state_change["count"], self._state_change["tag"], "ColourSwitch", self._connector["tag"]]
#tag = ["PCdrag", self._state_change["tag"], "ColourSwitch", self._connector["tag"]]
#configures the items to change the colour and the tags of the items accordingly
self.MainCanvas.itemconfig(self._state_change["item"], fill = self._state_change["colour"])
#print self.MainCanvas.gettags(self.MainCanvas.find_closest(event.x, event.y))
#______________________________________________________________________________________________________________________________________________________________________
class DragItem:
#Constructor
#Initiates when the class is called
#all the arguements are passed through
#the aruements have pre-set values that can be over written incase the program doesnt get passed anything
def __init__(self, window, width=100, height=100, colour="black", x_start=0, y_start=0, node = 5):
#window variables
self.window = window
tag = "circle-%d" % id(self)
#calls a method to create the computer object
self.circle = self.window.CreateComputer(x_start, y_start, colour, tag)
self.circle_text = self.window.CreateText(x_start, y_start, node, tag)
class Connector:
#Constructor
#Initiates when the class is called
#all the arguements are passed through
#the aruements have pre-set values that can be over written incase the program doesnt get passed anything
def __init__(self, window):
#window variables
self.window = window
self.window.ChangeClick()
最佳答案
您可以使用canvas.coords()
方法重新定义线的点( http://effbot.org/tkinterbook/canvas.htm )。
假设line
是id
或tag
您要 move 的行的 canvas.coords(line)
将返回线的坐标。如果它是一条直线,这会给你两个点 pt1
和pt2
代表线的起点和终点。 move 对象后,这两点之一将成为新线的起点。
然后,您 move 该对象。当释放鼠标按钮时,将调用一个函数。该函数的第一个参数是 event
并包含释放鼠标按钮时鼠标的坐标。这些坐标( event.x
和 event.y
)将是新行的末尾。
有了这个,您应该拥有新线的所有坐标(起点和终点),您需要做的就是调用 canvas.coords(line, new_coords)
将线 move 到新位置。
关于python - 使用 Object Tkinter move 线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29375901/
我将 Bootstrap 与 css 和 java 脚本结合使用。在不影响前端代码的情况下,我真的很难在css中绘制这个背景。在许多问题中,人们将宽度和高度设置为 0%。但是由于我的导航栏,我不能使用
我正在用 c 编写一个程序来读取文件的内容。代码如下: #include void main() { char line[90]; while(scanf("%79[^\
我想使用 javascript 获取矩阵数组的所有对 Angular 线。假设输入输出如下: input = [ [1,2,3], [4,5,6], [7,8,9], ] output =
可以用pdfmake绘制lines,circles和other shapes吗?如果是,是否有documentation或样本?我想用jsPDF替换pdfmake。 最佳答案 是的,有可能。 pdfm
我有一个小svg小部件,其目的是显示角度列表(参见图片)。 现在,角度是线元素,仅具有笔触,没有填充。但是现在我想使用一种“内部填充”颜色和一种“笔触/边框”颜色。我猜想line元素不能解决这个问题,
我正在为带有三角对象的 3D 场景编写一个非常基本的光线转换器,一切都工作正常,直到我决定尝试从场景原点 (0/0/0) 以外的点转换光线。 但是,当我将光线原点更改为 (0/1/0) 时,相交测试突
这个问题已经有答案了: Why do people write "#!/usr/bin/env python" on the first line of a Python script? (22 个回
如何使用大约 50 个星号 * 并使用 for 循环绘制一条水平线?当我尝试这样做时,结果是垂直(而不是水平)列出 50 个星号。 public void drawAstline() { f
这是一个让球以对角线方式下降的 UI,但球保持静止;线程似乎无法正常工作。你能告诉我如何让球移动吗? 请下载一个球并更改目录,以便程序可以找到您的球的分配位置。没有必要下载足球场,但如果您愿意,也可以
我在我的一个项目中使用 Jmeter 和 Ant,当我们生成报告时,它会在报告中显示 URL、#Samples、失败、成功率、平均时间、最短时间、最长时间。 我也想在报告中包含 90% 的时间线。 现
我有一个不寻常的问题,希望有人能帮助我。我想用 Canvas (android) 画一条 Swing 或波浪线,但我不知道该怎么做。它将成为蝌蚪的尾部,所以理想情况下我希望它的形状更像三角形,一端更大
这个问题已经有答案了: Checking Collision of Shapes with JavaFX (1 个回答) 已关闭 8 年前。 我正在使用 JavaFx 8 库。 我的任务很简单:我想检
如何按编号的百分比拆分文件。行数? 假设我想将我的文件分成 3 个部分(60%/20%/20% 部分),我可以手动执行此操作,-_-: $ wc -l brown.txt 57339 brown.tx
我正在努力实现这样的目标: 但这就是我设法做到的。 你能帮我实现预期的结果吗? 更新: 如果我删除 bootstrap.css 依赖项,问题就会消失。我怎样才能让它与 Bootstrap 一起工作?
我目前正在构建一个网站,但遇到了 transform: scale 的问题。我有一个按钮,当用户将鼠标悬停在它上面时,会发生两件事: 背景以对 Angular 线“扫过” 按钮标签颜色改变 按钮稍微变
我需要使用直线和仿射变换绘制大量数据点的图形(缩放图形以适合 View )。 目前,我正在使用 NSBezierPath,但我认为它效率很低(因为点在绘制之前被复制到贝塞尔路径)。通过将我的数据切割成
我正在使用基于 SVM 分类的 HOG 特征检测器。我可以成功提取车牌,但提取的车牌除了车牌号外还有一些不必要的像素/线。我的图像处理流程如下: 在灰度图像上应用 HOG 检测器 裁剪检测到的区域 调
我有以下图片: 我想填充它的轮廓(即我想在这张图片中填充线条)。 我尝试了形态学闭合,但使用大小为 3x3 的矩形内核和 10 迭代并没有填满整个边界。我还尝试了一个 21x21 内核和 1 迭代,但
我必须找到一种算法,可以找到两组数组之间的交集总数,而其中一个数组已排序。 举个例子,我们有这两个数组,我们向相应的数字画直线。 这两个数组为我们提供了总共 7 个交集。 有什么样的算法可以帮助我解决
简单地说 - 我想使用透视投影从近裁剪平面绘制一条射线/线到远裁剪平面。我有我认为是使用各种 OpenGL/图形编程指南中描述的方法通过单击鼠标生成的正确标准化的世界坐标。 我遇到的问题是我的光线似乎
我是一名优秀的程序员,十分优秀!