■ Canvas 클래스의 create_rectangle 메소드를 사용해 임의 사각형들을 그리는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import random from tkinter import * def drawRandomRectangle(canvas, width, height): x1 = random.randrange(width) y1 = random.randrange(height) x2 = random.randrange(x1 + random.randrange(width)) y2 = random.randrange(y1 + random.randrange(height)) canvas.create_rectangle(x1, y1, x2, y2) tk = Tk() canvas = Canvas(tk, width = 500, height = 500) canvas.pack() for i in range(0, 100): drawRandomRectangle(canvas, 500, 500) |