Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The less time planning, the more time programming.


devel / comp.lang.python / Re: Tkinter and cv2: "not responding" popup when imshow launched from tk app

SubjectAuthor
* Tkinter and cv2: "not responding" popup when imshow launched fromJohn O'Hagan
`* Re: Tkinter and cv2: "not responding" popup when imshow launched fromaapost
 `* Re: Tkinter and cv2: "not responding" popup when imshow launchedJohn O'Hagan
  +- Re: Tkinter and cv2: "not responding" popup when imshow launched fromaapost
  +- Re: Tkinter and cv2: "not responding" popup when imshow launched fromaapost
  `* Re: Tkinter and cv2: "not responding" popup when imshow launched fromaapost
   `- Re: Tkinter and cv2: "not responding" popup when imshow launchedJohn O'Hagan

1
Tkinter and cv2: "not responding" popup when imshow launched from tk app

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

  copy mid

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

  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: Tkinter and cv2: "not responding" popup when imshow launched from
tk app
Date: Tue, 14 Mar 2023 21:54:09 +1100
Lines: 67
Sender: "John O'Hagan" <johnmohagan@gmail.com>
Message-ID: <mailman.2305.1678791276.20444.python-list@python.org>
References: <fbb73dcc4a7bc45c57ce959e09cde2986aacb0b6.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 kzytHjng7gikcnzmAXz/rQirhETU/3dCNLViEC6ljrvw==
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=hl4Gvmx4;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.054
X-Spam-Evidence: '*H*': 0.89; '*S*': 0.00; '(which': 0.04; 'def':
0.04; '(e.g.': 0.05; 'error:': 0.05; 'arrays': 0.07;
'subject:when': 0.07; 'tkinter': 0.07; 'knowledge.': 0.09;
'line:': 0.09; 'numpy': 0.09; 'subject:not': 0.09; 'threads':
0.09; 'import': 0.15; 'closes': 0.16; 'demonstrates': 0.16;
'displayed': 0.16; 'displays': 0.16; 'paths': 0.16; 'slow': 0.16;
'stuck': 0.16; 'subject:: "': 0.16; 'subject:app': 0.16;
'zooming': 0.16; 'problem': 0.16; 'to:addr:python-list': 0.20;
'option': 0.20; "i've": 0.22; 'code': 0.23; 'saying': 0.25;
'available,': 0.26; 'suspect': 0.26; 'suggestions': 0.28; 'header
:User-Agent:1': 0.30; 'think': 0.32; 'elements': 0.32; 'window':
0.32; 'received:192.168.1': 0.32; 'but': 0.32; "i'm": 0.33;
'particular': 0.33; '0);': 0.33; 'rgb(0,': 0.33;
'received:google.com': 0.34; 'trying': 0.35; 'understood': 0.35;
'close': 0.35; 'auto;': 0.36; 'display': 0.36; 'subject:from':
0.37; "it's": 0.37; 'received:209.85': 0.37; 'received:192.168':
0.37; 'thanks': 0.38; 'received:209': 0.39; 'changes': 0.39;
'necessary': 0.39; 'list': 0.39; 'use': 0.39; 'methods': 0.39;
'still': 0.40; 'appears': 0.40; 'seconds': 0.40; 'both': 0.40;
'something': 0.40; 'event': 0.40; 'font-family:': 0.60; 'widows:':
0.62; 'none;': 0.62; 'here': 0.62; 'font-weight:': 0.62;
'subject': 0.63; '0px;': 0.63; 'between': 0.63; 'button': 0.64;
'skip:k 10': 0.64; 'orphans:': 0.64; 'text-indent:': 0.64;
'similar': 0.65; 'normal;': 0.66; 'back': 0.67; 'forcing': 0.69;
'within': 0.69; 'waiting': 0.73; 'features': 0.75; 'clicking':
0.76; 'des': 0.78; 'click': 0.83; 'happens': 0.84; 'billion':
0.84; 'cycle': 0.84; 'received:192.168.1.109': 0.84;
'sender:addr:gmail.com': 0.84; 'subject: \n ': 0.84; '400;': 0.89;
'none;">': 0.91; 'skip:q 30': 0.91; 'clicks': 0.93; 'skip:q 20':
0.95
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20210112; t=1678791273;
h=mime-version:user-agent:date:to:from:subject:message-id:sender:from
:to:cc:subject:date:message-id:reply-to;
bh=VIHqZi3xg9bsVO/sZWpZylomizF0taPUk4x6hWSrTN0=;
b=hl4Gvmx4KTXA6E+tP3lItpTYJaFTd7kZ613iT9etRm8Yj5T7LpzGag09d0CyqMEqpj
VNAAgWDlEXdmVIDFiqzeE4/6rciLiLPMlIKwKZo6wLB7+Lmaqh17HrRPJN3SE1VnuEoO
u1m7ZfUsL6UX947cyfHrocyG5hrXnJV00heGb2bX4xQUYrjnKgn5a5+WNYGn0s4KHTnJ
KyBRA+H6zT2N4hTXsaYivOuz7LRBqidMJkOGflX0Ge72SVsmKVqSPA8X59V1dtBb7Vnh
fjGktRfcppqSC2s8PDDhbdSfR7SXrNeuHVNBbyddHBOZswpMVRIlqqecZGh0B2cGlXgB
hLrg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112; t=1678791273;
h=mime-version:user-agent:date:to:from:subject:message-id:sender
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=VIHqZi3xg9bsVO/sZWpZylomizF0taPUk4x6hWSrTN0=;
b=LurOEn4MYpaTcxhYEdA9AqVRA2lpxXqSlGlFP8VLM8H4GtTsJiACTQluuvlqFqr7Cu
/8Fg3RWwZkPm57v7tXpa5siYlXqHlV0HXihrdwM3pf77x7y+pakxDd3L+PdpWTEWsBYT
d4mLQd6iU085KymZhr5l7YwaUmyE4OiveZp/76t4VK3uQW+WUWH4PzNTdQtfNsAf5N4q
92SYKeunKQ2V6COxBLP/9zxmWMjVwfVRpzhLXVm0evj3zmyC+L0lxmuEM/pOQChHR/m/
tdqQHxeB0WI9S8lDhu5uwqy9eWZDqnclXXROdx5QqvR+c3E4ljkm2SDVkF/NWrSh2g01
vN7A==
X-Gm-Message-State: AO0yUKWpEHJJooBfQW94ADjsdpBuNA1AZpQSBQy5zpwj3HbcNw5X8kit
wfXcvdAY/0H3ObYK0mrOb5cggqrBcKM=
X-Google-Smtp-Source: AK7set8UfTu/bo9+q7IyGl0zPxLItPX8twfhiz4KtC1+kZ3yxmlBe2iXnLpOOypJXpuvK9SqEub+Tg==
X-Received: by 2002:a05:6a20:6982:b0:cb:c276:58d6 with SMTP id
t2-20020a056a20698200b000cbc27658d6mr63732759pzk.34.1678791273040;
Tue, 14 Mar 2023 03:54:33 -0700 (PDT)
User-Agent: Evolution 3.46.4-1
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: <fbb73dcc4a7bc45c57ce959e09cde2986aacb0b6.camel@johnohagan.com>
 by: John O'Hagan - Tue, 14 Mar 2023 10:54 UTC

Hi list

