Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The existence of god implies a violation of causality.


devel / comp.lang.python / Re: Tkinter ttk Treeview binding responds to past events!

SubjectAuthor
o Re: Tkinter ttk Treeview binding responds to past events!John O'Hagan

1
Re: Tkinter ttk Treeview binding responds to past events!

<mailman.287.1694527444.23016.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23973&group=comp.lang.python#23973

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: resea...@johnohagan.com (John O'Hagan)
Newsgroups: comp.lang.python
Subject: Re: Tkinter ttk Treeview binding responds to past events!
Date: Tue, 12 Sep 2023 15:43:11 +1000
Lines: 137
Sender: "John O'Hagan" <johnmohagan@gmail.com>
Message-ID: <mailman.287.1694527444.23016.python-list@python.org>
References: <ffeca85a797b7a25f2a37ce7910b179c627f82c7.camel@johnohagan.com>
<084a1cfa-8889-3acd-d19c-8f204c314b7b@googlemail.com>
<b9d34cdf5dc70ddf8f3a3fdc5df1ea1ee5e6a5a4.camel@johnohagan.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de +CoVh/cA+iuH+vx9jv4swQzwjZuO7PhJXFNebF1LcxkA==
Cancel-Lock: sha1:JcLoye3NEUdSW1FFQRDA9SL0+Vg= sha256:fDKS40CkdGEyAaWe6EZ6J2ZiBciV3D1bCkR3eiccP58=
Return-Path: <johnmohagan@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=kyVCqW3t;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: UNSURE 0.313
X-Spam-Level: ***
X-Spam-Evidence: '*H*': 0.38; '*S*': 0.01; 'def': 0.04; 'queue': 0.07;
'tkinter': 0.07; 'binding': 0.09; 'delay': 0.09; 'prints': 0.09;
'schrieb': 0.09; 'skip:6 20': 0.09; 'import': 0.15; '+0200,':
0.16; 'relatively': 0.16; 'remember,': 0.16; 'solved': 0.16;
'triggered': 0.16; 'wrote:': 0.16; 'problem': 0.16; 'calls': 0.19;
'to:addr:python-list': 0.20; 'issue': 0.21; "i've": 0.22; 'i.e.':
0.22; 'skip:_ 10': 0.22; 'code': 0.23; 'installed': 0.23;
'actual': 0.25; 'anyone': 0.25; 'seems': 0.26; 'tried': 0.26;
'function': 0.27; 'header:User-Agent:1': 0.30; 'seem': 0.31;
'happening': 0.32; 'python-list': 0.32; 'words,': 0.32;
'received:192.168.1': 0.32; 'but': 0.32; "i'm": 0.33; '0);': 0.33;
'rgb(0,': 0.33; "didn't": 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'auto;': 0.36; 'mon,': 0.36;
'processes': 0.36; 'using': 0.37; 'class': 0.37; 'this.': 0.37;
'received:192.168': 0.37; 'way': 0.38; 'thanks': 0.38; 'quite':
0.39; 'list': 0.39; 'still': 0.40; 'processed': 0.40; 'event':
0.40; 'font-family:': 0.60; 'here.': 0.61; 'widows:': 0.62;
'none;': 0.62; 'font-weight:': 0.62; '0px;': 0.63; 'events': 0.63;
'button': 0.64; 'clear': 0.64; 'orphans:': 0.64; 'skip:t 40':
0.64; 'times.': 0.64; 'your': 0.64; 'text-indent:': 0.64; 'upon':
0.64; 'similar': 0.65; 'normal;': 0.66; 'skip:t 20': 0.66;
'worked': 0.67; 'prevent': 0.67; 'time,': 0.67; 'that,': 0.67;
'skip:# 10': 0.68; 'cast': 0.69; 'margin-left:': 0.69;
'temporarily': 0.69; 'below': 0.69; 'instead,': 0.70; 'subject:!':
0.76; 'cheers': 0.76; 'combination': 0.76; 'subsequent': 0.76;
'bind': 0.84; 'callback': 0.84; 'calls.': 0.84; 'executing': 0.84;
'rob': 0.84; 'selects': 0.84; 'sender:addr:gmail.com': 0.84;
'surprised': 0.84; '\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.84;
'400;': 0.89; 'expensive': 0.91; 'none;">': 0.91; 'replacing':
0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20221208; t=1694497425; x=1695102225; darn=python.org;
h=mime-version:user-agent:references:in-reply-to:date:to:from:subject
:message-id:sender:from:to:cc:subject:date:message-id:reply-to;
bh=3ohd86wqUCrAogE8wGOqWIGMLuGEYlbD0bo/gyTiNe4=;
b=kyVCqW3tLBtEqB8mneMIJ+Vww6ipezOHq5MXyYDpb2OALaQuRFNpgNztF3gMuUlPSg
ngoG++vZCVCs+Ws2LprAtxVigW9ucI5W3lhPLHZ9mUthHyTxWUbg5ZS/0WbHOIUnOW06
A0H4jPxjMzWFj1xKHY191OZpqTv0S/qefQAIMyUteACJvD1iDWvuLbSKQCrXe+CoqdjJ
4Cn8x4Kpm6qedjhYMYrqwiZUCeBhwut4TmgYj743HxqtL97u7PT10P7UeU2KiYviVqnQ
xwKmReeA9k48YXyp9zfiujaJSwKbH1/AXBBbaGTg47qL6SMJNU7Tyjwkrfi+f74e3oty
7uiQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1694497425; x=1695102225;
h=mime-version:user-agent:references:in-reply-to:date:to:from:subject
:message-id:sender:x-gm-message-state:from:to:cc:subject:date
:message-id:reply-to;
bh=3ohd86wqUCrAogE8wGOqWIGMLuGEYlbD0bo/gyTiNe4=;
b=mH+zMlLaVmMxIInuSChq9lq0EbAXlnKWkrySr/gMv8ZxiL9gxmpiVLUOZosDTRBIqk
Weh+6pDRUfJ0QccMHb4DQSgjMeJqkYfzXOF7zTSOUuh8EmwuWlMJXzelchjl31Cjoc23
yT8hZhgPYIUdtBB3Bftrzh3Gwz3Ucj88blxq2Pi2Icj8WgrmoDsRJGLquYDDXijUuGf+
+QjKAQBln7gPwKV9+Jv/D32JWPGc74RG+R0dTmmvM6xuawvhWcghc5HtjuX5NSSfTTSC
IgbdtdIDQXtdzVfzjAsCKVuWpWCWIDD9JbH5TFDQBnIMiesxZHRAiI1ypR3KNPFKcUYN
jtfw==
X-Gm-Message-State: AOJu0YxYMZZywg+t+pag8DvxnxV+tPy75K2wgdDp96SukNpU28Io6a63
SlkZvwCfM+kI6pPk708vNGp6F/kGErs=
X-Google-Smtp-Source: AGHT+IH58/BXpPA8BMQ7/wQyfOkDR6djBxZn4HhYMUuP+Hicsn2EfrjlUcs65/JhiNaOOpbhDqoxBA==
X-Received: by 2002:a05:6a00:f9a:b0:68f:cf6f:e225 with SMTP id
ct26-20020a056a000f9a00b0068fcf6fe225mr3129410pfb.20.1694497425174;
Mon, 11 Sep 2023 22:43:45 -0700 (PDT)
In-Reply-To: <084a1cfa-8889-3acd-d19c-8f204c314b7b@googlemail.com>
User-Agent: Evolution 3.49.3-1
X-Mailman-Approved-At: Tue, 12 Sep 2023 10:04:03 -0400
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <b9d34cdf5dc70ddf8f3a3fdc5df1ea1ee5e6a5a4.camel@johnohagan.com>
X-Mailman-Original-References: <ffeca85a797b7a25f2a37ce7910b179c627f82c7.camel@johnohagan.com>
<084a1cfa-8889-3acd-d19c-8f204c314b7b@googlemail.com>
 by: John O'Hagan - Tue, 12 Sep 2023 05:43 UTC

On Mon, 2023-09-11 at 22:25 +0200, Mirko via Python-list wrote:
> Am 11.09.23 um 14:30 schrieb John O'Hagan via Python-list:
> > I was surprised that the code below prints 'called' three times.
> >
> >
> > from tkinter import *
> > from tkinter.ttk import *
> >
> > root=Tk()
> >
> > def callback(*e):
> >      print('called')
> >
> > tree = Treeview(root)
> > tree.pack()
> >
> > iid = tree.insert('', 0, text='test')
> >
> > tree.selection_set(iid)
> > tree.selection_remove(iid)
> > tree.selection_set(iid)
> >
> > tree.bind('<<TreeviewSelect>>', callback)
> >
> > mainloop()
> >
> > In other words, selection events that occurred _before_ the
> > callback
> > function was bound to the Treeview selections are triggering the
> > function upon binding. AFAIK, no other tk widget/binding
> > combination
> > behaves this way (although I haven't tried all of them).
> >
> > This was a problem because I wanted to reset the contents of the
> > Treeview without triggering a relatively expensive bound function,
> > but
> > found that temporarily unbinding didn't prevent the calls.
> >
> > I've worked around this by using a regular button-click binding for
> > selection instead, but I'm curious if anyone can cast any light on
> > this.
> >
> > Cheers
> >
> > John
>
>
> AFAIK (it's been quite some time, since I used Tk/Tkinter):
>
> These selection events are not triggered upon binding, but after the
> mainloop has startet. Tk's eventloop is queue-driven, so the
> tree.selection_{set,remove}() calls just place the events on the
> queue. After that, you setup a callback and when the mainloop
> starts, it processes the events from the queue, executing the
> registered callback.
>
> I seem to remember, that I solved a similar issue by deferring the
> callback installation using root.after().
>
>
> from tkinter import *
> from tkinter.ttk import *
>
> root=Tk()
>
> def callback(*e):
>      print('called')
>
> tree = Treeview(root)
> tree.pack()
>
> iid = tree.insert('', 0, text='test')
>
> tree.selection_set(iid)
> tree.selection_remove(iid)
> tree.selection_set(iid)
>
> root.after(100, lambda: tree.bind('<<TreeviewSelect>>', callback))
>
> mainloop()
>
>
>
> This does not print "called" at all after startup (but still selects
> the entry), because the callback has not been installed when the
> mainloop starts. But any subsequent interaction with the list
> (clicking) will print it (since the callback is then setup).
>
> HTH

Thanks for your reply. However, please see the example below, which is
more like my actual use-case. The selection events take place when a
button is pressed, after the mainloop has started but before the
binding. This also prints 'called' three times. 

from tkinter import *
from tkinter.ttk import *

class Test:

def __init__(self):
root=Tk()
self.tree = Treeview(root)
self.tree.pack()
self.iid = self.tree.insert('', 0, text='test')
Button(root, command=self.temp_unbind).pack()
mainloop()

def callback(self, *e):
print('called')

def temp_unbind(self):
self.tree.unbind('<<TreeviewSelect>>')
self.tree.selection_set(self.iid)
self.tree.selection_remove(self.iid)
self.tree.selection_set(self.iid)
self.tree.bind('<<TreeviewSelect>>', self.callback)
#self.tree.after(0, lambda: self.tree.bind('<<TreeviewSelect>>',
self.callback))

c=Test()

It seems the events are still queued, and then processed by a later
bind?

However, your solution still works, i.e. replacing the bind call with
the commented line. This works even with a delay of 0, as suggested in
Rob Cliffe's reply. Does the call to after clear the event queue
somehow?

My issue is solved, but I'm still curious about what is happening here.

Regards

John

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor