แถบเลื่อนใช้เพื่อตัดข้อความหรืออักขระในกรอบหรือหน้าต่าง มีวิดเจ็ตข้อความเพื่อให้มีอักขระได้มากเท่าที่ผู้ใช้ต้องการ
แถบเลื่อนสามารถเป็นได้สองประเภท:แถบเลื่อนแนวนอนและแถบเลื่อนแนวตั้ง
ความยาวของแถบเลื่อนจะเปลี่ยนเมื่อใดก็ตามที่จำนวนอักขระในวิดเจ็ตข้อความเพิ่มขึ้น เราสามารถกำหนดสไตล์ของ Scrollbar ได้โดยใช้ ttk.Scrollbar . Ttk มีคุณสมบัติและแอตทริบิวต์ในตัวมากมายที่สามารถใช้กำหนดค่าแถบเลื่อนได้
ตัวอย่าง
ในตัวอย่างนี้ เราจะเพิ่มแถบเลื่อนแนวตั้งในวิดเจ็ตข้อความ เราจะใช้ ธีมสไตล์ ttk เพื่อปรับแต่งรูปลักษณ์ของแถบเลื่อน เราใช้ธีม 'คลาสสิก' ที่นี่ อ้างอิงลิงค์นี้สำหรับรายการธีม ttk ทั้งหมด
# 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("700x250")
style=ttk.Style()
style.theme_use('classic')
style.configure("Vertical.TScrollbar", background="green", bordercolor="red", arrowcolor="white")
# Create a vertical scrollbar
scrollbar = ttk.Scrollbar(win, orient='vertical')
scrollbar.pack(side=RIGHT, fill=BOTH)
# Add a Text Widget
text = Text(win, width=15, height=15, wrap=CHAR,
yscrollcommand=scrollbar.set)
for i in range(1000):
text.insert(END, i)
text.pack(side=TOP, fill=X)
# Configure the scrollbar
scrollbar.config(command=text.yview)
win.mainloop() ผลลัพธ์
การเรียกใช้โค้ดด้านบนจะแสดงหน้าต่างพร้อมวิดเจ็ตข้อความและแถบเลื่อนแนวตั้งที่ปรับแต่งได้
