def save_file_dialog(Title, wx_wildcard, qt_wildcard, dirname=''): """ creates a save file dialog in wx or PyQt4/PySide """ fname = None if dirname == '': dirname = os.getcwd() if _gui_mode == 0: # wx app = wx.App(redirect=False) app.MainLoop() dlg = wx.FileDialog(None, Title, dirname, "", wx_wildcard, wx.SAVE) app.MainLoop() if dlg.ShowModal() == wx.ID_OK: filename = dlg.GetFilename() dirname = dlg.GetDirectory() fname = os.path.join(dirname, filename) elif _gui_mode in [1, 2]: # PySide, PyQt4 # checks if QApplication already exists app = QApplication.instance() if not app: # create QApplication if it doesnt exist app = QApplication([]) form = QtDialog() form.show() fname = QFileDialog.getSaveFileName(form, Title, dirname, qt_wildcard) app.exit() #print("fname =", fname) else: msg = 'Could not import wx, PySide, or PyQt4. ' \ 'Please specify the file explicitly.' raise ImportError(msg) return fname
def watchdog(self): for thread in self.threads.values(): if thread.is_alive(): return if not self.disable_qt_management: self.timer.stop() QApplication.closeAllWindows() QApplication.exit()
def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) self.setWindowIcon(QIcon(':/ifmon.png')) try: self.model = BandwidthTableModel(self) except dberrors.OperationalError as e: QMessageBox.critical(self, 'Database Error', 'Could not access database.\nERROR: %s' % e) sys.exit(QApplication.exit()) self.tableView.setModel(self.model) self.tableView.horizontalHeader().setResizeMode(QHeaderView.Stretch) self.tableView.horizontalHeader().setResizeMode(0, QHeaderView.ResizeToContents) self.tableView.setAlternatingRowColors(True) self.dateFrom.setDate(self.model.settings.start) self.dateTo.setDate(self.model.settings.start + timedelta(days=29)) self.updateTotal() self.actionAbout.triggered.connect(self.about) self.timer = QTimer() self.timer.setInterval(1000) self.timer.timeout.connect(self.updateUsage) self.timer.start()
def convert_html_to_pdf(source, destination, page_size=QPrinter.Letter, print_format=QPrinter.PdfFormat, timeout=10000, app=None): """Converts an .html file at the source to a .pdf at the destination Any external files linked in the source file must be paths relative to the location of the source file itself. If building the html file dynamically, the rel_path function can be used to create proper relative paths using the .html location as the source location. The conversion is done using th ability of a QPrinter to print a QWebView to a PDF. This means we must have some Qt bindings, either PySide (default) or PyQt4 (5 support incoming?), but then are only limited by what the QWebView can display. While the intent is so print to a PDF, the format parameter is exposed so that this can be used to print directly to a printer or (if >=Qt4.2) PostScript format. If this is being used in a larger QApplication, we should only have one instance of QApplication, so pass the existing instance to the `app` parameter. """ if app is None: app = QApplication(sys.argv) view = QWebView() # We want to ensure the page was fully loaded before printing, so # we wait for the loadFinished event to fire. with wait_for_signal(view.loadFinished, timeout=timeout): # QUrl requires absolute path names view.load(QUrl.fromLocalFile(os.path.abspath(source))) # With the QWebView loaded, we now print to the destination PDF printer = QPrinter() printer.setPageSize(page_size) printer.setOutputFormat(print_format) printer.setOutputFileName(destination) view.print_(printer) # Exit the application app.exit()
def convert_html_to_pdf( source, destination, page_size=QPrinter.Letter, print_format=QPrinter.PdfFormat, timeout=10000, app=None): """Converts an .html file at the source to a .pdf at the destination Any external files linked in the source file must be paths relative to the location of the source file itself. If building the html file dynamically, the rel_path function can be used to create proper relative paths using the .html location as the source location. The conversion is done using th ability of a QPrinter to print a QWebView to a PDF. This means we must have some Qt bindings, either PySide (default) or PyQt4 (5 support incoming?), but then are only limited by what the QWebView can display. While the intent is so print to a PDF, the format parameter is exposed so that this can be used to print directly to a printer or (if >=Qt4.2) PostScript format. If this is being used in a larger QApplication, we should only have one instance of QApplication, so pass the existing instance to the `app` parameter. """ if app is None: app = QApplication(sys.argv) view = QWebView() # We want to ensure the page was fully loaded before printing, so # we wait for the loadFinished event to fire. with wait_for_signal(view.loadFinished, timeout=timeout): # QUrl requires absolute path names view.load(QUrl.fromLocalFile(os.path.abspath(source))) # With the QWebView loaded, we now print to the destination PDF printer = QPrinter() printer.setPageSize(page_size) printer.setOutputFormat(print_format) printer.setOutputFileName(destination) view.print_(printer) # Exit the application app.exit()
def load_file_dialog(title, wx_wildcard, qt_wildcard, dirname=''): # type: (str, str, str, str) -> (str, str) """ creates a load file dialog in wx or PyQt4/PySide """ fname = None if dirname == '': dirname = os.getcwd() wildcard_level = None assert isinstance(_gui_mode, str), _gui_mode if _gui_mode == 'wx': # wx app = wx.App(redirect=False) app.MainLoop() dlg = wx.FileDialog(None, title, dirname, "", wx_wildcard, wx.OPEN) app.MainLoop() if dlg.ShowModal() == wx.ID_OK: filename = dlg.GetFilename() dirname = dlg.GetDirectory() fname = os.path.join(dirname, filename) elif _gui_mode in ['pyqt', 'pyside']: # checks if QApplication already exists app = QApplication.instance() if not app: # create QApplication if it doesnt exist app = QApplication([]) form = QtDialog() form.show() output = QFileDialog.getOpenFileName(form, title, dirname, qt_wildcard) if len(output) == 1: fname, wildcard_level = output else: fname = output app.exit() else: msg = 'Could not import wx, PySide, or PyQt4/5. '\ 'Please specify the file explicitly.' + '\ngui_mode=%r' % _gui_mode raise ImportError(msg) return fname, wildcard_level
def updateUsage(self): d = self.dateFrom.date() start = datetime(year=d.year(), month=d.month(), day=d.day()) d = self.dateTo.date() end = datetime(year=d.year(), month=d.month(), day=d.day()) try: self.model.populateData(start, end) except dberrors.OperationalError as e: QMessageBox.critical(self, 'Database Error', 'Could not access database.\nERROR: %s' % e) sys.exit(QApplication.exit()) self.updateTotal()
class qt_method(common_method): def create_browser(self): try: from PySide.QtCore import QUrl from PySide.QtGui import QApplication from PySide.QtWebKit import QWebView except ImportError: raise self.raise_error("You need python-pyside\nDownload: http://developer.qt.nokia.com/wiki/PySide_Binaries_Linux") self.app = QApplication(sys.argv) self.web = QWebView() self.web.load(QUrl(self.OAUTH_URL)) self.web.loadFinished[bool].connect(self.load_finished) self.web.show() self.app.exec_() def get_url(self): return self.web.url().toString() def destroy(self): self.web.close() self.app.exit()
def main(argv=None): app = QApplication(argv or sys.argv) locale = QLocale.system().name() trans = QTranslator() locale_path = '%s/locale/ifmon_%s' % (os.path.dirname( os.path.dirname(os.path.realpath(__file__))), locale.lower()) trans.load(locale_path) app.installTranslator(trans) try: setup_db() except: QMessageBox.critical( None, 'Database error', 'Could not find the database.\n' 'Please install it properly by running install.py on Ubuntu.\n' 'For other Linux distributions, check the requirements.', QMessageBox.Ok) sys.exit(app.exit()) else: mw = MainWindow() mw.show() sys.exit(app.exec_())
def main(argv=None): app = QApplication(argv or sys.argv) locale = QLocale.system().name() trans = QTranslator() locale_path = '%s/locale/ifmon_%s' % (os.path.dirname(os.path.dirname( os.path.realpath(__file__))), locale.lower()) trans.load(locale_path) app.installTranslator(trans) try: setup_db() except: QMessageBox.critical(None, 'Database error', 'Could not find the database.\n' 'Please install it properly by running install.py on Ubuntu.\n' 'For other Linux distributions, check the requirements.', QMessageBox.Ok) sys.exit(app.exit()) else: mw = MainWindow() mw.show() sys.exit(app.exec_())
# -*- coding: utf-8 -*- """ Created on Fri Apr 10 15:03:50 2015 @author: rbanderson """ import sys import PySide from PySide.QtGui import QApplication from PySide.QtGui import QMessageBox app=QApplication.instance() if app is None: app=QApplication(sys.argv) msgBox=QMessageBox() msgBox.setText("Hello World - using PySide version "+PySide.__version__) msgBox.exec_() app.exit()
def on_action_exit_triggered(self): """Terminates the application normally.""" QApplication.exit()
def on_timedout(): webthumbnailer.save(args.out, args.width, args.height) QApplication.exit(0 if webthumbnailer.ok else 1)
def on_finished(ok): webthumbnailer.save(args.out, args.width, args.height) QApplication.exit(0 if ok else 1)
def stop(self): if self.clipThread is not None: self.stopClipboardThread() QApplication.exit() sys.exit()