Add Scrollbars to List Boxes - Python Tkinter GUI Tutorial #62

preview_player
Показать описание
In this video we'll look at List Boxes in more detail. I'll show you how to add a scrollbar to a Listbox and I'll also show you how to select multiple items in your List Box and do things with them.

Listboxes don't have scrollbars by default. We need to use a scrollbar widget and add both it and the list box to a frame, and then configure them both.

I'll also show you to to select multiple items in a list box by changing the select mode.
Рекомендации по теме
Комментарии
Автор

▶️ Watch Entire Tkinter Playlist ✅ Subscribe To My YouTube Channel:
▶️ See More At: ✅ Join My Facebook Group:

Codemycom
Автор

Really appreciate your videos man!!!
The pace of these tutorials are great and the tips on the nuances of the tkinter library has saved me so much time in writing code in my own project.

noobmeky
Автор

Hey! I just wanted to say HUGE THANKS! you are doing great work, and if ever you feel like stopping, Dont. It's because of people like you that people like us thrive, and learn. God Bless You!

krishnaraj
Автор

Your tutorials are awesome. I always search for helpful videos on your channel. I love them. Please make more videos so that more people can smash the like button.

deeptishikha
Автор

I have learn a ton from your tutorial. I found that your videos are a lot better than docs. I hope you will consider doing videos on pygame.

khaihoang
Автор

Thanks for such a great tutorial!, You deserve all the best!

insertxk_dev
Автор

very nice, i updated the code so all the buttons work since it was not doing what it was supposed to. I took your code and redid the select_all and the select since it was not doing it's thing as your described it. Your video helped me get started and thinking on how to solve the problem so it works.

from tkinter import *

root = Tk()
root.title('List Boxes')
root.geometry("400x600")

#Create frame and scrollbar
my_frame = Frame(root)
my_scrollbar = Scrollbar(my_frame, orient=VERTICAL)

#Listbox!
#Single, Browse, Multiple, Extended can set it for, when deleting making a list
my_listbox = Listbox(my_frame, width = 50, yscrollcommand=my_scrollbar.set, selectmode=MULTIPLE)
#configure scrollbar

my_scrollbar.pack(side=RIGHT, fill=Y)
my_frame.pack()
my_listbox.pack(pady=15)

#Add item to listbox
my_listbox.insert(END, "This is an item")
my_listbox.insert(END, "Second Item!")


#Add list of items
my_list = ["One", "Two", "Three", "One", "Two", "Three", "One", "Two", "Three", "One", "Two", "Three", "One", "Two", "Three"]

for item in my_list:
my_listbox.insert(END, item)

def delete():
for item in # deletes last item first
my_listbox.delete(item)
my_label.config(text='')

def select():#create a new variable and string together things on seperate lines
result = ''
for item in (my_listbox.curselection()):
result = result + str(my_listbox.get(item)) + '\n'
my_label.config(text=result)

def delete_all():
my_listbox.delete(0, END) #range of what to delete

def select_all():
my_listbox.select_set(0, END)

def delete_multiple(): #loops through and the list changes need to use reverse, starts at the beginning deletes the 0th item, then the next item is 1 and the list is changed
for item in #deletes last item first
my_listbox.delete(item)
my_label.config(text='')


global my_label
my_label = Label(root, text='')
my_label.pack(pady=5)

my_button = Button(root, text="Delete", command=delete)
my_button.pack(pady=10)

my_button2 = Button(root, text="Select", command=select)
my_button2.pack(pady=10)

my_button3 = Button(root, text="Delete All", command=delete_all)
my_button3.pack(pady=10)

my_button4 = Button(root, text="Select All", command=select_all)
my_button4.pack(pady=10)

my_button5 = Button(root, text="Delete Multiple", command=delete_multiple)
my_button5.pack(pady=10)

root.mainloop()

fentonmsu
Автор

Thank you very much You are the best! I'll nenver forget you

jaguermandozi
Автор

It's really a greate job, thank you very much, i fix the problem of scrollbar

haoranzheng
Автор

Wonderful sir ...great teaching.... continue...

sivasurya
Автор

Dear Sir, could you please explain how to add a scrollbar into a optionmenu, so I wonder how to make a compact visualisation of a list which has a scrollbar as it is too long, but in a way that the user sees one item at the time and he can scroll up and down or in a way that the window with the scrolling enlarges only if the user clicks on that option. Many thanks in advance

francesca
Автор

Great series. Do you have a video on dependent drop down list boxes? For instance, if drop down list box #1 contains states, I want to create a second drop down box (#2) that contains the counties of each respective state. Say if I clicked on Colorado in the first box, then I would want to be able to click on a list of counties in Colorado in the second box. Been cruising your channel and the i-net for info on this; surprising, the topic is somewhat hard to find. Cheers

vincenzo
Автор

In the previous video you mention that you have seen it where people need to use "end" and in other cases you don't. Not sure if this is what you were talking about, but it seems to be because of the imports.

If you import Tkinter by itself like "import tkinter as tk" then to use things like END or ANCHOR you need to include tk.END. However, the option "end" works either way.

deepfrieddevelopment
Автор

Hi,
Thank you so much for your videos.
I have one question,
How can i add scroll bar to the whole root i.e.( Entire widow ) ?

shrinidhimohandeshpande
Автор

I have a question. I know how to add vertical scrollbar in Listbox and also how to add horizontal one. I also know how to add vertical scrollbar to Text widget (as in ScrolledText widget). But I don't know how to add horizontal bar in Text widget (because whenever I type text more than the whole line in Text widget, it inserts text into newline ("\n")). Do you know how to make rest of the text go in one line only until I don't press Enter key?

psirohi
Автор

Just wondering if I am using grid system to position all the widgets, How should I position the scrollbar right next to my text box. I always have some space between the text box and the scrollbar. Thanks

jiangwenxing
Автор

Hello! Your video helps me so much! I have one question. How can I configure the shape of the scrollbar? I already used relief option, but nothing changed. And is it possible to use the image on scrollbar?

barthollomu
Автор

i reckon you should firstly have used sorted, after reversed function cause we don't know how user will choose items in listbox

wtcetko
Автор

Me again, I'm curious if you have a tutorial video explaining how to add both Vertical AND Horizontal scrollbars to a listbox frame like you showed in this video using the same frame or do you have to create 2 seprate frames? Thanks again for all the kick ass videos!

EDIT:

I did a bit of tinkering (trial and error) and I came up with this based off this video. It works probably not the best method, but I got it to work.

CODE:

list_frame = Frame(root, background="Cyan")
scrollbar = Scrollbar(list_frame, orient=VERTICAL)
scrollbarh = Scrollbar(list_frame, orient=HORIZONTAL)


listbox = Listbox(list_frame, background="Cyan", width=20, yscrollcommand=scrollbar.set, xscrollcommand=scrollbar.set)

scrollbar.pack(side=RIGHT, fill=Y)


scrollbarh.pack(side=BOTTOM, fill=X)

list_frame.pack()

LIKE I SAID B4, THANK YOU!

spikespike
Автор

Hello, I have a question. For example I learn tkinter pretty well and already write own app. Where I can publish my app?

gamerstrim