상위 목록: 하위 목록: 작성 날짜: 읽는 데 3 분 소요

Canvas(캔버스)

Canvas를 이용하여 선, 다각형, 원 등을 그리기 위한 캔버스를 생성할 수 있습니다.



Canvas 사용

import tkinter

window=tkinter.Tk()
window.title("YUN DAE HEE")
window.geometry("640x400+100+100")
window.resizable(False, False)

canvas=tkinter.Canvas(window, relief="solid", bd=2)

line=canvas.create_line(10, 10, 20, 20, 20, 130, 30, 140, fill="red")
polygon=canvas.create_polygon(50, 50, 170, 170, 100, 170, outline="yellow")
oval=canvas.create_oval(100, 200, 150, 250, fill="blue", width=3)
arc=canvas.create_arc(100, 100, 300, 300, start=0, extent=150, fill='red')

canvas.pack()

window.mainloop()


canvas=tkinter.Canvas(window, relief="solid", bd=2)

line=canvas.create_line(10, 10, 20, 20, 20, 130, 30, 140, fill="red")
polygon=canvas.create_polygon(50, 50, 170, 170, 100, 170, outline="yellow")
oval=canvas.create_oval(100, 200, 150, 250, fill="blue", width=3)
arc=canvas.create_arc(100, 100, 300, 300, start=0, extent=150, fill='red')

canvas.pack()

tkinter.Canvas(윈도우 창, 매개변수1, 매개변수2, 매개변수3, ...)을 사용하여 해당 윈도우 창에 표시할 캔버스의 속성을 설정할 수 있습니다.

매개변수를 사용하여 캔버스의 속성을 설정합니다.

메서드를 이용하여 캔버스에 도형을 그릴 수 있습니다.

캔버스의 좌표는 왼쪽 상단 모서리가 (0, 0)이며, 오른쪽으로 갈수록 x가, 아래쪽으로 갈수록 y가 커집니다.

create_* 메서드는 생성된 도형의 ID(정수)를 반환하며, 이 ID를 이용하여 도형을 수정하거나 삭제할 수 있습니다.



Canvas Method

이름 의미 설명
create_line(x1, y1, x2, y2, … , xn, yn, option) 선 (x1, y1), (x2, y2), …, (xn, yn) 까지 연결되는 선 생성
create_rectangle(x1, y1, x2, y2, option) 사각형 (x1, y1)에서 (x2, y2)의 크기를 갖는 사각형 생성
create_polygon(x1, y1, x2, y2, … , xn, yn, option) 다각형 (x1, y1), (x2, y2), …, (xn, yn) 의 꼭지점을 갖는 다각형 생성
create_oval(x1, y1, x2, y2, option) 원 (x1, y1)에서 (x2, y2)의 크기를 갖는 원 생성
create_arc(x1, y1, x2, y2, start, extent, option) 호 (x1, y1)에서 (x2, y2)의 크기를 가지며 start 각도부터 extent의 각을 지니는 호 생성
create_image(x, y, image, option) 이미지 (x, y) 위치의 image 생성
create_text(x, y, text, option) 문자열 (x, y) 위치에 text 문자열 생성
coords(ID, x1, y1, x2, y2, …) 좌표 변경 ID 도형의 좌표를 변경
move(ID, dx, dy) 도형 이동 ID 도형을 (dx, dy)만큼 이동
itemconfig(ID, option) 속성 변경 ID 도형의 option 변경
delete(ID) 도형 삭제 ID 도형 삭제, "all"을 전달하면 모든 도형 삭제


  • option

    • fill : 배경 색상

    • outline : 테두리 색상

    • width : 테두리 두께

    • dash : 점선 설정, 예) dash=(4, 2)

    • anchor : 위치 지정 (create_image, create_text)

    • tags : 도형에 부여할 태그 이름, 여러 도형을 하나의 태그로 묶어 한 번에 제어 가능


  • Tip : 이외에도 여러 option이 존재

  • Tip : create_polygon은 fill의 기본값이 black이고 outline의 기본값이 비어 있으므로, 테두리만 표시하려면 fill=""로 설정해야 합니다.

  • Tip : create_arc의 start와 extent는 도(degree) 단위이며, start는 3시 방향이 0도이고 반시계 방향으로 증가합니다. style 옵션으로 pieslice(기본값), chord, arc 모양을 선택할 수 있습니다.

  • Tip : create_image에 사용하는 이미지는 tkinter.PhotoImage로 생성하며, 자세한 내용은 22강 바로가기에서 확인할 수 있습니다.



