[PyQt] Testing a standalone dialog widget

Andre Roberge andre.roberge at gmail.com
Sun Dec 28 13:19:17 GMT 2014


I'm creating a series of "standalone widgets" that can be used in
procedural programs; the original idea is from the easygui project.  To
give a concrete idea of their use, instead of writing:

->>> name = input("What is your name? )

I could write

->>> name = get_string("What is your name? )

and a dialog would pop up, inviting the user to enter the response.

I would like to set up some automatic testing of these widgets.  I tried
with a third-party module
(pyautogui) which interacts with GUI programs, but the result is not
totally reliable.
I would prefer to use QTest but do not know how to connect with the dialog;
I suspect I may have
to use threading (as I had to do with the pyautogui solution) as the dialog
is effectively blocking the execution of the program.

Here is a simple implementation of get_string() mentioned above:

from PyQt4 import QtGui

def get_string(prompt="What is your name? ", title="Title",
               default_response="PyQt4", app=None):
    """GUI equivalent of input()."""

    if app is None:
        app = QtGui.QApplication([])
    app.dialog = QtGui.QInputDialog()
    text, ok = app.dialog.getText(None, title, prompt,
                                  QtGui.QLineEdit.Normal,
                                  default_response)
    app.quit()
    if ok:
        return text

if __name__ == '__main__':
    print(get_string())  # normal blocking mode
    app2 = QtGui.QApplication([])
        # perhaps start a delayed thread here, using QTest
    print(get_string(app=app2))


=======
Any help would be appreciated.

André Roberge
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20141228/6b3590c3/attachment.html>


More information about the PyQt mailing list