How to put Scrollbar inside a Listbox in Tkinter? - python

how can i move the scrollbar inside a listbox? See the photo attached i used a red arrow for explain, here is my code:
from tkinter import *
window = Tk()
window.geometry('500x500')
window.config(bg='#3c3f41')
window.state('zoomed')
frame_listbox = Frame(window,bg='#3c3f41')
frame_listbox.pack(side=LEFT)
listbox = Listbox(frame_listbox,font=('Helvetica',30))
listbox.pack(padx=400)
scrollbar = Scrollbar(window)
scrollbar.pack(side=RIGHT,fill=Y)
scrollbar.config(command=listbox.yview)
listbox.config(yscrollcommand=scrollbar.set)
window.mainloop()
I tried to put a scrollbar inside a Listbox, i expect to understand if it's possible or not

It is recommended to put the scrollbar just to the right of the listbox instead of inside it. To achieve it:
make the scrollbar child of frame_listbox instead of window
move option padx=400 from scrollbar.pack(...) to frame_listbox.pack(...)
pack listbox to the LEFT side
from tkinter import *
window = Tk()
window.geometry('500x500')
window.config(bg='#3c3f41')
window.state('zoomed')
frame_listbox = Frame(window,bg='#3c3f41')
frame_listbox.pack(side=LEFT,padx=400) # added padx=400
listbox = Listbox(frame_listbox,font=('Helvetica',30))
listbox.pack(side=LEFT) # removed padx=400 and added side=LEFT
scrollbar = Scrollbar(frame_listbox) # changed parent to frame_listbox
scrollbar.pack(side=RIGHT,fill=Y)
scrollbar.config(command=listbox.yview)
listbox.config(yscrollcommand=scrollbar.set)
window.mainloop()

Does this will Help? By using grid instead place.
import tkinter as tk
root = tk.Tk()
root.title("Listbox Operations")
# create the listbox (note that size is in characters)
listbox1 = tk.Listbox(root, width=50, height=6)
listbox1.grid(row=0, column=0)
# create a vertical scrollbar to the right of the listbox
yscroll = tk.Scrollbar(command=listbox1.yview, orient=tk.VERTICAL)
yscroll.grid(row=0, column=1, sticky=tk.N+tk.S)
listbox1.configure(yscrollcommand=yscroll.set)
for i in range(1, 50):
listbox1.insert(tk.END, i)
root.mainloop()
Result:

Related

Tkinter Scrollbar not scrolling

I have some code which is creating a scrollable Tkinter canvas with buttons inside. I believe my configuration of the scrollbar and Canvas is ok, but when I run the program, the scrollbar does not scroll. I have looked at other solutions online and I believe I am fulfilling everything that should be applied.
Thank you for any and all help:)
from tkinter import *
root = Tk()
responsesFr = Frame(root, width=700, height=275, bg="#d40b04")
responseFrCoverCanvas = Canvas(root, width=700,height=14 ,highlightthickness=0, border=0,bg="#d40b04")
w = Canvas(responsesFr, width=650, height=225, borderwidth=0,highlightthickness=0,background="white")
w.config(scrollregion=[0,0,1000,225])
for column in range(30):
button1 = Button(w, width=20, height=4)
button1.pack(side='left', padx=25)
if column == 29:
w.config(scrollregion=[0,0,1000,1000])
print("done")
else:
pass
w.pack(padx=25,pady=10)
hbar=Scrollbar(responsesFr, orient=HORIZONTAL)
hbar.pack(padx=25, side=TOP, fill=X)
hbar.config(command=w.xview)
w.config(xscrollcommand=hbar.set)
responsesFr.pack()
root.mainloop()
You can't scroll things added with pack. You must use the canvas create_window method to add widgets to the canvas.

Why does this scrollbar not work after inserting widgets into a listbox?

Basically, I am trying to add a scrollbar to a window containing widgets. I am able to successfully add a scrollbar to a Listbox widget and after inserting stuff, the program works just the way I want it to. But, I face a problem when I place widgets into the Listbox. The scrollbar appears but it seems to be disabled.
from tkinter import *
root = Tk()
root.geometry("640x480")
root.resizable(0,0)
myscrollbar = Scrollbar(root)
myscrollbar.pack(side=RIGHT, fill=Y)
mylist = Listbox(root, width=640, height=480, yscrollcommand=myscrollbar.set)
mylist.pack(fill=BOTH, expand=True)
for x in range(1, 101):
mylist.insert(END, Label(mylist, text="Label: "+str(x)).grid(row=x, column=0))
myscrollbar.config(command = mylist.yview)
root.mainloop()
Any way to fix this code?
from tkinter import *
def myScrollcmd(event):
mycanvas.config(scrollregion=mycanvas.bbox('all'))
root = Tk()
mycanvas = Canvas(root)
mycanvas.pack(fill=BOTH, expand=True)
myFrame = Frame(mycanvas)
mycanvas.create_window((0, 0), window=myFrame, anchor=NW)
myScrollbar = Scrollbar(mycanvas, orient=VERTICAL, command=mycanvas.yview)
myScrollbar.pack(side=RIGHT, fill=Y)
mycanvas.config(yscrollcommand=myScrollbar.set)
mycanvas.bind("<Configure>", myScrollcmd)
for x in range(100):
Label(myFrame, text="Text "+str(x)).pack()
root.mainloop()
This works. However, there is one problem that might not be a major one. The problem is, when my cursor is on the canvas, and I'm moving my mouse wheel, the scrollbar doesn't budge. However the scroll bar moves along with my mouse wheel when my cursor is on top of the scroll bar. I can drag the scroll box to scroll up and down and use the scroll buttons to scroll up and down but the mouse wheel only works when my cursor is hovering over the scrollbar widget.

