[Python 파이썬 독학 활용2편 2일차] GUI(tkinter) - 2-1
[2일차] 공부내용 정리
12. 메뉴
13. 메시지 박스
14. 프레임
15. 스크롤 바
16. 그리드 기본
17. 그리드 심화
18. 퀴즈 (메모장 만들기)
12. 메뉴
from tkinter import *
root = Tk()
root.title("WT GUI")
root.geometry("640x480") # 가로 X 세로 / 대문자X 하면 실행안됨
def create_new_file():
print("새 파일을 만듭니다.")
menu = Menu(root)
# File 메뉴
menu_file = Menu(menu, tearoff=0)
menu_file.add_command(label="New File", command=create_new_file)
menu_file.add_command(label="New Window")
menu_file.add_separator()
menu_file.add_command(label="Open File...")
menu_file.add_separator()
menu_file.add_command(label="Save All", state = "disable") # 비활성화
menu_file.add_separator()
menu_file.add_command(label="Exit", command = root.quit) # 비활성화
menu.add_cascade(label="File", menu=menu_file)
# Edit 메뉴 (빈값)
menu.add_cascade(label="Edit")
# Language 메뉴 추가 (radio 버튼을 택해서 택1)
menu_lang = Menu(menu, tearoff=0)
menu_lang.add_radiobutton(label="Python")
menu_lang.add_radiobutton(label="Java")
menu_lang.add_radiobutton(label="C++")
menu.add_cascade(label="Language", menu=menu_lang)
# View 메뉴 (Checkbox)
menu_view = Menu(menu, tearoff=0)
menu_view.add_checkbutton(label="Show Minimap")
menu.add_cascade(label="View", menu=menu_view)
root.config(menu=menu)
root. mainloop()
주의사항
- menu = Menu(root)
: munu 항목 변수 선언 , Menu(root) 와 같이 대소문자 구별하여 변수 사용되어야 함.
- menu_file = Menu(menu, tearoff=0)
: 메뉴 이름 = Menu(menu, 파라미터1, 파라미터2, ...) 의 형태로 메뉴 항목 사용
- menu_file.add_command(label="New File", command=create_new_file)
menu_file.add_command(label="New Window")
: add_command 를 통해 하위 항목의 추가 및 command를 부여할 수 있다.
- menu_file.add_separator()
: 동일 메뉴안에서 구분자 표시
- menu_file.add_command(label="Save All", state = "disable") # 비활성화
: state = "disable" 로 설정할 경우 해당 버튼의 상태가 비활성화 된다.
- menu.add_cascade(label="File", menu=menu_file)
: 상위메뉴와 하위메뉴를 연결(해당 cascade 선언이 안될 경우 해당 항목이 실행창에서 나타나지 않음)
- menu_lang = Menu(menu, tearoff=0)
menu_lang.add_radiobutton(label="Python")
menu_lang.add_radiobutton(label="Java")
menu_lang.add_radiobutton(label="C++")
menu.add_cascade(label="Language", menu=menu_lang)
: 메뉴 항목에서 radiobutton(택 1을 할 경우 사용) 사용 가능
- menu_view = Menu(menu, tearoff=0)
menu_view.add_checkbutton(label="Show Minimap")
menu.add_cascade(label="View", menu=menu_view)
: 메뉴 항목에서 Checkbox 사용 가능
- root.config(menu=menu)
: 실행 창(root)에서 해당 항목을 추가하여야 메뉴바가 나타남
: 윈도우창.config(menu=메뉴이름) 형태로 사용
실제 파라미터가 뭔가 종류가 많다...
사용법은 어렵지 않아 보이나 상황에 맞게 쓸 수 있어야 할 것 같다.
Menu Method
이름의미
add_command(파라미터) | 기본 메뉴 항목 생성 |
add_radiobutton(파라미터) | 라디오버튼 메뉴 항목 생성 |
add_checkbutton(파라미터) | 체크버튼 메뉴 항목 생성 |
add_cascade(파라미터) | 상위 메뉴와 하위 메뉴 연결 |
add_separator() | 구분선 생성 |
add(유형, 파라미터) | 특정 유형의 메뉴 항목 생성 |
delete(start_index, end_index) | start_index부터 end_index까지의 항목 삭제 |
entryconfig(index, 파라미터) | index 위치의 메뉴 항목 수정 |
index(item) | item 메뉴 항목의 index 위치 반환 |
insert_separator (index) | index 위치에 구분선 생성 |
invoke(index) | index 위치의 항목 실행 |
type(속성) | 선택 유형 반환 (command, radiobutton, checkbutton, cascade, separator, tearoff) |
- Tip : 파라미터 중, : label=이름을 이용하여 메뉴의 이름을 설정할 수 있습니다.
- Tip : 메뉴 이름.add_cascade(label="상위 메뉴 이름", menu=연결할 상위 메뉴)를 이용하여 메뉴를 부착할 수 있습니다.
Menu Parameter
메뉴 형태 설정
이름의미기본값속성
relief | 메뉴의 테두리 모양 | flat | flat, groove, raised, ridge, solid, sunken |
background=bg | 메뉴의 배경 색상 | SystemButtonFace | color |
foreground=fg | 메뉴의 문자열 색상 | SystemButtonFace | color |
selectcolor | 하위 메뉴의 선택 표시(√) 색상 | SystemWindow | color |
메뉴 형식 설정
이름의미기본값속성
font | 메뉴의 문자열 글꼴 설정 | TkDefaultFont | font |
cursor | 메뉴의 마우스 커서 모양 | - | * 커서 속성(최하단 확인) |
메뉴 상태 설정
이름의미기본값속성
activeborderwidth | active 상태일 때 메뉴의 테두리 두께 | 1 | 상수 |
activebackground | active 상태일 때 메뉴의 배경 색상 | SystemHighlight | color |
activeforeground | active 상태일 때 메뉴의 문자열 색상 | SystemButtonText | color |
disabledforeground | disabeld 상태일 때 메뉴의 문자열 색상 | SystemDisabledText | color |
메뉴 동작 설정
이름의미기본값속성
postcommand | 메뉴가 선택되었을 때 실행하는 메소드(함수) | - | 메소드, 함수 |
tearoff | 하위메뉴의 분리 기능 사용 유/무 | False | Boolean |
title | 하위메뉴의 분리 기능의 제목 | - | 문자열 |
tearoffcommand | 메뉴의 위젯 일치화 여부 | - | 메소드, 함수 |
참고
- 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
menu option 관련 참고 자료 :
https://076923.github.io/posts/Python-tkinter-8/