1

I wrote a code that work well in console python, an example:

from qgis.core import QgsProject, QgsLayerTreeGroup, QgsLayerTreeLayer, QgsLayerTree
from PyQt5.QtCore import Qt, QVariant, QSettings, QTranslator, qVersion, 

QCoreApplication
from PyQt5 import QtGui
from PyQt5.QtGui import QAction, QIcon, QFileDialog, QProgressDialog, QProgressBar, QShortcut, QKeySequence
from osgeo import ogr
from shutil import copyfile

from urllib.request import urlopen
import re

import processing, glob, os, time


newLyrName1 = '1_PdR da controllare 4326'
newLyrName2 = '2_PdR attivi da controllare 4326'

#definisco il progressivo delle operazioni
m=0
n=0

#Definisco il nome dei campi da utilizzare come variabili
fieldX = 'X_COORD'
fieldY = 'Y_COORD'

m=m+1
n=0
#creo un popup che richiede una risposta
msgBOX = QtGui.QMessageBox.question(iface.mainWindow(), '',#titolo
    str(m) + " - Importare i PdR da file excel?", #messaggi
    QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) #pulsanti

And I saved it in a file called "importXLS.py" stored in a folder "script". Then I built a plugin with a custom toolbar. enter image description here

Now, what I want to do is that when I press a button it call and execute a pyqgis script inside a console python but, for now, I can only open a console python but I cannot execute nothing inside it.

2 Answers 2

2

Add the following line to the imports of the plugin file.

from pathlib import Path # import section

Then use the next lines in the code that will run when the button is clicked.

python_file_path = "c:/test/importXLS.py"
exec(Path(python_file_path ).read_text())
4
  • Excuse me because I'm newbie... What you mean to "import section"? Above the first class? o under def __init__(self, iface):? I tried above first class, but nothin happens...
    – freecma80
    Commented Aug 30, 2023 at 12:41
  • Top lines of a Python file are known as import section containing the words of import and from Commented Aug 30, 2023 at 17:12
  • As you suggested I added the <from pathlib import Path> in the first part of the code, before class declaration. then I wrote the code under run definition with right path. Restarting QGis it don't give me and error pressing on button, but it not start the <QtGui.QMessageBox.question> so I cannot understad if something gone wrong, what is, and where
    – freecma80
    Commented Aug 31, 2023 at 9:55
  • Ok... now I know what's going wrong. Your code works well, but the code on the file path must have the syntax of qgis plugin. The problem is that the cose is writted for python console syntax and, because is very long and complex, I cannot translate it. So there's a way to use it as i do in python console?
    – freecma80
    Commented Aug 31, 2023 at 10:15
0

Looking around, I found this post: How to call QgisInterface class in a plugin. It helps me having and idea: first I changed iface before call and run a script:

enter image description here

Then anytime, when the script is called and run by QGis, if on console python QGis advised me that it cannot run the script because a function is not defined:

enter image description here

I will add it at the start of the code that I call by plugin script:

enter image description here

In that way I can run python console scripts by QGis plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.