Python tkinter Scale (숫자값 조정 바)

Python tkinter Scale에 대해 살펴보겠습니다.

Scale은 숫자 값을 조정하기 위한 바를 출력하는 위젯입니다.

 

Scale 만들기

Scale은 tkinter에 Scale(window) 함수로 생성합니다.

매개변수로 Scale이 생성될 window를 넣어줘야 합니다.

생성한 후 pack() 함수로 Scale을 화면에 배치합니다.

import tkinter
win = tkinter.Tk();

scale = tkinter.Scale(win);
scale.pack();

win.mainloop():

 

 

실제로는 Scale에 설정된 값으로 어떠한 동작을 하겠죠?

예시로 설정된값을 라벨에 출력해 보겠습니다.

import tkinter
win = tkinter.Tk();

def currNumber(self):
    value = "현재 숫자: " + str(scale.get());
    label.config(text=value);

scale = tkinter.Scale(win, command=currNumber, orient="horizontal");
scale.pack();

label = tkinter.Label(win, text="현재 숫자: 0");
label.pack();

win.mainloop():

Scale에 바가 이동되어 수치가 설정될 때마다 라벨의 값도 따라 바뀌게 됩니다.

어떠한 범위내에서 수치 값을 설정할 때 유용하게 사용할 수 있습니다.

(흔히들 옵션설정 같은 것 할 때 많이 보셨으리라 생각됩니다.)

 

Scale의 함수 및 옵션 살펴보면서 포스팅 마무리하겠습니다.

Scale의 함수들

이름 기능
set() Scale의 값을 설정
get() Scale의 값을 반환

 

Scale의 옵션들

1. Scale의 동작 관련

이름 기능 기본값 속성
command Scale이 active 상태일 때 실행될 함수   함수
variable Scale의 상태를 저장할 변수   tkinter.IntVar(), tkinter.StringVar()
takefocus Tab키를 이용하여 위젯 이동 False Boolean
repeatdelay Scale이 Active되고 command 실행까지 대기시간 300 숫자(ms)
repeatinterval Scale이 Active되고 command 실행의 반복시간 100 숫자(ms)
state Scale의 상태 설정 normal active, normal, disabled

 

2. Scale의 형태 관련

이름 기능 기본값 속성
resolution Scale내 수치 간격 1 숫자
from_ Scale의 최소값 0 숫자
to Scale의 최대값 100 숫자
tickinterval Scale내 값 출력할 간격 0 숫자

 

3. Scale의 디자인 관련

이름 기능 기본값 속성
width Scale내 슬라이더 넓이 15 숫자
sliderlength Scale내 슬라이더 길이 30 숫자
length Scale의 길이 100 숫자
orient Scale의 표시 방향 vertical vertical, horizontal
relief Scale의 테두리 모양 flat solid, flat, raised, ridge, groove, sunken
sliderrelief Scale내 슬라이더 테두리 모양 raised solid, flat, raised, ridge, groove, sunken
borderwidth Scale의 테두리 두께 0 숫자
background Scale의 배경색상 SystemButtonFace color
troughcolor Scale내부 바의 배경색상 SystemScrollbar color
font Scale의 문자열 글꼴 TkDefaultFont font
cursor Scale내 마우스 커서 모양   pointer, arrow등 cursor속성
activebackground Scale이 Active상태일 때 슬라이더 색상 SystemButtonFace color
highlightcolor Scale이 선택되었을 때 하이라이트 색상 SystemWindowFrame color
highlightbackground Scale이 선택되지 않았을 때 하이라이트 색상 SystemButtonFace color
highlightthickness Scale이 선택되었을 때 하이라이트 두께 2 숫자