แถบเลื่อนมีประโยชน์ในการจัดเตรียมลักษณะการทำงานแบบไดนามิกในแอปพลิเคชัน ในแอปพลิเคชัน Tkinter เราสามารถสร้างแถบเลื่อนแนวตั้งและแนวนอนได้ แถบเลื่อนถูกสร้างขึ้นโดยการเริ่มต้นวัตถุของ Scrollbar() วิดเจ็ต
ในการสร้างแถบเลื่อนแนวนอน เราต้องจัดเตรียมการวางแนว เช่น "แนวนอน" หรือ "แนวตั้ง" แถบเลื่อนสามารถเข้าถึงได้เมื่อเรากำหนดค่าวิดเจ็ตเฉพาะด้วยแถบเลื่อน
ตัวอย่าง
#Import the required libraries
from tkinter import *
#Create an instance of Tkinter Frame
win = Tk()
#Set the geometry of Tkinter Frame
win.geometry("700x350")
#Create some dummy Text
text_v = "Python is dynamically-typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly, procedural), object-oriented and functional programming."
text_h = ("\nNASA \n Google \nNokia \nFacebook \n Netflix \n Expedia \n Reddit \n Quora \n MIT\n Udemy \n Shutterstock \nSpotify\nAmazon\nMozilla\nDropbox")
#Add a Vertical Scrollbar
scroll_v = Scrollbar(win)
scroll_v.pack(side= RIGHT,fill="y")
#Add a Horizontal Scrollbar
scroll_h = Scrollbar(win, orient= HORIZONTAL)
scroll_h.pack(side= BOTTOM, fill= "x")
#Add a Text widget
text = Text(win, height= 500, width= 350, yscrollcommand= scroll_v.set,
xscrollcommand = scroll_h.set, wrap= NONE, font= ('Helvetica 15'))
text.pack(fill = BOTH, expand=0)
text.insert(END, text_v)
text.insert(END, text_h)
#Attact the scrollbar with the text widget
scroll_h.config(command = text.xview)
scroll_v.config(command = text.yview)
win.mainloop() ผลลัพธ์
การเรียกใช้โค้ดด้านบนจะแสดงหน้าต่างที่มีบริบทเกี่ยวกับภาษาการเขียนโปรแกรม Python สามารถดูบริบทแบบไดนามิกได้โดยใช้แถบเลื่อนแนวนอนและแนวตั้ง
