본문으로 바로가기

Tk의 캔버스(canvas)

category 카테고리 없음 2025. 8. 15. 01:14

캔버스 위젯

캔버스 위젯은 도형을 그리기 위한 위젯입니다. 캔버스 내에 그린 도형은 PostScript 형식의 파일로 출력할 수 있습니다. 아래는 그릴 수 있는 아이템입니다.

  • arc - 호
  • bitmap - 비트맵  
  • image - 이미지  
  • line - 선  
  • oval - 타원  
  • polygon - 다각형  
  • rectangle - 사각형  
  • text - 텍스트  
  • window - 위젯  

arc 

호를 그립니다. -start로 시작 각도를 지정합니다. 0은 시계의 3시 방향을 나타냅니다. -extent로 시계 반대 방향의 종료 각도를 지정합니다. -style로 호의 스타일을 pieslice, chord, arc 중에서 선택할 수 있습니다.

pack [canvas .c -width 100 -height 100 -bg blue]  
.c create arc 20 20 80 80 -start 0 -extent 270 -fill yellow  
.c create arc 20 20 80 80 -start 270 -extent 90 -fill red

bitmap  

비트맵을 그립니다. -bitmap으로 비트맵을 지정합니다. 

pack [canvas .c -width 100 -height 100 -bg blue]  
.c create bitmap 30 30 -bitmap questhead -foreground red  
.c create bitmap 70 70 -bitmap hourglass -foreground yellow

image  

이미지를 그립니다.  -image로 이미지를 지정합니다.  

image create photo foo -file c:/temp/hana.gif
pack [canvas .c -width 120 -height 100 -bg blue]  
.c create image 5 50 -image foo -anchor w

line  

선을 그립니다. -arrow로 화살표를 그릴 수 있습니다.  

pack [canvas .c -width 100 -height 100 -bg blue]  
.c create line 20 15 60 55 -fill red -arrow first  
.c create line 50 15 90 55 -fill green -arrow last  
.c create line 35 50 75 90 -fill yellow -arrow both

oval  

원 또는 타원을 그립니다.  

pack [canvas .c -width 100 -height 100 -bg blue]  
.c create oval 20 15 60 55 -fill red  
.c create oval 50 15 90 55 -fill green  
.c create oval 35 50 75 90 -fill yellow

polygon  

다각형을 그립니다. -smooth로 스무딩을 할지 지정할 수 있으며, -splinesteps로 스무딩 정도를 지정할 수 있습니다.  

pack [canvas .c -width 100 -height 100 -bg pink]  
.c create polygon 50 27 37 16 17 22 10 50 50 85 50 85 \
	90 50 82 22 62 16 50 27 50 27 -smooth true -splinesteps 5 -fill red

rectangle  

정사각형 또는 직사각형을 그립니다.  

pack [canvas .c -width 100 -height 100 -bg blue]  
.c create rectangle 20 20 80 80 -fill yellow  
.c create rectangle 40 40 60 60 -fill red

text  

텍스트를 그립니다.  -text로 문자열을 지정합니다.  

pack [canvas .c -width 100 -height 100 -bg blue]  
.c create text 30 30 -text Hello -anchor w -fill white  
.c create text 30 60 -text World! -anchor w -fill yellow

window  

위젯을 그립니다. -window로 위젯의 경로를 지정합니다.  

pack [canvas .c -width 100 -height 100 -bg blue]  
button .c.b -text Push -width 5  
entry .c.e -width 6  
.c create window 20 30 -window .c.b -anchor w  
.c create window 20 60 -window .c.e -anchor w

아이템 공통 옵션

-dash pattern 점선 패턴을 지정합니다.
-activedash pattern 점선 패턴을 지정합니다.
-disableddash pattern  점선 패턴을 지정합니다.
-dashoffset offset  dash의 시작 오프셋을 지정합니다.
-fill color    도형의 색상을 지정합니다.
-activefill color 도형의 색상을 지정합니다.
-disabledfill color 도형의 색상을 지정합니다.
-outline color  윤곽선의 색상을 지정합니다.
-activeoutline color  윤곽선의 색상을 지정합니다.
-disabledoutline color   윤곽선의 색상을 지정합니다.
-offset offset stipple의 오프셋을 지정합니다.
-outlinestipple bitmap   outline에 비트맵을 지정합니다.
-activeoutlinestipple bitmap    outline에 비트맵을 지정합니다.
-disabledoutlinestipple bitmap  outline에 비트맵을 지정합니다.
-stipple bitmap  fill에 비트맵을 지정합니다.
-activestipple bitmap   fill에 비트맵을 지정합니다.
-disabledstipple bitmap fill에 비트맵을 지정합니다.
-state state    상태를 지정합니다. (normal, disabled, hidden 중 하나)
-tags tagList   아이템에 태그명을 붙입니다.
-width outlineWidth  윤곽선의 두께를 지정합니다.
-activewidth outlineWidth   윤곽선의 두께를 지정합니다.
-disabledwidth outlineWidth  윤곽선의 두께를 지정합니다.

태그 

캔버스에 올린 아이템에는 순서대로 번호가 붙지만, 태그명을 붙이면 태그명으로 아이템을 조작할 수 있습니다. 하나의 아이템에 여러 개의 태그명을 붙일 수 있습니다. 또한, 여러 아이템에 같은 태그명을 붙일 수도 있습니다. 다음은 캔버스 위젯 내의 도형에 태그명을 붙이고, 그 도형의 윤곽선 색상을 변경하는 바인딩 예제 입니다..

pack [canvas .c -height 70 -width 70 -bg white]
.c create oval 10 10 40 40 -fill red -width 4 -tag foo
.c create oval 30 10 60 40 -fill green -width 4 -tag foo
.c create oval 20 30 50 60 -fill blue -width 4 -tag foo

.c bind foo <Enter> {
    %W itemconfigure foo -outline gold
}

.c bind foo <Leave> {
    %W itemconfigure foo -outline black
}

태그명을 붙일 때 주의할 점은, 캔버스 위젯에서는 all이라는 이름의 태그명은 모든 태그를 가리키기 위해 예약되어 있습니다.

PostScript  

캔버스는 PostScript 형식의 파일로 출력할 수 있습니다. 단, 한글 텍스트나 window는 PostScript로 변환할 수 없습니다.

.c postscript -file foo.ps