Click on a button to open a pdf file in python and QT

2

I'm a newbie programming in Python and my idea is that by clicking on the button that calls "Botonawg" a file pdf is opened. I would like this to work both in windows and systems UNIX .

I use GNU-Linux ARCh . I made this code but it gives me the following error:

  

NameError: name 'QtCore' is not defined.

Code:

import sys
import webbrowser #para abrir archivos
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import uic
"""from PyQt5.QtCore import Qt"""

"""#Clase heredada de QMainWindow (Constructor de ventanas)"""
class Ventana(QMainWindow):
 #Método constructor de la clase
 def __init__(self):
  #Iniciar el objeto QMainWindow
  QMainWindow.__init__(self)
  #Cargar la configuración del archivo .ui en el objeto
  uic.loadUi("MiProyecto.ui", self)
  self.setWindowTitle("Ktronix")

  #Abrir tabla AWG


"""
  def openFile(file):
  if sys.platform == 'linux2':
     subprocess.call(["xdg-open", file])
  else:
     os.startfilefile)

""" 

QtCore.QObject.connect(self.uiself.Botonawg, QtCore.SIGNAL ('clicked()'), webbrowser.open_new(r'/home/ronal/ktronic/awg.pdf'))

I would appreciate your help.

    
asked by Ronald Forero Leon 05.09.2016 в 00:12
source

2 answers

2

Apparently your mistake is that you do not import the QtCore module of PyQt5, also I recommend making the connections within __init__()

import sys
import subprocess

from PyQt5 import QtCore

from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import uic

"""#Clase heredada de QMainWindow (Constructor de ventanas)"""
class Ventana(QMainWindow):
    #Método constructor de la clase
    def __init__(self):
        #Iniciar el objeto QMainWindow
        QMainWindow.__init__(self)
        #Cargar la configuración del archivo .ui en el objeto
        uic.loadUi("MiProyecto.ui", self)
        self.setWindowTitle("Ktronix")

        QtCore.QObject.connect(self.Botonawg, QtCore.SIGNAL ('clicked()'), this.abrirTabla)

    def abrirTabla(this):
        this.openFile(r'/home/ronal/ktronic/awg.pdf') 

    #Abrir tabla AWG
    def openFile(this, file):
        if sys.platform == 'linux2':
            #Linux detectado
            subprocess.call(["xdg-open", file])
        elif sys.platform == 'darwin':
            #MacOSx detectado
            subprocess.call(["open", file])
        elif sys.platform.startswith('win'):
            #Windows detectado
            subprocess.call(["start", file])

In reality there is no universal and portable command to tell the operating system to open a file with its particular application, however, these commands are very similar. In windows the command start , for the majority of linux distributions xdg-open and finally for Apple systems with MacOS open .

Edited: The line was deleted: import webbrowser #para abrir archivos

    
answered by 21.09.2018 / 17:05
source
-1

One of the errors that they point out is the non-importation of QtCore.

The other one that I observe is in the case is the connection:

..., webbrowser.open_new(r'/home/ronal/ktronic/awg.pdf'))

since the slot is an evaluated function and must be a callable , otherwise the file will be called as soon as the window is displayed, which is what you do not want.

On the other hand, in PyQt5 it is recommended to use the new connection syntax since it is more readable, in addition to using the decorator QtCore.pyqtSlot() because it adds speed in the call to the slot and also saves resources.

I also propose a single solution for all OS: QDesktopServices.openUrl() , this method will try Open the file with the default program of the OS.

from PyQt5 import QtCore, QtGui, QtWidgets, uic


class Ventana(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(Ventana, self).__init__(parent)
        uic.loadUi("MiProyecto.ui", self)
        self.setWindowTitle("Ktronix")
        self.Botonawg.clicked.connect(self.open_file)

    @QtCore.pyqtSlot()
    def open_file(self):
        url = QtCore.QUrl.fromLocalFile("/ruta/del/archivo.pdf")
        QtGui.QDesktopServices.openUrl(url)


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Ventana()
    w.show()
    sys.exit(app.exec_())
    
answered by 21.09.2018 в 17:21