Tkinter Scrollbar

Tkinter
Scrollbar helps user to scroll and view the entire content.
Scrollbar(parent window, options .. )
Scrollbar in Tkinter GUI
import tkinter as tk
from tkinter import *
my_w=tk.Tk()
my_w.geometry("350x200")
sb = tk.Scrollbar(my_w,cursor='hand1')
sb.pack(side=RIGHT,fill=Y) 
l1 = tk.Listbox(my_w,height=8,width=90,
	bg='yellow', yscrollcommand = sb.set )
l1.pack(side=LEFT,padx=15) 
for i in range(1, 40): # multiple lines added to Listbox
    l1.insert(END, "Line No : " + str(i))
sb.config( command = l1.yview )

my_w.mainloop()

Options

In above code we used cursor='hand' to change the cursor shape when it is over the scrollbar. You can get a list of curshor shapes in our button widget.

orient

Scrollbar orient option in Tkinter GUI
By default the option orient is 'vertical' , we can set it to horizontal to display the Scrollbar in horizontal direction.
sb = tk.Scrollbar(my_w,orient='horizontal')

Using Scrollbar with Listbox

We will use grid layout to place the widgets
Tkinter Listbox with Scrollbar using grid layout
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("300x200")  # Size of the window 
my_w.title("plus2net.com")
l1=tk.Label(my_w,text='Listbox with Scroll bar')
l1.grid(row=1,column=1,columnspan=2,pady=10)
sb = tk.Scrollbar(my_w)
sb.grid(row=2, column=2,  sticky='e')
my_list=['PHP','Python','MySQL','HTML','Jquery','Java','CSS','Perl']
l1 = tk.Listbox(my_w,height=2, yscrollcommand = sb.set)
l1.grid(row=2,column=1,padx=20,pady=20) 

for element in my_list: # adding elements to Listbox
    l1.insert(tk.END,element)
sb.config( command = l1.yview )
my_w.mainloop()  # Keep the window open

Using Scrollbar with Text

We will use grid to place the widgets and one text box is used for inputs
Tkinter text with Scrollbar
import tkinter  as tk 
my_w = tk.Tk()
my_w.geometry("400x200")  

sb = tk.Scrollbar(my_w)
sb.grid(row=1, column=2,  sticky='w')

t1 = tk.Text(my_w, height=3, width=35) 
t1.grid(row=1,column=1,padx=20,pady=20)

my_str1='1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14'
t1.insert(tk.END, my_str1)

sb.config( command = t1.yview )
my_w.mainloop()
OptionMenu Projects in Tkinter

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer