วิดเจ็ต Scrollbar ใน Tkinter เป็นหนึ่งในวิดเจ็ตที่มีประโยชน์ ซึ่งใช้สำหรับจัดการองค์ประกอบคอนเทนเนอร์และเนื้อหาของมันพร้อมกับแถบเลื่อน ด้วยแถบเลื่อน เราสามารถดูชุดข้อมูลขนาดใหญ่ได้อย่างมีประสิทธิภาพ
โดยทั่วไป Tkinter รองรับการเพิ่มแถบเลื่อนแนวตั้งและแนวนอน ในการเพิ่มแถบเลื่อนแนวนอนในแอปพลิเคชัน เราต้องกำหนดค่า orientation เป็น 'horizontal' ในคอนสตรัคเตอร์ของ Scrollbar
ตัวอย่าง
มาสร้างโปรแกรมแก้ไขข้อความที่มีแถบเลื่อนแนวนอนกัน
# Import the required library
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
# Create an instance of tkinter frame
win = Tk()
# Set the geometry
win.geometry("700x350")
# Add a Scrollbar (horizontal)
h = Scrollbar(win, orient='horizontal')
h.pack(side=BOTTOM, fill='x')
# Add a text widget
text = Text(win, font=("Calibri", 16), wrap=NONE, xscrollcommand=h.set)
text.pack()
# Add some text in the text widget
for i in range(5):
text.insert(END, "Welcome to Tutorialspoint...")
# Attach the scrollbar with the text widget
h.config(command=text.xview)
win.mainloop()
ผลลัพธ์
เมื่อรันโค้ดด้านบน จะแสดงโปรแกรมแก้ไขข้อความที่มีข้อความอยู่บ้าง วิดเจ็ต Text จะถูกจัดวางพร้อมกับแถบเลื่อนแนวนอนซึ่งจะปรากฏเมื่อข้อความล้นออกนอกพื้นที่แสดง