Canvas Parameter

캔버스 형태 설정

이름 의미 기본값 속성
width 캔버스의 너비 10c(약 378) 상수
height 캔버스의 높이 7c(약 265) 상수
relief 캔버스의 테두리 모양 flat flat, groove, raised, ridge, solid, sunken
borderwidth=bd 캔버스의 테두리 두께 0 상수
background=bg 캔버스의 배경 색상 SystemButtonFace color
offset 캔버스의 오프셋 설정 0,0 x, y, n, e, w, s, ne, nw, se, sw


캔버스 형식 설정

이름 의미 기본값 속성
cursor 캔버스의 마우스 커서 모양 - 커서 속성
xscrollcommand 캔버스의 가로스크롤 위젯 적용 - Scrollbar위젯.set
yscrollcommand 캔버스의 세로스크롤 위젯 적용 - Scrollbar위젯.set
xscrollincrement 캔버스 가로스크롤의 증가량 0 상수
yscrollincrement 캔버스 세로스크롤의 증가량 0 상수
scrollregion 캔버스 스크롤 영역 크기 설정 - (x1, y1, x2, y2)
confine 캔버스의 스크롤 영역 내 제한 True Boolean


캔버스 하이라이트 설정

이름 의미 기본값 속성
highlightcolor 캔버스가 선택되었을 때 색상 SystemWindowFrame color
highlightbackground 캔버스가 선택되지 않았을 때 색상 SystemButtonFace color
highlightthickness 캔버스가 선택되었을 때 두께 (두께 설정) 2 상수


캔버스 동작 설정

이름 의미 기본값 속성
takefocus Tab 키를 이용하여 위젯 이동 허용 여부 - Boolean


참고

  • width와 height의 기본값은 10c, 7c(센티미터 단위)이며, 화면 해상도에 따라 픽셀 값이 달라짐

  • takefocus를 설정하지 않으면 키 바인딩이 있을 때에만 Tab 키로 이동할 수 있도록 자동으로 결정됨

  • highlightthickness의 기본값이 2이므로, 캔버스의 테두리를 완전히 없애려면 highlightthickness=0으로 설정해야 함

  • cursor 매개변수

    • arrow, based_arrow_down, based_arrow_up, boat, bogosity, bottom_left_corner, bottom_right_corner, bottom_side, bottom_tee, box_spiral, center_ptr, circle, clock, coffee_mug, cross, cross_reverse, crosshair, diamond_cross, dot, dotbox, double_arrow, draft_large, draft_small, draped_box, exchange, fleur, gobbler, gumby, hand1, hand2, heart, icon, iron_cross, left_ptr, left_side, left_tee, leftbutton, ll_angle, lr_angle, man, middlebutton, mouse, pencil, pirate, plus, question_arrow, right_ptr, right_side, right_tee, rightbutton, rtl_logo, sailboat, sb_down_arrow, sb_h_double_arrow, sb_left_arrow, sb_right_arrow, sb_up_arrow, sb_v_double_arrow, shuttle, sizing, spider, spraycan, star, target, tcross, top_left_arrow, top_left_corner, top_right_corner, top_side, top_tee, trek, ul_angle, umbrella, ur_angle, watch, wait, xterm, X_cursor
  • highlightbackground를 설정하였을 경우, 캔버스가 선택되지 않았을 때에도 두께가 표시됨

댓글 남기기