简体   繁体   中英

Live drawing on tkinter canvas

I need to draw many lines/polygons on a canvas but i want to display the canvas first and then see every object while is drawn.

Here is my code:

root = tk.Tk()
canvas = tk.Canvas(root, height=800, width=800)
canvas.pack()

draw_something(canvas)

root.mainloop()

The problem is that python shows the canvas after drawing everything and this is not what i want.

In the following example, 10 random lines will be created every second on the canvas.

import tkinter as tk
import random

def make_segment():
    return [random.randrange(0, 800) for _ in range(4)]

def draw_random_lines():
    canvas.create_line(*make_segment())
    root.after(100, draw_random_lines)

root = tk.Tk()
canvas = tk.Canvas(root, height=800, width=800)
canvas.pack()

draw_random_lines()

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM