Tkinter ttk Treeview binding responds to past events!
Mirko
mirkok.lists at googlemail.com
Tue Sep 12 14:51:24 EDT 2023
Am 12.09.23 um 07:43 schrieb John O'Hagan via Python-list:
> My issue is solved, but I'm still curious about what is happening here.
MRAB already said it: When you enter the callback function, Tk's
mainloop waits for it to return. So what's happening is:
1. Tk's mainloop pauses
2. temp_unbind() is called
3. TreeviewSelect is unbound
4. events are queued
5. TreeviewSelect is bound again
6. temp_unbind() returns
7. Tk's mainloop continues with the state:
- TreeviewSelect is bound
- events are queued
Am 11.09.23 um 23:58 schrieb Rob Cliffe:
> Indeed. And you don't need to specify a delay of 100 milliseconds. 0 will work (I'm guessing that's because queued actions are performed in the order that they were queued).
Ah, nice, didn't know that!
> I have also found that after() is a cure for some ills, though I
> avoid using it more than I have to because it feels ... a bit
> fragile, perhaps.
Yeah. Though for me it was the delay which made it seem fragile.
With a 0 delay, this looks much more reliable.
FWIW, here's a version without after(), solving this purely on the
python side, not by temporarily unbinding the event, but by
selectively doing nothing in the callback function.
from tkinter import *
from tkinter.ttk import *
class Test:
def __init__(self):
self.inhibit = False
root=Tk()
self.tree = Treeview(root)
self.tree.pack()
self.iid = self.tree.insert('', 0, text='test')
Button(root, command=self.temp_inhibit).pack()
mainloop()
def callback(self, *e):
if not self.inhibit:
print('called')
def temp_inhibit(self):
self.inhibit = True
self.tree.selection_set(self.iid)
self.tree.selection_remove(self.iid)
self.tree.selection_set(self.iid)
self.inhibit = False
self.callback()
c=Test()
HTH and regards
More information about the Python-list
mailing list