Python listbox expand and hide after clicks

I have the following sample code below. Is there a way to expand the listbox, showing 10 out of 100 numbers when clicked? And when selecting one of the numbers, does the listbox hide the others again?
from tkinter import *
root = Tk()
scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
listbox = Listbox(root)
listbox.pack()
for i in range(100):
listbox.insert(END, i)
# attach listbox to scrollbar
listbox.config(yscrollcommand=scrollbar.set, height = 1)
scrollbar.config(command=listbox.yview)
mainloop()
You can connect an event handler to the <Button-1> signal, and toggle the listbox height every time:
def listbox_clicked(event):
listbox = event.widget
if listbox['height'] == 1:
listbox.config(height=10)
else:
listbox.config(height=1)
listbox.bind('<Button-1>', listbox_clicked)
I believe packing options will help.
scrollbar.pack(side=RIGHT, fill=Y, expand=True)
listbox.pack(fill=Y, expand=True)
Thanks alot for the help, but all I needed was a combobox!

Scrollbars Tkinter

I have two listboxes one next to the other and I want each of them to have horizontal and vertical scrollbars. I have managed to create them, but when I press the arraws on the scrollbars, nothing happens, and I cannot see the entire content.
This is the code I have so far:
from Tkinter import *
root = Tk()
frame4 = Frame(root)
frame4.grid(row=2,columnspan=2,sticky=E+W)
l5 = Label(frame4, text='Output:').grid(row=0,columnspan=2)
frame5 = Frame(frame4)
frame5.grid(row=1,column=0,sticky=E+W)
l6 = Label(frame5, text='Algo1:').pack()
yscroll1 = Scrollbar(frame5, orient=VERTICAL)
xscroll1 = Scrollbar(frame5, orient=HORIZONTAL)
output_algo = Listbox(frame5,height=5, width=40)
output_algo.config (yscrollcommand=xscroll1.set)
output_algo.config (yscrollcommand=yscroll1.set)
yscroll1.config (command=output_algo.yview)
xscroll1.config (command=output_algo.yview)
yscroll1.pack(side=RIGHT, fill=Y,expand=0)
xscroll1.pack(side=BOTTOM, fill=X,expand=0)
output_algo.pack(side=LEFT,fill=BOTH,expand=1)
frame6 = Frame(frame4)
frame6.grid(row=1,column=1,sticky=E+W)
l7 = Label(frame6, text='Algo2:').pack()
yscroll2 = Scrollbar(frame6, orient=VERTICAL)
xscroll2 = Scrollbar(frame6, orient=HORIZONTAL)
output_opt = Listbox(frame6,height=5, width=40)
output_opt.config (yscrollcommand=xscroll2.set)
output_opt.config (yscrollcommand=yscroll2.set)
yscroll2.config (command=output_opt.yview)
xscroll2.config (command=output_opt.yview)
yscroll2.pack(side=RIGHT, fill=Y,expand=0)
xscroll2.pack(side=BOTTOM, fill=X,expand=0)
output_opt.pack(side=LEFT,fill=BOTH,expand=1)
root.mainloop()
How can I change it for the scrollbars to work?
Also, I read that using grid in the case of scrollbars is better than pack. If I were to modify my code to use grid would I have to create a frame which contains only the listbox and the scrollbar?
Likely copy and paste was not your friend:
output_algo.config (yscrollcommand=xscroll1.set)
should be
output_algo.config (xscrollcommand=xscroll1.set)
and
xscroll1.config (command=output_algo.yview)
should be
xscroll1.config (command=output_algo.xview)
And of course you'll need to do the same for the other Listbox :)
You can't scroll empty Listbox.
I added some elements to listbox and I could scroll up and down.
for item in range(30):
output_algo.insert(END, str(item)*item)
output_opt.insert(END, str(item)*item)
root.mainloop()
There is some problem with scrolling left to right but I saw mistake in xscroll1.config()

Python scrollbar on text widget

I'm trying to get a simple scrollbar to show up on the text widget...I've derived the following code which adds the scrollbar but seems to eliminate the text widget completely.
eula = Tkinter.Text(screen1_2)
eula.insert("1.0", "text")
eula.pack(side="left",fill="both",expand=True)
scroll = Tkinter.Scrollbar(eula)
scroll.pack(side="right",fill="y",expand=False)
Note that you must define yscrollcommand=scroll.set in the Text widget, and configure the scrollbar using the line: scroll.config(command=eula.yview)
Here is your code, rewritten with one scrollbar:
# Import Tkinter
from Tkinter import *
# define master
master = Tk()
# Vertical (y) Scroll Bar
scroll = Scrollbar(master)
scroll.pack(side=RIGHT, fill=Y)
# Text Widget
eula = Text(master, wrap=NONE, yscrollcommand=scroll.set)
eula.insert("1.0", "text")
eula.pack(side="left")
# Configure the scrollbars
scroll.config(command=eula.yview)
mainloop()
Here is a Tkinter example with two scrollbars:
# Import Tkinter
from Tkinter import *
# define master
master = Tk()
# Horizontal (x) Scroll bar
xscrollbar = Scrollbar(master, orient=HORIZONTAL)
xscrollbar.pack(side=BOTTOM, fill=X)
# Vertical (y) Scroll Bar
yscrollbar = Scrollbar(master)
yscrollbar.pack(side=RIGHT, fill=Y)
# Text Widget
text = Text(master, wrap=NONE,
xscrollcommand=xscrollbar.set,
yscrollcommand=yscrollbar.set)
text.pack()
# Configure the scrollbars
xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)
# Run tkinter main loop
mainloop()

Categories

Resources