Canvas widget is one of the most widely used widgets for graphical representation in a Tkinter application. To display a line in the Canvas widget, we can use the built-in library method create_line(x1,y1,x2,y2, **options).
We can also specify the type of line using the dash property. To change the line type from solid to dash dynamically, we can use configure() method. By passing an empty value to the dash property, we can change the line from solid to dash.
Example
Let us take an example to see how it works.
# Import the required libraries from tkinter import * from tkinter import ttk # Create an instance of tkinter frame or window win=Tk() # Set the size of the tkinter window win.geometry("700x350") def update_line(): canvas.itemconfig(line, dash=()) # Create a canvas widget canvas=Canvas(win, width=400, height=300) canvas.pack() # Create a line canvas.create_line(300, 30, 300, 150, dash=(4, 2), width=5) # create a button to change the dash property of the line ttk.Button(win, text="Change", command=update_line) win.mainloop()
Output
If we run the above code, it will display a dashed line inside the Canvas widget.