如何在 tkinter 中用箭头连接两个状态圈?
我目前正在使用 tkinter 编写 fsm 编辑器。但是,我坚持连接两个州。我有两个问题:
1)如何使过渡箭头根据鼠标移动而增长?
2)如何将箭头的起点粘贴在一个状态上,将箭头的终点粘贴在另一个状态上?
附言。您认为 tkinter 的文档足够好吗?
I am currently writing a fsm editor with tkinter. But, I stuck on connecting two states. I have two questions:
1) How can make the transition arrow growable according to mouse movement?
2) How can I stick the starting point of the arrow on a state and the end point of the arrow on another state?
PS. Do you think the documentation of tkinter is good enough?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个展示这个概念的例子。简而言之,使用标签将线条与框关联起来,只需在用户移动鼠标时适当调整坐标即可。
运行该示例,然后在米色框中单击并拖动。
当然,对于生产代码,您需要制定一个更通用的解决方案,但希望这可以向您展示创建一个带有箭头的框是多么容易,该箭头会随着您移动框而调整。
Here's an example that shows the concept. In a nutshell, use tags to associate lines with boxes, and simply adjust the coordinates appropriately when the user moves the mouse.
Run the example, then click and drag from within the beige box.
Of course, for production code you need to make a more general solution, but hopefully this shows you how easy it is to create a box with arrows that adjust as you move the box around.
Tkinter 非常适合此类应用程序。过去,我开发过一些工具,这些工具是用箭头连接的盒子,当您移动盒子时,箭头会保持连接(我认为这就是您要问的问题)。不要让不太了解 Tkinter 的人影响您——它是一个完美的工具包,而且画布非常灵活。
您的问题的解决方案是简单的数学。您只需要计算框的边缘或角的坐标即可知道将箭头锚定在哪里。要使其像您所说的那样“增长”,只需对鼠标移动进行绑定并适当更新坐标即可。
要使线条可增长,您所要做的就是每次鼠标移动时调整线条的坐标。最简单的事情就是充分利用画布标签。通过标签,您可以知道哪些箭头连接到哪些框,以便在移动框时可以调整指向或来自该框的任何箭头的坐标。
Tkinter is perfectly fine for this sort of application. In the past I've worked on tools that were boxes connected with arrows that stayed connected as you move the boxes around (which is what I think you are asking about). Don't let people who don't know much about Tkinter sway you -- it's a perfectly fine toolkit and the canvas is very flexible.
The solution to your problem is simple math. You merely need to compute the coordinates of the edges or corners of boxes to know where to anchor your arrows. To make it "grow" as you say, simply make a binding on mouse movements and update the coordinates appropriately.
To make the line growable all you have to do is adjust the coordinates of the line each time the mouse moves. The easiest thing to do is make liberal use of canvas tags. With the tags you can know which arrows connect to which boxes so that when you move the box you adjust the coordinates of any arrows that point to or from it.