I'm trying to use cv2 to display images created as numpy arrays, from
within a tkinter app (which does other things with the arrays before
they are displayed as images). The arrays are colour-coded
visualisations of genomes and can be over a billion elements in size,
and I've found the PIL methods to display images in tkinter are too
slow and memory-heavy.

Here is minimal code that demonstrates the problem in the subject line:

import cv2
from tkinter import *

images=['a.jpg', 'b.jpg', 'c.jpg'] #change to image paths

cv2.namedWindow('W', cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty('W', cv2.WND_PROP_FULLSCREEN, 
cv2.WINDOW_FULLSCREEN)
counter=[0]
def show():
cv2.imshow('W', cv2.imread(images[counter[0] % len(images)]))
cv2.waitKey(1)
counter[0] += 1

root=Tk()
root.wm_attributes("-topmost", 1)
Button(root, text=' Show ', command=show).pack()
mainloop()

It works up to a point - I can cycle through the images by clicking the
button - but if I mouse-click on the displayed image (e.g. to use the
zooming and panning features of cv2), nothing happens, and a few
seconds later the image greys out and a popup appears saying "'Unknown'
is not responding" and giving the option of waiting or forcing close
(but sometimes these options are greyed out too). Clicking "wait", if
available, closes the popup but it comes back a few seconds later. If I
then click on the tkinter window titlebar, the popup changes to "'Tk'
is not responding". Clicking on the button still works and after a few
clicks the popup closes.

This happens under both x11 and wayland, but under wayland, I  also get
this error:

"QSocketNotifier: Can only be used with threads started with QThread
qt.qpa.wayland: Wayland does not support QWindow::requestActivate()"

and only every second button press displays a new image ,with only
every second image displayed.

I think this particular popup is a Gnome thing, but AIUI most DEs have
something similar to detect stuck apps. But the app is not stuck.

I suspect this is some kind of interaction between the call to
cv2.waitKey (which is necessary but I've never understood why!) and the
tkinter event loop, but it's beyond my knowledge.

Any suggestions about causes or workarounds?

Thanks

--

John

Re: Tkinter and cv2: "not responding" popup when imshow launched from tk app

<tuql39$nec$1@nnrp.usenet.blueworldhosting.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!nnrp.usenet.blueworldhosting.com!.POSTED!not-for-mail
From: aap...@idontexist.club (aapost)
Newsgroups: comp.lang.python
Subject: Re: Tkinter and cv2: "not responding" popup when imshow launched from
tk app
Date: Tue, 14 Mar 2023 16:22:55 -0400
Organization: BlueWorld Hosting Usenet (https://usenet.blueworldhosting.com)
Message-ID: <tuql39$nec$1@nnrp.usenet.blueworldhosting.com>
References: <fbb73dcc4a7bc45c57ce959e09cde2986aacb0b6.camel@johnohagan.com>
<mailman.2305.1678791276.20444.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 14 Mar 2023 20:23:05 -0000 (UTC)
Injection-Info: nnrp.usenet.blueworldhosting.com;
logging-data="24012"; mail-complaints-to="usenet@blueworldhosting.com"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.6.0
Cancel-Lock: sha1:0zRKChAPCaT4c9kzGNXewXFrOHw= sha256:XX3gyeCKy58ishM6GdAKGAwo6BTS36wIOYpgie16gdg=
sha1:515H1xJLp42JbonabeSQ0NUbgUU= sha256:62TRf1SZIfXFnhm1N+LzaHPfneWRDe8f0l0XljD9zKI=
Content-Language: en-US
In-Reply-To: <mailman.2305.1678791276.20444.python-list@python.org>
 by: aapost - Tue, 14 Mar 2023 20:22 UTC

On 3/14/23 06:54, John O'Hagan wrote:
> Hi list
>
> I'm trying to use cv2 to display images created as numpy arrays, from
> within a tkinter app (which does other things with the arrays before
> they are displayed as images). The arrays are colour-coded
> visualisations of genomes and can be over a billion elements in size,
> and I've found the PIL methods to display images in tkinter are too
> slow and memory-heavy.
>
> Here is minimal code that demonstrates the problem in the subject line:
>
> import cv2
> from tkinter import *
>
> images=['a.jpg', 'b.jpg', 'c.jpg'] #change to image paths
>
> cv2.namedWindow('W', cv2.WND_PROP_FULLSCREEN)
> cv2.setWindowProperty('W', cv2.WND_PROP_FULLSCREEN,
> cv2.WINDOW_FULLSCREEN)
> counter=[0]
> def show():
> cv2.imshow('W', cv2.imread(images[counter[0] % len(images)]))
> cv2.waitKey(1)
> counter[0] += 1
>
> root=Tk()
> root.wm_attributes("-topmost", 1)
> Button(root, text=' Show ', command=show).pack()
> mainloop()
>
> It works up to a point - I can cycle through the images by clicking the
> button - but if I mouse-click on the displayed image (e.g. to use the
> zooming and panning features of cv2), nothing happens, and a few
> seconds later the image greys out and a popup appears saying "'Unknown'
> is not responding" and giving the option of waiting or forcing close
> (but sometimes these options are greyed out too). Clicking "wait", if
> available, closes the popup but it comes back a few seconds later. If I
> then click on the tkinter window titlebar, the popup changes to "'Tk'
> is not responding". Clicking on the button still works and after a few
> clicks the popup closes.
>
> This happens under both x11 and wayland, but under wayland, I  also get
> this error:
>
> "QSocketNotifier: Can only be used with threads started with QThread
> qt.qpa.wayland: Wayland does not support QWindow::requestActivate()"
>
> and only every second button press displays a new image ,with only
> every second image displayed.
>
> I think this particular popup is a Gnome thing, but AIUI most DEs have
> something similar to detect stuck apps. But the app is not stuck.
>
> I suspect this is some kind of interaction between the call to
> cv2.waitKey (which is necessary but I've never understood why!) and the
> tkinter event loop, but it's beyond my knowledge.
>
> Any suggestions about causes or workarounds?
>
> Thanks
>
> --
>
> John
>
>

I don't get any of the zoom/panning behavior with waitKey(1). But a
couple notes from the web:

https://docs.opencv.org/2.4/modules/highgui/doc/user_interface.html

Note

This function should be followed by waitKey function which displays the
image for specified milliseconds. Otherwise, it won’t display the image.
For example, waitKey(0) will display the window infinitely until any
keypress (it is suitable for image display). waitKey(25) will display a
frame for 25 ms, after which display will be automatically closed. (If
you put it in a loop to read videos, it will display the video
frame-by-frame)

https://pythonexamples.org/python-opencv-imshow/

cv2.waitKey(0) is important for holding the execution of the python
program at this statement, so that the image window stays visible. If
you do not provide this statement, cv2.imshow() executes in fraction of
a second and the program closes all the windows it opened, which makes
it almost impossible to see the image on the window.

if I change waitKey to 0, I get the ability to zoom/pan, but it places
the tk window in a blocked state because it is waiting on cv2 to return.
If I hit the ESC key, it releases the wait and gives control back to the
tk window, allowing me to press show again to continue to the next image.

I can try to change waitKey to a high ms like 10000000 and have zoom/pan
for that amount of time before it gives control back to tk (not a
sensical approach).

The fact that you can still see the image after tk takes back control is
I believe just a matter of design, some examples show
cv2.destroyAllWindows() after waitKey(0) to clean that up, but of course
if you are reusing the same window, that destroys the target.

You can resolve that if you move
cv2.namedWindow('W', cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty('W', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

to inside the show, and destroy it after the wait, and make waitKey 0,
this allows creation/cleanup of that window per image

Hitting ESC when done zooming/panning on each image to get back to tk.

Re: Tkinter and cv2: "not responding" popup when imshow launched from tk app

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

  copy mid

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

  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 and cv2: "not responding" popup when imshow launched
from tk app
Date: Wed, 15 Mar 2023 22:37:30 +1100
Lines: 207
Sender: "John O'Hagan" <johnmohagan@gmail.com>
Message-ID: <mailman.2322.1678880278.20444.python-list@python.org>
References: <fbb73dcc4a7bc45c57ce959e09cde2986aacb0b6.camel@johnohagan.com>
<mailman.2305.1678791276.20444.python-list@python.org>
<tuql39$nec$1@nnrp.usenet.blueworldhosting.com>
<d5c34a93dd8ac7afa95ad93ce1c9e34206aba750.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 fVPUFXKDN/vV5vS4gnIlnAIQaUQZDBYPHs2yh5r3DUdw==
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=jlNCdMSL;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.012
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '(which': 0.04; 'def':
0.04; '(e.g.': 0.05; 'despite': 0.05; 'gui': 0.05; 'app.': 0.07;
'hitting': 0.07; 'image.': 0.07; 'loop': 0.07; 'subject:when':
0.07; 'tkinter': 0.07; 'knowledge.': 0.09; 'line:': 0.09;
'subject:not': 0.09; 'url:modules': 0.09; 'window.': 0.09;
'import': 0.15; '(it': 0.16; '06:54,': 0.16; 'behaviour': 0.16;
'closes': 0.16; 'demonstrates': 0.16; 'displayed': 0.16;
'displays': 0.16; 'enough,': 0.16; 'gui.': 0.16; 'impossible':
0.16; 'key,': 0.16; 'none:': 0.16; 'paths': 0.16; 'presumably':
0.16; 'releases': 0.16; 'stuck': 0.16; 'subject:: "': 0.16;
'subject:app': 0.16; 'target.': 0.16; 'url:doc': 0.16; 'zooming':
0.16; 'wrote:': 0.16; 'problem': 0.16; 'python': 0.16; 'tue,':
0.19; 'to:addr:python-list': 0.20; 'option': 0.20; 'issue': 0.21;
"i've": 0.22; 'integration': 0.22; 'problem,': 0.22; 'code': 0.23;
'examples': 0.25; 'saying': 0.25; 'seems': 0.26; 'available,':
0.26; 'behavior': 0.26; 'notes': 0.26; 'suspect': 0.26;
'creating': 0.27; 'function': 0.27; 'done': 0.28; 'fact': 0.28;
'example,': 0.28; 'suggestions': 0.28; 'header:User-Agent:1':
0.30; 'seem': 0.31; 'takes': 0.31; 'url-ip:188/8': 0.31;
'program': 0.31; 'think': 0.32; "doesn't": 0.32; 'execution':
0.32; 'resolve': 0.32; 'specified': 0.32; 'suitable': 0.32;
'window': 0.32; 'received:192.168.1': 0.32; 'but': 0.32; "i'm":
0.33; 'particular': 0.33; 'windows': 0.34; 'same': 0.34; 'work.':
0.34; 'header:In-Reply-To:1': 0.34; 'received:google.com': 0.34;
'understood': 0.35; 'following': 0.35; 'close': 0.35; 'display':
0.36; 'change': 0.36; 'couple': 0.37; 'subject:from': 0.37;
'really': 0.37; 'using': 0.37; "it's": 0.37; 'received:209.85':
0.37; 'received:192.168': 0.37; 'way': 0.38; 'put': 0.38; 'read':
0.38; 'thanks': 0.38; 'seen': 0.62; 'skip:i 20': 0.62; 'gives':
0.62; 'skip:z 10': 0.62; 'here': 0.62; 'subject': 0.63; 'destroy':
0.63; 'between': 0.63; 'everything': 0.63; 'skip:b 10': 0.63;
'button': 0.64; 'remains': 0.64; 'url:4': 0.64; 'with:': 0.64;
'won\xe2\x80\x99t': 0.64; 'your': 0.64; 'similar': 0.65; 'back':
0.67; 'time,': 0.67; 'per': 0.68; 'video': 0.68; 'matter': 0.68;
'forcing': 0.69; 'within': 0.69; 'desktop': 0.70; 'ability': 0.71;
'waiting': 0.73; 'features': 0.75; 'clicking': 0.76; 'supposed':
0.76; 'viewing': 0.76; 'des': 0.78; 'click': 0.83; 'happens':
0.84; 'practical': 0.84; 'up,': 0.84; 'cycle': 0.84; 'esc': 0.84;
'fraction': 0.84; 'infinitely': 0.84; 'label,': 0.84; 'opened,':
0.84; 'realise': 0.84; 'received:192.168.1.109': 0.84;
'sender:addr:gmail.com': 0.84; 'subject: \n ': 0.84; 'return.':
0.91; 'shame': 0.91; 'stays': 0.91; 'blocked': 0.93; 'clicks':
0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20210112; t=1678880275;
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=AM1iqN8rYIJnNg1DNEDGoLMyBzlVP/r+gkGFObM+v1A=;
b=jlNCdMSLpKFyXCyXSz1KT+3FgqFU6iiMO09sSGiyzjPo9fXiRtJzpgpWZRslik1iUR
Ge39XvpZmvrNH4/7kaWfHJNTpX+MXjeB9r4XnPu6/ciIW0DbfcZAWRUt99KfHTIhJyLf
3jSAItyfBi1kY3PED1q8h7pkgOcNZDbYxHRndOp5Yn4sGtiStArYziD3/09QRERfjKBP
feJOfso5E8vORn7SYv+m4p9V7abzCCJZS0sKhR177O/XzMcF9xf5V2de50RE84sJWugf
nyzkRWWUAXEkrqpWodnVc0jhdxeMYdhJorTC5GC4hs9CZj40ta5sbjLnIu33C2ycpg4/
VgzA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112; t=1678880275;
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=AM1iqN8rYIJnNg1DNEDGoLMyBzlVP/r+gkGFObM+v1A=;
b=rL9FgQETna37OtIERQAHL99rP3se0gWkIRo6gJjDvnGWLp+SUQXRxis5XjjHkD0XkA
J4odt9E7yoEBHZPDj/htUWbAsHYGW3Arc+h9eExY44SVzV3pKpxc3sz9koYLvmbu2iov
YIGddzvCtVoPhHL3v8/rWWumsacZtRyrasAPEsBPgf0NubCh6QYyQgHoD37aMJmntggT
ydzSdhZCafUNSPyKgdS8Ssaxm2PbQP9xEDN1aYBENzq7nA8GhlTbJCzTNYv6cvXOz7hC
hNYZexftvnFNsLFaGv4bYglym+pc4pL4nWNg6wk1pOXUbD28h6f627F6GfVclHQKj8MI
bSgw==
X-Gm-Message-State: AO0yUKXxlHEcFhx8gv8f2RjHwV7PoOQ47Y5c6i0SwdzBdhkq9d22jkz5
zlMYadhdaVIuYdguseh0dT9EcS3/Luo=
X-Google-Smtp-Source: AK7set9jRqzcMulPIJcbpB2DFezX8s0xFQaSbNWJuUgTtNHdRbZ/3C2lj5PxJ0Ol2XzZtHDxNoECqg==
X-Received: by 2002:a17:903:1c3:b0:1a1:7b8d:6719 with SMTP id
e3-20020a17090301c300b001a17b8d6719mr1152705plh.27.1678880275010;
Wed, 15 Mar 2023 04:37:55 -0700 (PDT)
In-Reply-To: <tuql39$nec$1@nnrp.usenet.blueworldhosting.com>
User-Agent: Evolution 3.46.4-1
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: <d5c34a93dd8ac7afa95ad93ce1c9e34206aba750.camel@johnohagan.com>
X-Mailman-Original-References: <fbb73dcc4a7bc45c57ce959e09cde2986aacb0b6.camel@johnohagan.com>
<mailman.2305.1678791276.20444.python-list@python.org>
<tuql39$nec$1@nnrp.usenet.blueworldhosting.com>
 by: John O'Hagan - Wed, 15 Mar 2023 11:37 UTC

On Tue, 2023-03-14 at 16:22 -0400, aapost wrote:
> On 3/14/23 06:54, John O'Hagan wrote:

[...]
> >
> > Here is minimal code that demonstrates the problem in the subject
> > line:
> >
> > import cv2
> > from tkinter import *
> >
> > images=['a.jpg', 'b.jpg', 'c.jpg'] #change to image paths
> >
> > cv2.namedWindow('W', cv2.WND_PROP_FULLSCREEN)
> > cv2.setWindowProperty('W', cv2.WND_PROP_FULLSCREEN,
> > cv2.WINDOW_FULLSCREEN)
> > counter=[0]
> > def show():
> >     cv2.imshow('W', cv2.imread(images[counter[0] % len(images)]))
> >     cv2.waitKey(1)
> >     counter[0] += 1
> >
> > root=Tk()
> > root.wm_attributes("-topmost", 1)
> > Button(root, text=' Show ', command=show).pack()
> > mainloop()
> >
> > It works up to a point - I can cycle through the images by clicking
> > the
> > button - but if I mouse-click on the displayed image (e.g. to use
> > the
> > zooming and panning features of cv2), nothing happens, and a few
> > seconds later the image greys out and a popup appears saying
> > "'Unknown'
> > is not responding" and giving the option of waiting or forcing
> > close
> > (but sometimes these options are greyed out too). Clicking "wait",
> > if
> > available, closes the popup but it comes back a few seconds later.
> > If I
> > then click on the tkinter window titlebar, the popup changes
> > to "'Tk'
> > is not responding". Clicking on the button still works and after a
> > few
> > clicks the popup closes.

[...]

> > I think this particular popup is a Gnome thing, but AIUI most DEs
> > have
> > something similar to detect stuck apps. But the app is not stuck.
> >
> > I suspect this is some kind of interaction between the call to
> > cv2.waitKey (which is necessary but I've never understood why!) and
> > the
> > tkinter event loop, but it's beyond my knowledge.
> >
> > Any suggestions about causes or workarounds?
> >
> > Thanks
> >
> > --
> >
> > John
> >
> >
>
>
> I don't get any of the zoom/panning behavior with waitKey(1). But a
> couple notes from the web:
>
>
> https://docs.opencv.org/2.4/modules/highgui/doc/user_interface.html
>
> Note
>
> This function should be followed by waitKey function which displays
> the
> image for specified milliseconds. Otherwise, it won’t display the
> image.
> For example, waitKey(0) will display the window infinitely until any
> keypress (it is suitable for image display). waitKey(25) will display
> a
> frame for 25 ms, after which display will be automatically closed.
> (If
> you put it in a loop to read videos, it will display the video
> frame-by-frame)
>
> https://pythonexamples.org/python-opencv-imshow/
>
> cv2.waitKey(0) is important for holding the execution of the python
> program at this statement, so that the image window stays visible. If
> you do not provide this statement, cv2.imshow() executes in fraction
> of
> a second and the program closes all the windows it opened, which
> makes
> it almost impossible to see the image on the window.
>
> if I change waitKey to 0, I get the ability to zoom/pan, but it
> places
> the tk window in a blocked state because it is waiting on cv2 to
> return.
> If I hit the ESC key, it releases the wait and gives control back to
> the
> tk window, allowing me to press show again to continue to the next
> image.
>
> I can try to change waitKey to a high ms like 10000000 and have
> zoom/pan
> for that amount of time before it gives control back to tk (not a
> sensical approach).
>
> The fact that you can still see the image after tk takes back control
> is
> I believe just a matter of design, some examples show
> cv2.destroyAllWindows() after waitKey(0) to clean that up, but of
> course
> if you are reusing the same window, that destroys the target.
>
> You can resolve that if you move
> cv2.namedWindow('W', cv2.WND_PROP_FULLSCREEN)
> cv2.setWindowProperty('W', cv2.WND_PROP_FULLSCREEN,
> cv2.WINDOW_FULLSCREEN)
>
> to inside the show, and destroy it after the wait, and make waitKey
> 0,
> this allows creation/cleanup of that window per image
>
> Hitting ESC when done zooming/panning on each image to get back to
> tk.

Thanks for your reply.

I'm afraid in the real app it won't be practical to have no gui control
while viewing images.

But your suggestions have made me realise that my issue has nothing to
do with tkinter, it seems to be the way imshow is supposed to work.
This code gives the same behaviour described above:

cv2.imshow('W', array)
cv2.waitKey(100)
time.sleep(20)

I've seen the docs you mention that say the window will close if no
keypress happens within the waitKey time, but that doesn't seem to be
what happens as you say. After waitKey returns, the image remains and
everything works, but for some reason the desktop complains about an
unresponsive app.

You can use cv2 to display video with:

while 1:
frame = camera.read() #pseudocode
cv2.imshow('W', frame)
cv2.waitKey(1)

(In fact this used to work without waitKey if you called
cv2.startWindowThread() after creating the window, but that stopped
working at some point.)

The fact that this works, presumably because the image is being
replaced often enough, suggested the following workaround to my
problem, using a call to after() to re-display the current image before
the desktop starts complaining:

images=[cv2.imread(i) for i in ('a.jpg', 'b.jpg', 'c.jpg')]

cv2.namedWindow('W', cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty('W', cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)

counter=[0]

image = [None]

def show():
im = images[counter[0] % 3]
cv2.imshow('W', im)
cv2.waitKey(1)
image[:] = [im]
counter[0] += 1

root=Tk()
root.wm_attributes("-topmost", 1)
b=Button(root, text=' Show ', command=show)
b.pack()

def update():
if image[0] is not None:
cv2.imshow('W', image[0])
cv2.waitKey(1)
root.after(1000, update)

update()

mainloop()

Not ideal, but I'm starting to think that despite the "highgui" label,
cv2.imshow is not really suited for integration into a gui. Which is a
shame because it's so damn fast! 

Thanks

John

Re: Tkinter and cv2: "not responding" popup when imshow launched from tk app

<tut5oi$2nv2$1@nnrp.usenet.blueworldhosting.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!nnrp.usenet.blueworldhosting.com!.POSTED!not-for-mail
From: aap...@idontexist.club (aapost)
Newsgroups: comp.lang.python
Subject: Re: Tkinter and cv2: "not responding" popup when imshow launched from
tk app
Date: Wed, 15 Mar 2023 15:19:46 -0400
Organization: BlueWorld Hosting Usenet (https://usenet.blueworldhosting.com)
Message-ID: <tut5oi$2nv2$1@nnrp.usenet.blueworldhosting.com>
References: <fbb73dcc4a7bc45c57ce959e09cde2986aacb0b6.camel@johnohagan.com>
<mailman.2305.1678791276.20444.python-list@python.org>
<tuql39$nec$1@nnrp.usenet.blueworldhosting.com>
<d5c34a93dd8ac7afa95ad93ce1c9e34206aba750.camel@johnohagan.com>
<mailman.2322.1678880278.20444.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 15 Mar 2023 19:19:46 -0000 (UTC)
Injection-Info: nnrp.usenet.blueworldhosting.com;
logging-data="90082"; mail-complaints-to="usenet@blueworldhosting.com"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.6.0
Cancel-Lock: sha1:AZ09gUWHrUBrshDo0wmkiAqfbvE= sha256:pDY7zMISKeIMXr23bLgVNWyFEIDk0AbTznlbg7xHtFg=
sha1:QEOg0LLo3gDvhA6TBb0NO7ZJscc= sha256:SPHevykLfbLOVBilfAmWEfrM9EedMkG3/Fh+RoXEFZg=
Content-Language: en-US
In-Reply-To: <mailman.2322.1678880278.20444.python-list@python.org>
 by: aapost - Wed, 15 Mar 2023 19:19 UTC

On 3/15/23 07:37, John O'Hagan wrote:
> On Tue, 2023-03-14 at 16:22 -0400, aapost wrote:
>> On 3/14/23 06:54, John O'Hagan wrote:
>>> It works up to a point - I can cycle through the images by clicking
>>> the
>>> button - but if I mouse-click on the displayed image (e.g. to use
>>> the
>>> zooming and panning features of cv2), nothing happens, and a few
>>> seconds later the image greys out and a popup appears saying
>>> "'Unknown'
>>> is not responding" and giving the option of waiting or forcing
>>> close
>>> (but sometimes these options are greyed out too). Clicking "wait",
>>> if
>>> available, closes the popup but it comes back a few seconds later.
>>> If I
>>> then click on the tkinter window titlebar, the popup changes
>>> to "'Tk'
>>> is not responding". Clicking on the button still works and after a
>>> few
>>> clicks the popup closes.
>
> [...]
>
>>> I think this particular popup is a Gnome thing, but AIUI most DEs
>>> have
>>> something similar to detect stuck apps. But the app is not stuck.
>>>
>>> I suspect this is some kind of interaction between the call to
>>> cv2.waitKey (which is necessary but I've never understood why!) and
>>> the
>>> tkinter event loop, but it's beyond my knowledge.
>>>
>>> Any suggestions about causes or workarounds?
>>>
>>> Thanks
>>>
>>> --
>>>
>>> John
>>>
>>>
>>
>>
>> I don't get any of the zoom/panning behavior with waitKey(1). But a
>> couple notes from the web:
>>
>>
>> https://docs.opencv.org/2.4/modules/highgui/doc/user_interface.html
>>
>> Note
>>
>> This function should be followed by waitKey function which displays
>> the
>> image for specified milliseconds. Otherwise, it won’t display the
>> image.
>> For example, waitKey(0) will display the window infinitely until any
>> keypress (it is suitable for image display). waitKey(25) will display
>> a
>> frame for 25 ms, after which display will be automatically closed.
>> (If
>> you put it in a loop to read videos, it will display the video
>> frame-by-frame)
>>
>> https://pythonexamples.org/python-opencv-imshow/
>>
>> cv2.waitKey(0) is important for holding the execution of the python
>> program at this statement, so that the image window stays visible. If
>> you do not provide this statement, cv2.imshow() executes in fraction
>> of
>> a second and the program closes all the windows it opened, which
>> makes
>> it almost impossible to see the image on the window.
>>
>> if I change waitKey to 0, I get the ability to zoom/pan, but it
>> places
>> the tk window in a blocked state because it is waiting on cv2 to
>> return.
>> If I hit the ESC key, it releases the wait and gives control back to
>> the
>> tk window, allowing me to press show again to continue to the next
>> image.
>>
>> I can try to change waitKey to a high ms like 10000000 and have
>> zoom/pan
>> for that amount of time before it gives control back to tk (not a
>> sensical approach).
>>
>> The fact that you can still see the image after tk takes back control
>> is
>> I believe just a matter of design, some examples show
>> cv2.destroyAllWindows() after waitKey(0) to clean that up, but of
>> course
>> if you are reusing the same window, that destroys the target.
>>
>> You can resolve that if you move
>> cv2.namedWindow('W', cv2.WND_PROP_FULLSCREEN)
>> cv2.setWindowProperty('W', cv2.WND_PROP_FULLSCREEN,
>> cv2.WINDOW_FULLSCREEN)
>>
>> to inside the show, and destroy it after the wait, and make waitKey
>> 0,
>> this allows creation/cleanup of that window per image
>>
>> Hitting ESC when done zooming/panning on each image to get back to
>> tk.
>
> Thanks for your reply.
>
> I'm afraid in the real app it won't be practical to have no gui control
> while viewing images.
>
> But your suggestions have made me realise that my issue has nothing to
> do with tkinter, it seems to be the way imshow is supposed to work.
> This code gives the same behaviour described above:
>
> cv2.imshow('W', array)
> cv2.waitKey(100)
> time.sleep(20)
>
> I've seen the docs you mention that say the window will close if no
> keypress happens within the waitKey time, but that doesn't seem to be
> what happens as you say. After waitKey returns, the image remains and
> everything works, but for some reason the desktop complains about an
> unresponsive app.
>
> You can use cv2 to display video with:
>
> while 1:
> frame = camera.read() #pseudocode
> cv2.imshow('W', frame)
> cv2.waitKey(1)
>
> (In fact this used to work without waitKey if you called
> cv2.startWindowThread() after creating the window, but that stopped
> working at some point.)
>
> The fact that this works, presumably because the image is being
> replaced often enough, suggested the following workaround to my
> problem, using a call to after() to re-display the current image before
> the desktop starts complaining:
>
> images=[cv2.imread(i) for i in ('a.jpg', 'b.jpg', 'c.jpg')]
>
> cv2.namedWindow('W', cv2.WND_PROP_FULLSCREEN)
> cv2.setWindowProperty('W', cv2.WND_PROP_FULLSCREEN,
> cv2.WINDOW_FULLSCREEN)
>
> counter=[0]
>
> image = [None]
>
> def show():
> im = images[counter[0] % 3]
> cv2.imshow('W', im)
> cv2.waitKey(1)
> image[:] = [im]
> counter[0] += 1
>
> root=Tk()
> root.wm_attributes("-topmost", 1)
> b=Button(root, text=' Show ', command=show)
> b.pack()
>
> def update():
> if image[0] is not None:
> cv2.imshow('W', image[0])
> cv2.waitKey(1)
> root.after(1000, update)
>
> update()
>
> mainloop()
>
>
> Not ideal, but I'm starting to think that despite the "highgui" label,
> cv2.imshow is not really suited for integration into a gui. Which is a
> shame because it's so damn fast!

Yep, it was just a solution for the extent of the snippet. I don't have
any easy fixes for the single thread blocking issue as I have not needed
to investigate any UI blocking situations yet (if anyone has experience
in that they can chime in, or you may need to ask about
blocking/threading solutions/examples in regard to imshow on a cv2
specific forum, some sort of entirely different approach may be needed).

To clarify the docs, it does not say "close if no key presses", it says
2 different things. 2nd part being, Setting the ms will close the window
after the time regardless. The wording of the "displaying the window
indefinitely until any keypress" - when 0, does not say anything about
closing, so the until a keypress is unclear as to what that means, as
any keypress when it is 0 definitely does not close it. They do activate
the pan/zoom, and ability to ESC though..

Also to clarify (just in case), waitKey is in ms, and time.sleep is in
seconds, so waitKey 100 is 0.1, the equivalent to time.sleep(20) would
be waitKey(20000)

The likely reason the desktop is complaining about an unresponsive app
is that even though you still see that image, even at 100 ms (0.1s) the
window you are clicking on by that time is technically already dead and
closed, just not cleaned up.

Additionally at least one of the error messages you were getting were in
regard to qt (one of the graphical options under the hood), my
understanding is that qt has some added controls for imshow that show up
in a toolbar above the image (I don't have them so I don't see them). A
sort of hypothetical guess, but it might be that those features are
getting launched, but when they don't have the live imshow to operate
on, they choke and the OS steps in (could be wrong).

Does the update solve your problem in practice though? That is just a
loop that makes the imshow live again for 1 ms, then pauses for 1
second, then runs recursively. Potentially indefinitely delaying
anything from being flagged as stuck. I assume that doesn't give you the
pan/zoom ability either (at least it doesn't on mine).


Click here to read the complete article
Re: Tkinter and cv2: "not responding" popup when imshow launched from tk app

<tutdcd$1pdp$1@nnrp.usenet.blueworldhosting.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!nnrp.usenet.blueworldhosting.com!.POSTED!not-for-mail
From: aap...@idontexist.club (aapost)
Newsgroups: comp.lang.python
Subject: Re: Tkinter and cv2: "not responding" popup when imshow launched from
tk app
Date: Wed, 15 Mar 2023 17:29:49 -0400
Organization: BlueWorld Hosting Usenet (https://usenet.blueworldhosting.com)
Message-ID: <tutdcd$1pdp$1@nnrp.usenet.blueworldhosting.com>
References: <fbb73dcc4a7bc45c57ce959e09cde2986aacb0b6.camel@johnohagan.com>
<mailman.2305.1678791276.20444.python-list@python.org>
<tuql39$nec$1@nnrp.usenet.blueworldhosting.com>
<d5c34a93dd8ac7afa95ad93ce1c9e34206aba750.camel@johnohagan.com>
<mailman.2322.1678880278.20444.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 15 Mar 2023 21:29:49 -0000 (UTC)
Injection-Info: nnrp.usenet.blueworldhosting.com;
logging-data="58809"; mail-complaints-to="usenet@blueworldhosting.com"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.6.0
Cancel-Lock: sha1:BilSQEThZZHDCjtwSdB68lwb10c= sha256:nnkTcKcenrcHENYAUSaUDXxkZ99xD7V09JiN/inaLTM=
sha1:wSdg1hGEvtKTOm6oEbovviLmwqI= sha256:r3wEPqEXuVNH/Ke23kCiZNea0YlCTx6GUFDgNfr778M=
Content-Language: en-US
In-Reply-To: <mailman.2322.1678880278.20444.python-list@python.org>
 by: aapost - Wed, 15 Mar 2023 21:29 UTC

On 3/15/23 07:37, John O'Hagan wrote:
> On Tue, 2023-03-14 at 16:22 -0400, aapost wrote:
>> On 3/14/23 06:54, John O'Hagan wrote:
>

Doing a quick read, tkinter is not threadsafe, so diving in to a
threading solution is probably not the best approach.

But just to throw out another possible solution to see if you can find a
"good enough" work around that fits your desired behavior.

You could spawn the imshow as it's own program:

file2: sp.py

import sys
import cv2
import tkinter as tk
def show(img, root):
cv2.namedWindow(str(root), cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(str(root), cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)
cv2.imshow(str(root), cv2.imread(img))
cv2.waitKey(0)
cv2.destroyAllWindows()
exit()
root=tk.Tk()
show(sys.argv[1], root)
tk.mainloop()

file1:main.py

import cv2
import tkinter as tk
import subprocess
images=['c.jpg', 'b.jpg', 'c.jpg']
control = {
"counter" : 0,
"pid": None
} def show(control):
if control["pid"]:
control["pid"].kill()
control["pid"] = subprocess.Popen(["python", "sp.py",
images[control["counter"] % len(images)]])
control["counter"] += 1
root=tk.Tk()
root.wm_attributes("-topmost", 1)
tk.Button(root, text=' Show ', command=lambda: show(control)).pack()
tk.mainloop()

refactor/design as needed, you track the pid, kill the existing on each
subsequent show press.

caveats would be that since it is an entirely separate program, if you
close the main window, the other window will still linger until it is
also closed. You could try to catch the close to run a .kill() on the
subprocess if you want right before the exit, etc.

But this gives control back to the main GUI since they are now
independent of each other and not within the same thread.

If they need to talk to each other in some way more than that, I am sure
deeper design solutions could be thought up.

Re: Tkinter and cv2: "not responding" popup when imshow launched from tk app

<tuujia$sie$1@nnrp.usenet.blueworldhosting.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!nnrp.usenet.blueworldhosting.com!.POSTED!not-for-mail
From: aap...@idontexist.club (aapost)
Newsgroups: comp.lang.python
Subject: Re: Tkinter and cv2: "not responding" popup when imshow launched from
tk app
Date: Thu, 16 Mar 2023 04:21:30 -0400
Organization: BlueWorld Hosting Usenet (https://usenet.blueworldhosting.com)
Message-ID: <tuujia$sie$1@nnrp.usenet.blueworldhosting.com>
References: <fbb73dcc4a7bc45c57ce959e09cde2986aacb0b6.camel@johnohagan.com>
<mailman.2305.1678791276.20444.python-list@python.org>
<tuql39$nec$1@nnrp.usenet.blueworldhosting.com>
<d5c34a93dd8ac7afa95ad93ce1c9e34206aba750.camel@johnohagan.com>
<mailman.2322.1678880278.20444.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 16 Mar 2023 08:21:30 -0000 (UTC)
Injection-Info: nnrp.usenet.blueworldhosting.com;
logging-data="29262"; mail-complaints-to="usenet@blueworldhosting.com"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.6.0
Cancel-Lock: sha1:I35IDQ1VLlSlhAOwn9vZ8wDrWI0= sha256:CgIlMCVwPL3VYIO58YoiBkBZCtNZmJAMz23+VW21wkI=
sha1:zyrFE0AlvIKrYyfyEYf1SqG7s+Y= sha256:25Gg2wmZqiQRa7PBIN8HI6B2nH/r++Bw8bmece5MBY4=
Content-Language: en-US
In-Reply-To: <mailman.2322.1678880278.20444.python-list@python.org>
 by: aapost - Thu, 16 Mar 2023 08:21 UTC

On 3/15/23 07:37, John O'Hagan wrote:
> On Tue, 2023-03-14 at 16:22 -0400, aapost wrote:
>> On 3/14/23 06:54, John O'Hagan wrote:
>
> [...]
>>>

Read an alternative description of the waitKey behavior

>For example, waitKey(0) will display the window infinitely until any
keypress (it is suitable for image display). waitKey(25) will display a
frame and wait approximately 25 ms for a key press (suitable for
displaying a video frame-by-frame). To remove the window, use
cv::destroyWindow.

I went back to double check and I stand corrected on the "any keypress"
part. Any keypress on the 'keyboard' does break the wait (as I
incorrectly concluded and assumed only ESC did).

It is still slightly ambiguous in explaining that when using 25ms,
keypress or not, the wait breaks at 25ms (or before that if you press a
keyboard key). For my setup the window is stale at that point, no
controls, just a stale frame behind the tkinter window that needs to be
destroyed or reused.

Whether that is the exact behavior on all set-ups isn't clear, the note
further uses the ambiguous phrasing "might".

>Note: This function should be followed by a call to cv::waitKey or
cv::pollKey to perform GUI housekeeping tasks that are necessary to
actually show the given image and make the window respond to mouse and
keyboard events. Otherwise, it won’t display the image and the window
might lock up.

It seems with the several variations, behavior varies between them, like
one comment saying startWindowThread when using c++ and gtk allows you
to not use waitKey (not the case here.. it seems -- changing my language
to not misspeak, lol).

I haven't come across any examples beyond imshow running stand-alone as
in my solution suggestion 2. But waitKey does return a keyvalue for the
keypess to allow extending the functionality, so you can grab it, do
something, and go right back to waiting, I haven't seen any use of this
though.

You can also leave out reference to tkinter all together when using
startWindowThread:

import sys
import cv2

cv2.startWindowThread()
cv2.namedWindow("W", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("W", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("W", cv2.imread(sys.argv[1]))
while(1):
a = cv2.waitKey(0)
if a == 27:#ESC
break
#elif a == something else, do something
cv2.destroyAllWindows()
exit()

But it still blocks if integrated in to the main tkinter thread, and
appears to use tkinter under the hood. And as tkinter is considered
thread-unsafe, the startWindowThread would only be ok when spawned as a
separate process like the subprocess example.

Anyway, apologies for the mistake on the any key part.

Re: Tkinter and cv2: "not responding" popup when imshow launched from tk app

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!lilly.ping.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: resea...@johnohagan.com (John O'Hagan)
Newsgroups: comp.lang.python
Subject: Re: Tkinter and cv2: "not responding" popup when imshow launched
from tk app
Date: Sat, 18 Mar 2023 13:56:04 +1100
Lines: 143
Sender: "John O'Hagan" <johnmohagan@gmail.com>
Message-ID: <mailman.2355.1679108198.20444.python-list@python.org>
References: <fbb73dcc4a7bc45c57ce959e09cde2986aacb0b6.camel@johnohagan.com>
<mailman.2305.1678791276.20444.python-list@python.org>
<tuql39$nec$1@nnrp.usenet.blueworldhosting.com>
<d5c34a93dd8ac7afa95ad93ce1c9e34206aba750.camel@johnohagan.com>
<mailman.2322.1678880278.20444.python-list@python.org>
<tuujia$sie$1@nnrp.usenet.blueworldhosting.com>
<8dd9e654d6d5a8285adb3e98fdce6f8841f50026.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 bvNakooKMV7RXlVQcDZWmgjcrz0o+FJOwQ67Gvitlsew==
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=UTP2jA+f;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.007
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'looks': 0.02; 'this:':
0.03; 'def': 0.04; 'gui': 0.05; 'thread': 0.05; 'loop': 0.07;
'subject:when': 0.07; 'suggestion': 0.07; 'tkinter': 0.07;
'breaks': 0.09; 'example.': 0.09; 'something,': 0.09;
'subject:not': 0.09; 'though.': 0.09; 'threads': 0.09; 'import':
0.15; '(it': 0.16; '06:54,': 0.16; 'buttons': 0.16; 'c++': 0.16;
'cases,': 0.16; 'corrected': 0.16; 'displayed': 0.16; 'events.':
0.16; 'explaining': 0.16; 'extending': 0.16; 'gtk': 0.16; 'hood.':
0.16; 'ideal.': 0.16; 'part.': 0.16; 'pythonic': 0.16; 'replies,':
0.16; 'reused.': 0.16; 'stale': 0.16; 'subject:: "': 0.16;
'subject:app': 0.16; 'thread.': 0.16; 'zooming': 0.16;
'\xc2\xa0so': 0.16; 'wrote:': 0.16; 'uses': 0.19; 'calls': 0.19;
'thu,': 0.19; 'tue,': 0.19; 'to:addr:python-list': 0.20;
'language': 0.21; 'skip:_ 10': 0.22; 'code': 0.23; "i'd": 0.24;
'idea': 0.24; 'examples': 0.25; 'saying': 0.25; 'seems': 0.26;
'tried': 0.26; 'behavior': 0.26; 'opening': 0.26; 'perform': 0.26;
"isn't": 0.27; 'leave': 0.27; 'function': 0.27; 'example,': 0.28;
'it,': 0.29; 'header:User-Agent:1': 0.30; 'whole': 0.30;
'comment': 0.31; 'putting': 0.31; 'think': 0.32; "doesn't": 0.32;
'(as': 0.32; 'else,': 0.32; 'point,': 0.32; 'suitable': 0.32;
'window': 0.32; 'received:192.168.1': 0.32; 'but': 0.32; "i'll":
0.33; 'there': 0.33; 'focus': 0.33; '0);': 0.33; 'rgb(0,': 0.33;
'same': 0.34; 'header:In-Reply-To:1': 0.34; 'received:google.com':
0.34; 'running': 0.34; 'fine': 0.35; 'auto;': 0.36; 'display':
0.36; 'main': 0.37; 'subject:from': 0.37; 'using': 0.37; "it's":
0.37; 'received:209.85': 0.37; 'received:192.168': 0.37; 'way':
0.38; 'read': 0.38; 'thanks': 0.38; 'received:209': 0.39;
'necessary': 0.39; 'use': 0.39; 'break': 0.39; 'still': 0.40;
'otherwise,': 0.40; 'font-family:': 0.60; 'above': 0.62; 'seen':
0.62; 'skip:\xc2 10': 0.62; 'widows:': 0.62; 'love': 0.62;
'none;': 0.62; 'come': 0.62; 'font-weight:': 0.62; '0px;': 0.63;
'between': 0.63; 'skip:k 10': 0.64; 'key': 0.64; 'orphans:': 0.64;
'lock': 0.64; 'won\xe2\x80\x99t': 0.64; 'your': 0.64; 'text-
indent:': 0.64; 'touch': 0.65; 'normal;': 0.66; 'back': 0.67;
'skip:t 30': 0.67; 'stand': 0.67; 'respond': 0.67; 'video': 0.68;
'right': 0.68; 'further': 0.69; 'above,': 0.70; 'them,': 0.70;
'too.': 0.70; 'waiting': 0.73; '8bit%:100': 0.76; 'subsequent':
0.76; 'led': 0.81; 'controls,': 0.84; 'esc': 0.84; 'infinitely':
0.84; 'mouse': 0.84; 'received:192.168.1.109': 0.84;
'sender:addr:gmail.com': 0.84; 'subject: \n ': 0.84; 'time!':
0.84; 'varies': 0.84; '\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.84;
'behind': 0.88; '400;': 0.89; 'concluded': 0.91; 'none;">': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20210112; t=1679108195;
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=V8zmQ5SLNxwEnnLwK5Yf5z6XcNJmHmO7WnYYW6Yd03k=;
b=UTP2jA+fxGQ1lhGv7Z11aGl/wA3CJFXKg0QU6YpnmNNjH280Fmc6uPhhx4Zncm5cA5
Z8qhFob/GUd6IQkLEMd50ZDA1lGmPcFrUiNHofqLGE9UiICYe+Rck5mME7N4MS+N+dJp
sPUhb72JA4MO8Ef4qKB2bOdGMGQw+SgcjRLyA8x4iis0NdheLSamAUvakYAF/iZF+mML
ovFC2pU9STupEXao5taTm17BicJish8JcwUxjIlBoR1p26xaHrecSKfDf9OgOVmxdVj5
Bbmy8IdxUisZU3GE618bQtyE4lgZ0mGTqHdyy1qvyXkywDSLZaDV4DcV8iGR01sFjivM
KW5g==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112; t=1679108195;
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=V8zmQ5SLNxwEnnLwK5Yf5z6XcNJmHmO7WnYYW6Yd03k=;
b=6un31UrNc9mhhxVHAJbLQDNYhzZMgkPg8uSAf7BV7HCdRiHAaIpNgNyEbp646k58Tq
c3FzyFVRx9q5uOXsl8iC0J5ZKbpaNqZGwPw0kul27epFRm5pnVjnGIcxAWBFduJlTvzC
9xLqSxJuDSctW6TmROzpHLD74HFX1DWG326BLVMK4GuVE5EspXAuDMTKU+wCihSwLvOE
8gec9oxpx0v03J0HG087r7T+8HVjSAU9p/TjgRTAkDVXIYUMaio2cGfVlbjRy+rpvTav
EAqSWvWJ7xkVWLGyEqavmb7NctAUOmIIjHFjZvdmIjYkQgRoCwPZBwPlx16v4hzGrSKs
conA==
X-Gm-Message-State: AO0yUKXVW6ZQfdxzZlFtvQl0mcbejx6B9XpOegA+8xQkgpthrwCJj5iK
I0QD5BE6Nlnj1rP4AEwHwx3okR+zEYU=
X-Google-Smtp-Source: AK7set/1b/2u96X4vXxRsaX/qT+Q7xYPlFFO7Imeal9huju7CkJRJIDaErjHMnK9l5LzBWaD9vD5ig==
X-Received: by 2002:a17:903:42ce:b0:1a1:b748:f344 with SMTP id
jy14-20020a17090342ce00b001a1b748f344mr526860plb.38.1679108195016;
Fri, 17 Mar 2023 19:56:35 -0700 (PDT)
In-Reply-To: <tuujia$sie$1@nnrp.usenet.blueworldhosting.com>
User-Agent: Evolution 3.46.4-1
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: <8dd9e654d6d5a8285adb3e98fdce6f8841f50026.camel@johnohagan.com>
X-Mailman-Original-References: <fbb73dcc4a7bc45c57ce959e09cde2986aacb0b6.camel@johnohagan.com>
<mailman.2305.1678791276.20444.python-list@python.org>
<tuql39$nec$1@nnrp.usenet.blueworldhosting.com>
<d5c34a93dd8ac7afa95ad93ce1c9e34206aba750.camel@johnohagan.com>
<mailman.2322.1678880278.20444.python-list@python.org>
<tuujia$sie$1@nnrp.usenet.blueworldhosting.com>
 by: John O'Hagan - Sat, 18 Mar 2023 02:56 UTC

On Thu, 2023-03-16 at 04:21 -0400, aapost wrote:
> On 3/15/23 07:37, John O'Hagan wrote:
> > On Tue, 2023-03-14 at 16:22 -0400, aapost wrote:
> > > On 3/14/23 06:54, John O'Hagan wrote:
> >
> > [...]
> > > >
>
>
> Read an alternative description of the waitKey behavior
>
>  >For example, waitKey(0) will display the window infinitely until
> any
> keypress (it is suitable for image display). waitKey(25) will display
> a
> frame and wait approximately 25 ms for a key press (suitable for
> displaying a video frame-by-frame). To remove the window, use
> cv::destroyWindow.
>
> I went back to double check and I stand corrected on the "any
> keypress"
> part. Any keypress on the 'keyboard' does break the wait (as I
> incorrectly concluded and assumed only ESC did).
>
> It is still slightly ambiguous in explaining that when using 25ms,
> keypress or not, the wait breaks at 25ms (or before that if you press
> a
> keyboard key). For my setup the window is stale at that point, no
> controls, just a stale frame behind the tkinter window that needs to
> be
> destroyed or reused.
>
> Whether that is the exact behavior on all set-ups isn't clear, the
> note
> further uses the ambiguous phrasing "might".
>
>  >Note: This function should be followed by a call to cv::waitKey or
> cv::pollKey to perform GUI housekeeping tasks that are necessary to
> actually show the given image and make the window respond to mouse
> and
> keyboard events. Otherwise, it won’t display the image and the window
> might lock up.
>
> It seems with the several variations, behavior varies between them,
> like
> one comment saying startWindowThread when using c++ and gtk allows
> you
> to not use waitKey (not the case here.. it seems -- changing my
> language
> to not misspeak, lol).
>
> I haven't come across any examples beyond imshow running stand-alone
> as
> in my solution suggestion 2. But waitKey does return a keyvalue for
> the
> keypess to allow extending the functionality, so you can grab it, do
> something, and go right back to waiting, I haven't seen any use of
> this
> though.
>
> You can also leave out reference to tkinter all together when using
> startWindowThread:
>
> import sys
> import cv2
>
> cv2.startWindowThread()
> cv2.namedWindow("W", cv2.WND_PROP_FULLSCREEN)
> cv2.setWindowProperty("W", cv2.WND_PROP_FULLSCREEN,
> cv2.WINDOW_FULLSCREEN)
> cv2.imshow("W", cv2.imread(sys.argv[1]))
> while(1):
>      a = cv2.waitKey(0)
>      if a == 27:#ESC
>          break
>      #elif a == something else, do something
> cv2.destroyAllWindows()
> exit()
>
> But it still blocks if integrated in to the main tkinter thread, and
> appears to use tkinter under the hood. And as tkinter is considered
> thread-unsafe, the startWindowThread would only be ok when spawned as
> a
> separate process like the subprocess example.
>

Thanks for this and your other detailed replies, which I won't go into
because your comment above led me to a simple workaround using
threading, with a caveat. 

This may be controversial, but AIUI it's fine to use threads in tkinter
as long as you don't touch the same widget from different threads. I do
it all the time!  So I tried opening and waiting for the window in a
thread like this:

def window_thread():
cv2.namedWindow('W', cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty('W', cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)
cv2.waitKey(0)

threading.Thread(target=window_thread, daemon=True).start()

This works - the images can be displayed by calling imshow() via
tkinter buttons in the main thread, and all the cv2 panning and zooming
works too. 

But the fly in the ointment is that any keypress while the focus is on
the cv2 window exits the thread. I think this would apply to your
subprocess example too.

I tried:

- putting the waitKey call in a while loop as in your example above,
hoping it would just keep waiting, but the second call to waitKey
doesn't respond to keypresses
- putting the whole window_thread code in a while loop with a call to
destroyAllWindows, hoping that this would re-create the window, but it
doesn't
- restarting the window thread after waitKey, which has the same
results as above and
- using a new name for the namedWindow each time

In all cases, any subsequent calls to imshow block. I have no idea
why. 

For now it looks like I'll have to tell my users not to touch the
keyboard while the image is in focus! Not ideal.

If there is another nice fast pythonic way to display an image from a
very large array _programatically_, I'd love to know about it.

--

John

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor