Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Gắn thanh cuộn vào Listbox thay vì cửa sổ trong Tkinter

Listbox chứa danh sách mục. Danh sách dài cần thanh cuộn. Thêm Scrollbar vào Listbox bằng Listbox(...) constructor và Scrollbar(...).

Ví dụ

# Import thư viện
from tkinter import *
from tkinter import ttk

# Tạo khung
win = Tk()
win.geometry("700x350")

# Thanh cuộn dọc
scrollbar = ttk.Scrollbar(win, orient='vertical')
scrollbar.pack(side=RIGHT, fill=BOTH)

# Listbox
listbox = Listbox(win, width=350, bg='bisque')
listbox.pack(side=LEFT, fill=BOTH)

for values in range(100):
    listbox.insert(END, values)

listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)

win.mainloop()

Kết quả

Listbox với 100 mục, thanh cuộn dọc gắn trực tiếp vào Listbox.

Gắn thanh cuộn vào Listbox thay vì cửa sổ trong Tkinter