Python tkinter Entry ( 입력창 )

Python tkinter Entry 위젯을 만들어 보겠습니다.

Entry는 텍스트를 입력 또는 출력하는 기입창입니다.

Entry 만들기

Entry는 tikinter에 Entry(window) 함수로 생성합니다. 매개변수로 Entry가 표시될 window를 넣어줘야 합니다.

생성한 후 pack() 함수로 Entry를 window안에 배치합니다.

import tkinter
win = tkinter.Tk();

# Entry만들기 + 옵션설정
entry = tkinter.Entry(win,
    width = 5,
    height = 2);
    
# Entry 옵션설정
entry.config(borderwidth = 3);

# Entry 배치하기
entry.pack();

win.mainloop();

 Entry를 선언하면서 옵션을 설정해도 되고, config() 함수로 설정할 수 도 있습니다.

 

입력창을 통해 대부분은 입력받은 결과로 어떤 작업을 할 것입니다.

그러기 위해 bind() 함수를 이용해 event를 잡아 동작을 실행시킬 수 있습니다.

import tkinter
win = tkinter.Tk();

label = tkinter.Label(win);
label.config(text= 'hello, Label');
label.pack();

def showInput(event):
    label.config(text=entry.get());

entry = tkinter.Entry(win);
entry.bind("<Return>", showInput);

entry.pack();
win.mainloop();

위 예제를 보면 Entry에 엔터키 입력될 때마다 라벨의 값이 입력된 값으로 변경되는 것을 확인할 수 있습니다.

 

Entry 제공 함수

이름 기능
insert(index, '문자열') index위치에 문자열 추가
delete(start_index, end_index) start_index 부터 end_index 까지 삭제
get() Entry내 텍스트를 반환
icursor(index) index위치로 커서설정
select_adjust(index) 시작(첫글자)부터 index 위치까지 문자열 블록처리
select_range(start_index, end_index) start_index ~ end_index 블록처리
select_to(index) 현재위치한 커서부터 index 까지 블록처리
select_present() 현재 Entry내 블록처리 여부리턴 ( True / False )
select_clear() 블록처리 해제

 

Entry의 옵션들

1. Entry의 문자 관련

이름 기능 기본값 속성
show 화면상 표시되는 문자
(암호같은것 입력할때)
  문자 ( 문자열 입력해도 첫글자만 반영됨 )
textvariable 표시할 문자열 변수   변수
font Entry내 폰트 TKDefaultFont font
justify Entry내 문자열이 여러줄일때 정렬 center center, left, right

 

2. Entry의 디자인 관련

이름 기능 기본값 속성
width Entry의 길이 0 숫자
relief Entry의 테두리 설정 flat flat, solid, ridge, sunken, raised, groove
borderwidth Entry의 테두리 두께 2 숫자
background Entry의 배경색상 SystemButtonFace color
foreground Entry의 문자열 색상 SystemButtonText color
cursor Entry의 마우스 커서모양   pointer, arrow등 cursor 속성
disabledforeground disabled상태 Entry의 문자열 색상 SystemDisabledText color
disabledbackground disabled상태 Entry의 배경색상 SystemButtonFace color
readonlybackground readonly상태 Entry의 배경색상 SystemButtonFace color
highlightcolor Entry 선택되었을때 하이라이트 색상 SystemWindowFrame color
highlightbackground Entry 선택되지않았을때 하이라이트 색상 SystemButtonFace color
highlightthickness Entry 선택되었을때 두께 설정 0 숫자
insertwidth 커서의 길이 2 숫자
insertborderwidth 커서의 테두리 두께 0 숫자
insertbackground 커서의 색상 SystemWindowText color
selectborderwidth 문자열 블록처리시 테두리 두께 0 숫자
selectbackground 문자열 블록처리시 배경색상 SystemHighlight color
selectforeground 문자열 블록처리시 문자색상 SystemHighlight color

 

3. Entry의 동작 관련

이름 기능 기본값 속성
state 상태설정 normal normal, readonly, disalbed
takefocus Tab키를 이용 위젯이동 허용여부 True Boolean ( True / False )
insertofftime 커서 깜빡임이 보이지 않는 시간 300 숫자(ms)
insertontime 커서 깜빡임이 보이는시간 600 숫자(ms)