How to connect two state circles with an arrow in tkinter? - python

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?

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.
from Tkinter import *
class CanvasDemo(Frame):
def __init__(self, width=200, height=200):
Frame.__init__(self, root)
self.canvas = Canvas(self)
self.canvas.pack(fill="both", expand="1")
self.canvas.create_rectangle(50, 25, 150, 75, fill="bisque", tags="r1")
self.canvas.create_line(0,0, 50, 25, arrow="last", tags="to_r1")
self.canvas.bind("<B1-Motion>", self.move_box)
self.canvas.bind("<ButtonPress-1>", self.start_move)
def move_box(self, event):
deltax = event.x - self.x
deltay = event.y - self.y
self.canvas.move("r1", deltax, deltay)
coords = self.canvas.coords("to_r1")
coords[2] += deltax
coords[3] += deltay
self.canvas.coords("to_r1", *coords)
self.x = event.x
self.y = event.y
def start_move(self, event):
self.x = event.x
self.y = event.y
root = Tk()
canvas = CanvasDemo(root)
canvas.pack()
mainloop()

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.

Related

Gui for Particlefilter with Python

I'm trying to implement a particle filter and I chose python for it because I kinda like python. By now i have written my gui using tkinter and python 3.4.
I use the tkinter.canvas object to display a map (png image loaded with PIL) and then i create dots for each particle like:
dot = canvas.create_oval(x, y, x + 1, y + 1)
When the robot moves I calculate the new position of each particle with the control command of the robot, the particles position and the particles alignment.
To move the particle tkinter.canvas has two methods:
canvas.move() canvas.coords()
But both methods seem to update the gui immediately which is OK when there are about 100 particles but not if there are 200 - 5000 (what I actually should have in the beginning for the global localization). So my problem is the performance of the gui.
So my actual question is: Is there a way in tkinter to stop the canvas from updating the gui, then change the gui and then update the gui again? Or can you recommend me a module that is better than tkinter for my use-case?
Your observation is incorrect. The canvas is not updated immediately. The oval isn't redrawn until the event loop is able to process events. It is quite possible to update thousands of objects before the canvas is redrawn. Though, the canvas isn't a high performance tool so moving thousands of objects at a high frame rate will be difficult.
If you are seeing the object being updated immediately it's likely because somewhere in your code you are either calling update, update_idletasks, or you are otherwise allowing the event loop to run.
The specific answer to your question, then, is to make sure that you don't call update or update_idletasks, or let the event loop process events, until you've changed the coordinates of all of your particles.
Following is a short example. When it runs, notice that all of the particles move at once in one second intervals. This is because all of the calculations are done before allowing the event loop to redraw the items on the canvas.
import Tkinter as tk
import random
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.canvas = tk.Canvas(self, width=500, height=500, background="black")
self.canvas.pack(fill="both", expand=True)
self.particles = []
for i in range(1000):
x = random.randint(1, 499)
y = random.randint(1, 499)
particle = self.canvas.create_oval(x,y,x+4,y+4,
outline="white", fill="white")
self.particles.append(particle)
self.animate()
def animate(self):
for i, particle in enumerate(self.particles):
deltay = (2,4,8)[i%3]
deltax = random.randint(-2,2)
self.canvas.move(particle, deltax, deltay)
self.after(30, self.animate)
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()

Python tkinter: how to restrict mouse cursor within canvas?

