tkinter Listbox item binding - python

I'm trying to show a context menu when an item in a Listbox widget is right clicked.
The problem is that if a bind to the listbox, the whole Listbox will be active for send the event and it doesn't seem possible to bind to the list items only. I can't use <<ListboxSelect>> because it will be trigged on left click. So I tried to use the methods curselection() but I fell into unwanted results (the right clicked item doesn't have to be selected). I think I need to simulate <<ListboxSelect>> using generate_event() and nearest(). Can someone tell me how to do that or maybe where can i found the defaults binding inside tkinter package ?

You will need to use nearest(event.y). Bind to right-click and popup the menu when the callback is invoked.
import Tkinter
def context_menu(event, menu):
widget = event.widget
index = widget.nearest(event.y)
_, yoffset, _, height = widget.bbox(index)
if event.y > height + yoffset + 5: # XXX 5 is a niceness factor :)
# Outside of widget.
return
item = widget.get(index)
print "Do something with", index, item
menu.post(event.x_root, event.y_root)
root = Tkinter.Tk()
aqua = root.tk.call('tk', 'windowingsystem') == 'aqua'
menu = Tkinter.Menu()
menu.add_command(label=u'hi')
listbox = Tkinter.Listbox()
listbox.insert(0, *range(1, 10, 2))
listbox.bind('<2>' if aqua else '<3>', lambda e: context_menu(e, menu))
listbox.pack()
root.mainloop()

Related

Get dynamically which button uses function

I have a question about buttons and binds, but it's better if I show you.
from tkinter import Tk,Button
root = Tk()
startbutton = Button(root,text="start button")
pingbutton = Button(root,text="ping button")
startbutton.pack()
pingbutton.pack()
def startenterbind(e):
startbutton.config(relief='sunken')
def startleavebind(e):
startbutton.config(relief='raised')
def pingenterbind(e):
pingbutton.config(relief='sunken')
def pingleavebind(e):
pingbutton.config(relief='raised')
startbutton.bind("<Enter>", startenterbind)
startbutton.bind("<Leave>", startleavebind)
pingbutton.bind("<Enter>", pingenterbind)
pingbutton.bind("<Leave>", pingleavebind)
root.mainloop()
This is my code, now I am wondering, is there a better way to do this?
Maybe it's possible to get which button was hovered dynamically, to then change the button that was hovered?
This is so I can use one function for multiple buttons, while only affecting the one being <Enter>'d or <Leave>'d?
You can reuse an event handler function by making use of the event object they are passed which has an attribute telling you the widget that triggered it.
from tkinter import Tk,Button
root = Tk()
startbutton = Button(root,text="start button")
pingbutton = Button(root,text="ping button")
startbutton.pack()
pingbutton.pack()
def startenterbind(event):
event.widget.config(relief='sunken')
def startleavebind(event):
event.widget.config(relief='raised')
startbutton.bind("<Enter>", startenterbind)
startbutton.bind("<Leave>", startleavebind)
pingbutton.bind("<Enter>", startenterbind)
pingbutton.bind("<Leave>", startleavebind)
root.mainloop()
You could go a bit further by writing a single function that simply toggled the state of the button whenever it was called. One way that could be accomplished is by making the new relief type depend on what it currently is which can be determined by calling the universal widget cget() method:
def enterleavebind(event):
new_relief = 'sunken' if event.widget.cget('relief') == 'raised' else 'raised'
event.widget.config(relief=new_relief)
startbutton.bind("<Enter>", enterleavebind)
startbutton.bind("<Leave>", enterleavebind)
pingbutton.bind("<Enter>", enterleavebind)
pingbutton.bind("<Leave>", enterleavebind)

How to dynamically control multiple Listboxes simultaneously in tkinter?

So, I have 5 listboxes in which I need to control at the same time, almost as if they were one listbox with columns.
I am trying to find a way in which when I select an item from any one of the listboxes and delete them, it will highlight and delete the other items in the corresponding index.
so far I am only able to delete the other indexed items only when I invoke curselection() on Listbox1, but if a user selects an item on listbox2 and calls the same, it'll throw an error because the variable is looking for listbox1.
I can't seem to find any documentation or examples of how to control multiple listboxes simultaneously anywhere.
Is it possible to have a self.listbox[0, 1, 2, 3].curselection() type of thing? or even an if statement that allows me to check if self.listbox1.curselection() == True: and then execute according.
This is the function anyway:
def removeSeq(self, event=None):
index = self.listbox1.curselection()[0]
print(index)
## self.listbox1.selection_set(1)
## selectedItem = self.listbox2.curselection()
## print(selectedItem)
## self.listbox1.delete(selectedItem)
## self.listbox2.delete(selectedItem)
## self.listbox3.delete(selectedItem)
## self.listbox4.delete(selectedItem)
## self.listbox5.delete(selectedItem)
pass
I've commented most of it out for test purposes, any help would be massively appreciated.
In your binding you can use event.widget to know which widget was clicked on. Then it's just a matter of getting the selection from that widget and applying it to the other listboxes.
Here's a simple example. To delete a row, double-click in any listbox:
import tkinter as tk
class MultiListbox(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
for i in range(5):
lb = tk.Listbox(self, height=10, exportselection=False)
lb.pack(side="left", fill="y")
for j in range(10):
lb.insert("end", f"Listbox {i+1} value {j+1}")
lb.bind("<Double-1>", self.removeSeq)
def removeSeq(self, event):
lb = event.widget
curselection = lb.curselection()
index = curselection[0] if curselection else None
for listbox in self.winfo_children():
listbox.delete(index)
root = tk.Tk()
mlb = MultiListbox(root)
mlb.pack(side="top", fill="both", expand=True)
root.mainloop()

Why does my selection callback get called on my second Listbox?

from tkinter import *
from tkinter.ttk import *
root = Tk()
listbox = None
listboxMultiple = None
listboxStr = None
listboxMultipleStr = None
def main():
global root
global listboxStr
global listboxMultipleStr
global listbox
global listboxMultiple
root.protocol("WM_DELETE_WINDOW", exitApplication)
root.title("Title Name")
root.option_add('*tearOff', False) # don't allow tear-off menus
root.geometry('1600x300')
listboxStr = StringVar()
listboxStr.set("ABCD")
listbox = Listbox(root, name="lb1", listvariable=listboxStr, width=120)
listbox.pack(side=LEFT)
listbox.bind("<<ListboxSelect>>", selectListItemCallback)
listboxMultipleStr = StringVar()
listboxMultipleStr.set("")
listboxMultiple = Listbox(root, name="lb2", listvariable=listboxMultipleStr, width=120)
listboxMultiple.pack(side=LEFT)
root.mainloop()
def selectListItemCallback(event):
global listboxMultipleStr
global listbox
global listboxMultiple
print("event.widget is {} and listbox is {} and listboxMultiple is {}\n".format(event.widget, listbox, listboxMultiple))
selection = event.widget.curselection()
listboxMultipleStr.set("")
if selection:
index = selection[0]
data = event.widget.get(index)
newvalue = "{}\n{}".format(data,"SOMETHING")
print("selected \"{}\"\n".format( data ))
print("newvalue is \"{}\"\n".format( newvalue ))
listboxMultiple.insert(END, "{}".format(data))
listboxMultiple.insert(END, "SOMETHING")
#listboxMultipleStr.set( newvalue )
else:
pass
def exitApplication():
global root
root.destroy()
if __name__ == "__main__":
main()
Using python3 on Windows 7. I've setup a callback "selectListItemCallback" for one of my two listbox widgets. And yet, when I click on the text in "lb1" it works as expected, I update "lb2" with the same selected text plus I add another line to "lb2".
The issue is, when I then select the item in "lb2", it still calls the callback and the event.widget is "lb1" and not "lb2".
My intent is to have a list of items in 'lb1' and when I select any of them, then 'lb2' gets filled with info related to the selected 'lb1' item. And, I don't want a selection callback invoked in my 'lb2' widget.
Can you see what I'm doing wrong that could be causing this strange behavior?
Thank you.
I've posted the code; and this does run on my Windows 7 machine using python3. Python 3.8.6 to be exact.
It is because the event fires when the first list box loses the selection when you click on the other list box. The event fires whenever the selection changes, not just when it is set.
If you don't want the first listbox to lose its selection when you click in the second listbox, set exportselection to False for the listboxes. Otherwise, tkinter will only allow one to have a selection at a time.

Tkinter - How to go to the top of a Treeview when pressing down at the bottom, and vice versa?

tree = ttk.Treeview(root, selectmode="browse")
tree.pack()
When a user presses the Down arrow key while they have the last item selected already, I need it to select the top item, and the same for the Up arrow. Thanks!
You can bind() function to key Down (Up) in TreeView which will check if you are in last (first) row and jump to first (last) row. It will have to move selection, move focus, scroll window and block event so TreeView will not use this key to move to next (previous) row.
import tkinter as tk
from tkinter import ttk
def jump_to_first(event):
last = tree.get_children()[-1]
if tree.focus() == last:
first = tree.get_children()[0]
tree.selection_set(first) # move selection
tree.focus(first) # move focus
tree.see(first) # scroll to show it
return "break" # don't send event to TreeView
def jump_to_last(event):
first = tree.get_children()[0]
if tree.focus() == first:
last = tree.get_children()[-1]
tree.selection_set(last) # move selection
tree.focus(last) # move focus
tree.see(last) # scroll to show it
return "break" # don't send event to TreeView
root = tk.Tk()
tree = ttk.Treeview(root, selectmode="browse")
tree.pack()
for x in range(1, 21):
print(tree.insert('', 'end', text=str(x)))
tree.bind('<Down>', jump_to_first)
tree.bind('<Up>', jump_to_last)
root.mainloop()

ComboBox clears unrelated ListBox tkinter python

I have a really simple GUI program written in python with tkinter:
from Tkinter import *
from ttk import Combobox
class TestFrame(Frame):
def __init__(self, root, vehicles):
dropdownVals = ['test', 'reallylongstring', 'etc']
listVals = ['yeaaaah', 'mhm mhm', 'untz untz untz', 'test']
self.dropdown = Combobox(root, values=dropdownVals)
self.dropdown.pack()
listboxPanel = Frame(root)
self.listbox = Listbox(listboxPanel, selectmode=MULTIPLE)
self.listbox.grid(row=0, column=0)
for item in listVals:
self.listbox.insert(END, item) # Add params to list
self.scrollbar = Scrollbar(listboxPanel, orient=VERTICAL)
self.listbox.config(yscrollcommand=self.scrollbar.set) # Connect list to scrollbar
self.scrollbar.config(command=self.listbox.yview) # Connect scrollbar to list
self.scrollbar.grid(row=0, column=1, sticky=N+S)
listboxPanel.pack()
self.b = Button(root, text='Show selected list values', command=self.print_scroll_selected)
self.b.pack()
root.update()
def print_scroll_selected(self):
listboxSel = map(int, self.listbox.curselection()) # Get selections in listbox
print '=========='
for sel in listboxSel:
print self.listbox.get(sel)
print '=========='
print ''
# Create GUI
root = Tk()
win = TestFrame(root, None)
root.mainloop();
The GUI looks like this:
I click in a few items in the ListBox and hit the button.
The output I get is as expected:
==========
untz untz untz
==========
==========
yeaaaah
untz untz untz
==========
I now choose a value from the ComboBox and suddenly the selection in the ListBox is gone. Hitting the button shows that no value is selected in the ListBox:
==========
==========
My question is: why does the selection of a ComboBox item clear the selection of the ListBox? They are in no way related so this bug really puzzles me!
I finally found the answer. I will leave this question here in case someone else finds it.
The problem was not in the ComboBox but in the ListBox. This becomes clear if I use two (unrelated) ListBoxes. With two ListBoxes the selection will clear when the focus is changed.
From this question and its accepted answer I found that adding exportselection=0 to a ListBox disables the X selection mechanism where the selection is exported.
From effbot listbox about X selection mechanism: selects something in one listbox, and then selects something in another, the original selection disappears.

Categories

Resources