본문 바로가기
SW 코딩/GUI, tkinter

[파이썬 python] tkinter - listbox 리스트박스

2021. 9. 5.
반응형

 

 

참고사이트: 유튜브 나도코딩

code - 창, 크기 조절하기 

5. 리스트박스 listbox.txt
0.00MB

code 설명

import tkinter as tk

root = tk.Tk()  # tkinter root창 생성

root.title("계산기") #창 이름
root.geometry("500x500+200+200") # 창 크기, 가로 x 세로 + 창 출력 위치 좌표

'''
height=0 : list숫자만큼 자동으로 생성
height=3 : list 3개만 보이게 생성
'''
listbox = tk.Listbox(root, selectmode="extend", height=0)
listbox.insert(0, "구글")
listbox.insert(1, "애플")
listbox.insert(2, "삼성")
listbox.insert(tk.END, "아마존")
listbox.insert(tk.END, "테슬라")
listbox.pack()


def command_del():
    '''
    리스트 박스 삭제하기
    '''
    #listbox.delete(0) # 첫 번째 삭제
    listbox.delete(tk.END) # 마지막 삭제

def command_count():
    '''
    리스트 박스 개수 확인
    '''
    print(f"리스트 박스에는 {listbox.size()} 개가 존재")

def command_read():
    '''
    리스트 항목 확인 (시작, 끝)
    '''
    print(f"첫번째부터 세번째가지의 항목은{listbox.get(0,2)} 입니다")

def command_return_index():
    '''
    선택된 항목 위치 index로 반환하기
    '''
    print(f"선택된 항목의 index 값은 {listbox.curselection()} 입니다.")



button1 = tk.Button(root, text="삭제", command=command_del)
button1.pack()

button2 = tk.Button(root, text="리스트 총 개수", command=command_count)
button2.pack()

button3 = tk.Button(root, text="읽기", command=command_read)
button3.pack()

button4 = tk.Button(root, text="선택 index 갖고오기", command=command_return_index)
button4.pack()


root.mainloop()

 

결과 화면

댓글


loading