Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Tkinter를 사용하여 그리드 내에서 스크롤 가능한 목록 상자 만들기

<시간/>

목록 상자 위젯은 숫자 목록, 항목 목록, 회사의 직원 목록 등과 같은 항목 목록을 표시합니다. 목록 상자의 긴 항목 목록을 창 내에서 볼 방법이 필요한 경우가 있을 수 있습니다. 이를 위해 Scrollbar() 객체를 초기화하여 목록 상자 위젯에 스크롤 막대를 연결할 수 있습니다. 목록 상자를 구성하고 스크롤 막대와 함께 연결하면 목록 상자를 스크롤할 수 있게 됩니다.

예시

이 예에서는 1에서 100 사이의 숫자 목록으로 목록 상자를 만들 것입니다. 목록 상자 위젯에는 연결된 스크롤 막대가 있습니다.

#Import the required libraries
from tkinter import *
from tkinter import ttk

#Create an instance of Tkinter Frame
win = Tk()

#Set the geometry of Tkinter Frame
win.geometry("700x350")

#Create an object of Scrollbar widget
s = Scrollbar()

#Create a horizontal scrollbar
scrollbar = ttk.Scrollbar(win, orient= 'vertical')
scrollbar.pack(side= RIGHT, fill= BOTH)

#Add a Listbox Widget
listbox = Listbox(win, width= 350, font= ('Helvetica 15 bold'))
listbox.pack(side= LEFT, fill= BOTH)

#Add values to the Listbox
for values in range(1,101):
   listbox.insert(END, values)

listbox.config(yscrollcommand= scrollbar.set)

#Configure the scrollbar
scrollbar.config(command= listbox.yview)

win.mainloop()

출력

위의 코드를 실행하면 스크롤 가능한 목록 상자가 포함된 창이 표시됩니다.

Tkinter를 사용하여 그리드 내에서 스크롤 가능한 목록 상자 만들기