Python listbox expand and hide after clicks - python

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!

Related

How to put Scrollbar inside a Listbox in Tkinter?

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:

Tkinter Enter and Motion bindings

Is it possible to track what widgets I enter with my mouse into while it's pressed?
I want to create a chain-like effect that the background of the label\button change while click and drag the mouse and moving from widget to widget.
Thanks :)
You can bind to the <B1-Motion> event, and then use winfo_containing to get the widget under the cursor.
Here's a simple example:
import tkinter as tk
root = tk.Tk()
current_label = tk.Label(root, text="", anchor="w", width=100)
current_label.pack(side="top", fill="x")
def show_widget(event):
widget = event.widget.winfo_containing(event.x_root, event.y_root)
current_label.configure(text=f"widget: {str(widget)}")
for x in range(10):
name = f"Label #{x+1}"
label = tk.Label(root, text=name)
label.pack(padx=10, pady=10)
label.bind("<B1-Motion>", show_widget)
root.mainloop()
I've done stuff like tracking entry/exit for a specific widget:
widget.bind("<Enter>", enter_func)
widget.bind("<Leave>", exit_func)
you may be able to do something cute with that

How to disable a TKinter scrollbar widget?

I am writing a single thread program with a GUI, which executes a series of tasks. During these tasks the GUI is refreshed regularly to check for a few available inputs (e.g. abort). To avoid halting the task with unwanted inputs all unnecessary GUI elements are disabled by .config(state='disabled') during execution.
This however does not seem work for scrollbars which for some reason are unique and don't have a "-state" option.
Another possible approach to get the scroll bar to display with a disabled look (instead of being hidden) is to:
Remove all entries in the list (the scroll bar will become disabled)
Detach the scroll bar from the list
Re-insert the removed entries with the scroll bar detached (the scroll bar will remain disabled)
In the code below, 'listbox' is my tk.Listbox object,'scrollbar' is the associated 'tk.Scrollbar' object, and self is a reference to the parent object. Code tested on Window with Python 3.8.4rc1.
Disable code:
entries = listbox.get(0, "end")
listbox.delete(0, "end")
self.update()
listbox.config(yscrollcommand="")
scrollbar.config(command="")
listbox.insert("end", *entries)
listbox['state'] = "disabled"
Enable code:
listbox['state'] = "normal"
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
I think you can't disable the scrollbar widget using the -state option, I've searched in the Tcl/Tk website and I didn't find anything about it.
But I am sure you can use the pack_forget() method to delete it from the window if you are using the pack geometry or the grid_forget() if you are using the grid geometry, so you can later show the scrollbar widget again calling pack/grid again.
Here's a example:
from tkinter import *
master = Tk()
scrollbar = Scrollbar(master)
scrollbar.pack(side=RIGHT, fill=Y)
def disable_scroll():
scrollbar.pack_forget()
def active_scroll():
scrollbar.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=None)
btn1 = Button(master, text="OK 1", command=disable_scroll)
btn1.pack()
btn2 = Button(master, text="OK 2", command=active_scroll)
btn2.pack()
listbox = Listbox(master, yscrollcommand=scrollbar.set)
for i in range(1000):
listbox.insert(END, str(i))
listbox.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=listbox.yview)
mainloop()
The Scrollbar changes according the Listbox's values, so as I said here's the code:
from tkinter import *
import random
master = Tk()
scrollbar = Scrollbar(master)
scrollbar.pack(side=RIGHT, fill=Y)
def disable_scroll():
scrollbar.pack_forget()
def active_scroll():
scrollbar.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=None)
btn1 = Button(master, text="disable_scroll", command=disable_scroll)
btn1.pack()
btn2 = Button(master, text="active_scroll", command=active_scroll)
btn2.pack()
listbox = Listbox(master, yscrollcommand=scrollbar.set)
#NEW FUNCTIONS
def count():
listbox.delete(0, END)
lista = [5, 8, 500, 1000]
for i in range(random.choice(lista)):
listbox.insert(END, str(i))
def delete():
listbox.delete(0, END)
#NEW BUTTONS
btn3 = Button(master, text="start", command=count)
btn3.pack()
btn4 = Button(master, text="delete", command=delete)
btn4.pack()
listbox.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=listbox.yview)
mainloop()

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.

Categories

Resources