Scrollbar and Scale Widget in Tkinter
Theory
1. Tkinter Scrollbar:
- A scrollbar allows users to navigate through content that is too large to fit in the available
window.
- It can be associated with widgets like Text, Canvas, or Listbox.
2. Tkinter Scale:
- A scale widget provides a graphical slider to select a numeric value.
- You can set minimum, maximum values and orient the slider horizontally or vertically.
Python Program
import tkinter as tk
def display_scale_value(val):
label.config(text=f"Scale Value: {val}")
# Create main window
root = tk.Tk()
root.title("Scrollbar and Scale Example")
# Frame for Text widget with scrollbar
frame = tk.Frame(root)
frame.pack(padx=10, pady=10)
# Create Text widget
text = tk.Text(frame, wrap="none", width=40, height=10)
text.pack(side="left")
# Create Scrollbar for Text widget
scrollbar = tk.Scrollbar(frame, command=text.yview)
scrollbar.pack(side="right", fill="y")
text.config(yscrollcommand=scrollbar.set)
# Add sample text to demonstrate scrolling
for i in range(1, 101):
text.insert("end", f"This is line {i}\n")
# Create Scale widget
scale = tk.Scale(root, from_=0, to=100, orient="horizontal", command=display_scale_value)
scale.pack(pady=10)
# Label to display scale value
label = tk.Label(root, text="Scale Value: 0")
label.pack()
# Run the application
root.mainloop()