Python tkinter Menubutton 위젯에 대해 살펴보겠습니다.
Menubutton 은 Menu기능을 가진 button입니다.
Menubutton 만들기
Menubutton은 tkinter에 Menubutton(window) 함수로 생성합니다.
매개변수로 Menubutton이 출력될 window를 넣어줘야 합니다.
import tkinter
win = tkinter.Tk();
menuBtn = tkinter.Menubutton(win, text='Menubutton')
menuBtn.pack();
win.mainloop();
위 코드를 실행하면 버튼이 하나 생성됩니다, 그냥 생성하면 일반 버튼과 다를 게 없습니다.
Menubutton 이기 때문에 Menu와 연결이 가능합니다.
import tkinter
win = tkinter.Tk();
menuBtn = tkinter.Menubutton(win, text='Menubutton')
menuBtn.pack();
menu = tkinter.Menu(menuBtn, tearoff=0);
menu.add_command(label='menu1');
menu.add_command(label='menu2');
menu.add_command(label='menu3');
menuBtn['menu']=menu;
win.mainloop();
메뉴를 하나 만든 뒤에 Menubutton의 menu 옵션으로 해당 메뉴를 연결하였습니다.
이제 Menubutton을 클릭하면 연결된 메뉴가 출력되게 됩니다.
Menubutton의 옵션들
1. Menubutton의 동작 관련
이름 | 기능 | 기본값 | 속성 |
state | Menubutton 상태설정 | normal | normal, active, disabled |
menu | Menubutton이 선택되었을때 나타날 Menu위젯 | tkinter.Menu() | |
direction | Menubutton이 선택되었을때 나타날 Menu위젯의 방향 | below | below, above, flush, left, right |
takefocus | Tab키로 위젯이동 허용여부 | True | Boolean |
indicatoron | Menubutton의 위젯 일치화 여부 | False | Boolean |
2. Menubutton의 문자열 관련
이름 | 기능 | 기본값 | 속성 |
text | Menubutton에 출력할 문자열 | ||
textvariable | Menubutton에 출력할 문자열변수 | 변수 | |
font | Menubutton내 문자열 글꼴 | TkDefaultFont | font |
anchor | Menubutton내 문자열 또는 이미지의 위치 | center | n, ne, nw, s, se, sw, e, w, center |
justify | Menubutton내 문자열 정렬 | center | center, left, right |
wraplength | Menubutton내 자동 줄내림 설정 넓이 | 0 | 숫자 |
3. Menubutton의 이미지 관련
이름 | 기능 | 기본값 | 속성 |
image | Menubutton에 출력할 이미지 | ||
bitmap | Menubutton에 출력할 기본 이미지 | info, error, warning, question, questhead, hourglass, gray75, gray50, gray25 | |
compound | Menubutton에 문자열과 이미지가 동시에 있을때 출력할 이미지의 위치 | none | top, center, bottom, left, right, none |
4. Menubutton의 디자인 관련
이름 | 기능 | 기본값 | 속성 |
width | Menubutton의 길이 | 0 | 숫자 |
height | Menubutton의 높이 | 0 | 숫자 |
foreground | Menubutton의 문자열 색상 | SystemButtonFace | color |
background | Menubutton의 배경색상 | SystemButtonFace | color |
relief | Menubutton의 테두리 모양 | flat | groove, raised, flat, ridge, sunken, solid |
padx | Menubutton의 가로여백 ( padding ) | 5 | 숫자 |
pady | Menubutton의 세로여백 ( padding ) | 4 | 숫자 |
cursor | Menubutton의 마우스 커서 설정 | arrow, pointer 등 cursor속성 | |
activeforeground | active상태일때 Menubutton의 문자열 색상 | SystemButtonText | color |
activebackground | active상태일때 Menubutton의 배경색상 | SystemButtonFace | color |
disabeldforeground | disabled상태일때 Menubutton의 문자열 색상 | SystemDisabledText | color |
highlightcolor | Menubutton이 선택되었을때 하이라이트 색상 | SystemWindowFrame | color |
highlightbackground | Menubutton이 선택되지 않았을때 하이라이트 색상 | SystemButtonFace | color |
highlightthickness | Menubutton이 선택되었을때 하이라이트 두께 | 0 | 숫자 |