From 5332c198c2ca82782cc3790bd31181980747b156 Mon Sep 17 00:00:00 2001 From: Robert Jordens Date: Sat, 7 May 2016 19:01:14 +0200 Subject: [PATCH] gui/tools: add getOpenFileName --- artiq/gui/tools.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/artiq/gui/tools.py b/artiq/gui/tools.py index 7782165d6..e89bf0787 100644 --- a/artiq/gui/tools.py +++ b/artiq/gui/tools.py @@ -1,6 +1,7 @@ +import asyncio import logging -from PyQt5 import QtCore, QtWidgets, QtGui +from PyQt5 import QtCore, QtWidgets def log_level_to_name(level): @@ -45,3 +46,18 @@ class LayoutWidget(QtWidgets.QWidget): def addWidget(self, item, row=0, col=0, rowspan=1, colspan=1): self.layout.addWidget(item, row, col, rowspan, colspan) + + +async def getOpenFileName(parent, caption, dir, filter): + """like QtWidgets.QFileDialog.getOpenFileName(), but a coroutine""" + dialog = QtWidgets.QFileDialog(parent, caption, dir, filter) + dialog.setFileMode(dialog.ExistingFile) + dialog.setAcceptMode(dialog.AcceptOpen) + fut = asyncio.Future() + + def on_accept(): + fut.set_result(dialog.selectedFiles()[0]) + dialog.accepted.connect(on_accept) + dialog.rejected.connect(fut.cancel) + dialog.open() + return await fut