I use tkinter's canvas to load an image and draw a vector on top of it (using create_line).
I would like to restrict the mouse movement when drawing this vector, so that it cannot be dragged outside of the image area, whatever it may be. The mouse cursor should just snap back to image boundaries.
I tried searching, and found various ways of dealing with this, ideally this would need to be cross-platform. So far, I couldn't make any of those various ways working... so I'm kindly asking for help! Thank you :)
OK in the end I decided not to restrict mouse cursor physically (by forcing it not to go beyond certain coordinates), but rather virtually (by storing the mouse position to a variable, then if-elseing it around the bounding box that it needed to stay in). So the mouse cursor goes wherever it wants, but when it's actually drawing something in - it stays within the designated area I want it to.
Drawing lines on a Canvas was the task, over the loaded image. Line shouldn't pass by the boundaries of the image. This is how it worked out:
imgsize = (int(self.viewport.cget('width')) - 1,int(self.viewport.cget('height')) - 1)
# limit the draggable mouse area to just the image dimensions
if event.x < 4:
currentx = 4
elif event.x > imgsize[0]:
currentx = imgsize[0]
else:
currentx = event.x
if event.y < 4:
currenty = 4
elif event.y > imgsize[1]:
currenty = imgsize[1]
else:
currenty = event.y
Then from that point onward it's create_line time.

Python GUI changing the width of a line

I'm programming a drawing application in a GUI canvas.
I need to make it possible for the user to change the Width of the line in the canvas by using a spinbox.
You're not ever getting the value of the spinbox, or using the value to draw/redraw a line. Modify your add_point() function like so:
def add_point(self, event):
#Use color[1] to get the second element in the color tuple.
self.canvas.create_line(self.prev_x, self.prev_y, event.x, event.y, fill=color[1], width=self.spinbox1.get())
self.prev_x = event.x
self.prev_y = event.y
Now you can pick a color, draw a line, change the spinbox's value, and draw another line with a different thickness (width). See here for all the arguments you can pass to the create_line() method.

Python circle - moves and detects obstacles (moves alone, not with arrows)

I want to make a circle move and avoid obstacles with collision detection. Here's the code I have.
from Tkinter import *
window = Tk()
canvas = Canvas(window,width=800, height=500, bg='pink')
canvas.pack()
finishline = canvas.create_oval(700, 300, 700+50, 300+50,fill='green')
robot = canvas.create_oval(20,200,20+45,200+45,fill='yellow')#(x1, y1, x2, y2)
ob1 = canvas.create_rectangle(200,400,200+50,200+1,fill='black')
canvas.update()
ob1 = canvas.create_rectangle(500,200,150+400,300+100,fill='blue')
canvas.update()
You could always use the canvas.find_overlapping method (found here: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.find_overlapping-method), and if it ever returns a value other than your own little circle, you can make the circle go in the other direction or something.
It's kind of hard to give a specific answer with such unspecific requirements.

How do I selectively delete drawings in Tkinter?

I am drawing a lot of moving particles in a stationary box with Tkinter. My box is always there and does not change as time goes by, whereas the particles need to be updated.
My first intuition is to delete ALL the things (both particles and the box) and then redraw everything.
canvas.delete(ALL)
It indeed works, but the frame updates get extremely slow. This is because my box is of an irregular shape, which implies that I have to draw the box dot by dot. So this delete-everything-and-redraw-everything method is unsatisfactory.
I wish that the box is drawn only once, and only the particles get deleted and redrawn (updated). How should I do this?
Suppose you have a rectangle on canvas:
canvas.create_rectangle(x0, y0, x1, y1)
This would return a handle, so if you keep track of it,
myRectangle = canvas.create_rectangle(x0, y0, x1, y1)
canvas.delete(myRectangle)
This will delete only the myRectangle object.
Another way of doing it is to use tags.
canvas.create_rectangle(x0, y0, x1, y1, tags="myRectangle")
canvas.delete("myRectangle")
What you need to do is assign the drawings to variables, and then delete those. The below script demonstrates this:
from Tkinter import Button, Canvas, Tk
root = Tk()
canvas = Canvas()
canvas.grid()
drawing1 = canvas.create_oval((10,50,20,60), fill="red")
drawing2 = canvas.create_oval((30,70,40,80), fill="blue")
Button(text="Kill 1", command=lambda: canvas.delete(drawing1)).grid()
Button(text="Kill 2", command=lambda: canvas.delete(drawing2)).grid()
root.mainloop()
In addition to ALL, the delete method can also accept a specific drawing.

Categories

Resources