Python tkinter Radiobutton 위젯에 대해 살펴보겠습니다.
Radiobutton은 여러 옵션들 중 단일 선택하기 위한 버튼 위젯입니다.
Radiobutton 만들기
Radiobutton은 tkinter에 Radiobutton(window) 함수로 생성합니다.
매개변수로 Radiobutton이 출력될 window를 넣어줘야 합니다.
import tkinter
win = tkinter.Tk();
# Radiobutton 생성
rdoVar = tkinter.StringVar();
rdoBtn1 = tkinter.Radiobutton(win, text='Orange', variable=rdoVar);
rdoBtn2 = tkinter.Radiobutton(win, text='Apple', variable=rdoVar);
rdoBtn1.config(value='Orange');
rdoBtn2.config(value='Apple');
rdoBtn1.pack();
rdoBtn2.pack();
win.mainloop();
variable이 같은 Radiobutton 끼리 같은 그룹으로 묶여 같은 그룹 내에는 단일 선택만 가능합니다.
옵션은 다른 위젯들처럼 생성 시에 파라미터로 설정 또는 config() 함수로 설정할 수 있습니다.
Radiobutton 이 가장 많이 쓰일 때는 여러 옵션 중 단일 옵션 선택받고
선택받은 값을 가지고 어떤 동작을 하는 것입니다.
variable 속성을 통해 Radiobutton의 선택 값을 얻을 수 있습니다.
import tkinter
win = tkinter.Tk();
# Radiobutton 생성
rdoVar = tkinter.StringVar();
def currRdoValue():
print(rdoVar.get());
rdoBtn1 = tkinter.Radiobutton(win, text='Orange', variable=rdoVar);
rdoBtn2 = tkinter.Radiobutton(win, text='Apple', variable=rdoVar);
rdoBtn1.config(value='Orange', command=currRdoValue);
rdoBtn2.config(value='Apple', command=currRdoValue);
rdoBtn1.pack();
rdoBtn2.pack();
win.mainloop();
command 옵션을 이용해 선택되었을 때 currRdoValue() 함수를 실행시켰고
해당 함수는 현재 Radiobutton의 value가 variable 옵션 즉, rdoVar 변수에 들어있기에 해당 값을 출력시켰습니다.
( 아래쪽 옵션 참조 : variable에 들어가는 변수는 필히 tkinter.IntVar() 또는 tkinter.StringVar()처럼 tkinter 제공 함수로 생성해야 합니다. 또 value와 variable의 변수 자료형을 맞춰야 합니다. IntVar() = Int, StringVar() = String )
해당 함수로 생성된 변수는 container 이기 때문에 get() 함수로 값을 빼내서 출력해야 합니다.
Radiobutton의 값이 필요할 때 참조하면 될 것 같습니다.
Radiobutton의 함수들
이름 | 기능 |
select() | 상태를 체크로 변경 |
deselect() | 상태를 해제로 변경 |
invoke() | 해당 Radiobutton을 클릭한것과 같은 실행 |
flash() | 깜빡임 ( normal 상태 배경과 active 상태 배경 교차 ) |
Radiobutton의 옵션들
1. Radiobutton의 동작 관련
이름 | 기능 | 기본값 | 속성 |
state | Radiobutton의 상태 설정 | normal | normal, active, disabled |
command | Radiobutton이 active 상태일 때 실행할 함수 | 함수 | |
variable | Radiobutton의 상태를 저장할 변수 | tkinter.IntVar() tkinter.StringVar() tkinter.BooleanVar()... |
|
value | Radiobutton의 값 | 설정될 variable 의 따른 자료형 IntVar() - Int, StringVar() - String | |
indicatoron | Radiobutton의 위젯 일치화 여부 | True | Boolean |
takefocus | Tab키로 위젯 이동 허용여부 | True | Boolean |
2. Radiobutton의 문자열 관련
이름 | 기능 | 기본값 | 속성 |
text | Radiobutton에 출력할 문자열 | ||
textvariable | Radiobutton에 출력할 문자열변수 | 변수 | |
anchor | Radiobutton내 문자열 또는 이미지의 위치 | center | center, se, s, sw, w, nw, n, ne, e |
justify | Radiobutton내 문자열 정렬 | center | center, left, right |
wraplength | Radiobutton내 자동 줄내림 설정 넓이 | 0 | 숫자 |
font | Radiobutton내 문자열 글꼴 | TkDefaultFont | font |
3. Radiobutton의 이미지 관련
이름 | 기능 | 기본값 | 속성 |
image | Radiobutton내 출력할 이미지 | ||
bitmap | Radiobutton내 출력할 기본 이미지 | info, error, warning, questhead, question, hourglass, gray75, gray50, gray25 | |
selectimage | Radiobutton이 체크상태일 때 출력할 이미지 | ||
compound | Radiobutton 내 문자열과 이미지가 동시에있을때 이미지의 위치 | none | top, center, bottom, left, right, none |
4. Radiobuttom의 디자인 관련
이름 | 기능 | 기본값 | 속성 |
width | Radiobutton의 길이 | 0 | 숫자 |
height | Radiobutton의 높이 | 0 | 숫자 |
relief | Radiobutton의 테두리 설정 | flat | flat, raised, groove, ridge, sunken, solid |
overrelief | Radiobutton의 테두리 설정 (마우스를 올렸을때) |
raised | flat, raised, groove, ridge, sunken, solid |
foreground | Radiobutton의 문자열 색상 | SystemButtonFace | color |
background | Radiobutton의 배경 색상 | SystemButtonFace | color |
selectcolor | 선택된 Radiobutton의 배경 색상 | SystemWindow | color |
padx | Radiobutton내 가로 여백 ( padding ) | 1 | 숫자 |
pady | Radiobutton내 세로 여백 ( padding ) | 1 | 숫자 |
cursor | Radiobutton내 마우스 커서 설정 | pointer, arrow등 cursor속성 | |
activeforground | active 상태일때 Radiobutton의 문자열 색상 |
SystemButtonText | color |
activebackground | active 상태일때 Radiobutton의 배경 색상 |
SystemButtonFace | color |
disabledforeground | disabled 상태일때 Radiobutton의 문자열 색상 |
SystemDisabledText | color |
highlightcolor | Radiobutton이 선택되었을 때 하이라이트 색상 | SystemWindowFrame | color |
highlightbackground | Radiobutton이 선택되지 않았을때 하이라이트 색상 | SystemButtonFace | color |
highlightthickness | Radiobutton이 선택되었을 때 하이라이트 두께 | 0 | 숫자 |