Change coords of line in python tkinter canvas

Thomas Sablik picture Thomas Sablik · Jun 5, 2018 · Viewed 9.9k times · Source

I've drawn a line in tkinter.Canvas and now I want to move one end. Is this possible, e.g. with itemconfig?

import tkinter

tk = tkinter.Tk()
canvas = tkinter.Canvas(tk)
canvas.pack()
line = canvas.create_line(0, 0, 100, 100)
tk.mainloop()

Now I want to change the end of the line to 75, 25. Is there a better way as to delete the line and create a new one?

Answer

TheSHETTY-Paradise picture TheSHETTY-Paradise · Jun 7, 2018

Once you've created some item on canvas you can use the following methods to edit the item:

  • coords( )
  • itemconfig( )
  • move( )

For example:

from tkinter import *

root = Tk()
w = Canvas(root, width=200, height=200)
w.pack()
var = w.create_line(0, 0, 100, 100)
w.coords(var, 0, 0, 75, 25)
root.mainloop()

For learning more about Canvas, you may refer: