[PyQt] QMessageBox.question returns NoButton in python interactive

Thomas Caswell tcaswell at gmail.com
Thu Jun 27 22:05:13 BST 2019


ok, it is defiantly related to PyOS_InputHook.

I think I have the rough outlines of this correct below, but may have some
details/terminology wrong.

When using pyqt "interactively" the infinite loop of the terminal and the
Qt event loop need to be interlaced.  This is done (in plain python) via
the PyOS_InputHook which is a c-function which is run while the terminal is
waiting for the (slow) humans to type something.  The terminal than handles
the keystroke (including possibly running what ever python there is run)
and the call the input hook again.  On posix systems the Qt event loop is
run forever and then file descriptors / sockets are used to interrupt the
event loop when the user hits a key, however on windows we do not have that
and instead the input hook runs the Qt event loop for a short period of
time, checks if a key has been hit, and if not loops.

Modal windows in Qt work by blocking the event loop of their parent and the
run their own event loop (which effectively freeze's their parents, but
allows the modal window to be responsive).  However, when the outer event
loop is interrupted, the question window gets closed and
`QtWidgets.QMessageBox.question(...) returns NoButton.  In the case of MS
Windows, this happens on some polling loop so the window just flashes.  On
linux (and I suspect OSX) you can get the same behavior by clicking on the
button to pop up the question, but then typing into the terminal (any key
will do) before you click on the question box.

Tom


On Thu, Jun 27, 2019 at 4:12 PM Thomas Caswell <tcaswell at gmail.com> wrote:

> I suspect, but can not prove, that this is related to PyOS_InputHook and
> may depend on exactly which terminal emulator you are using on windows.
>
> Tom
>
> On Thu, Jun 27, 2019 at 3:01 PM Kyle Altendorf <sda at fstab.net> wrote:
>
>> Seems to work ok in 5.9.2 and 5.12.1 in Linux here.  I get 16384 (Yes)
>> and 65536 (No).
>>
>> Though this is an odd program.  I thought message boxes maybe ran their
>> own event loop if needed but is showing a button and expecting it's signals
>> to emit functionally really proper?
>>
>> Cheers,
>>
>> -kyle
>>
>>
>> On 2019-06-27 14:36, Luke Campagnola wrote:
>>
>> Greetings,
>> I have found that when calling QMessageBox.question, the message box
>> flashes up briefly before disappearing and the call returns NoButton. This
>> behavior is present in both PyQt4 and PyQt5 (most recently tested on 5.9.2
>> from anaconda package), on windows (but not linux), and only if I have not
>> called QApplication.exec_() because I am running in an interactive python
>> shell.
>>
>> Code to reproduce (run with python -i):
>>
>> import sys
>> #from PyQt4 import QtGui as QtWidgets
>> from PyQt5 import QtWidgets
>>
>> app = QtWidgets.QApplication([])
>>
>> def msg():
>>    print(QtWidgets.QMessageBox.question(btn, "title", "message"))
>>
>> btn = QtWidgets.QPushButton("show messagebox")
>> btn.clicked.connect(msg)
>> btn.show()
>>
>> # message box works if event loop is started
>> # app.exec_()
>>
>>
>> Cheers,
>> Luke
>>
>> _______________________________________________
>> PyQt mailing list    PyQt at riverbankcomputing.com
>> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>>
>> _______________________________________________
>> PyQt mailing list    PyQt at riverbankcomputing.com
>> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>>
>
>
> --
> Thomas Caswell
> tcaswell at gmail.com
>


-- 
Thomas Caswell
tcaswell at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190627/42712d3e/attachment-0001.html>


More information about the PyQt mailing list