1

I want to modify the copy_coords plugin such that when I click on it, It should open a dialog box and ask for a file name and address. After this it should store all the points I click on into that file. As of now, I have modified the code such that it will save it in a CSV file which is predetermined, so rather than saving it in a predetermined file, it is better to store it in a file determined by the user.

I have added these few lines at the end of the original code:

xx=str(point.x())
yy=str(point.y())
clipboard= QApplication.clipboard()
clipboard.setText(str(xx)+","+str(yy)) #This is the original part of the code
#these are the lines i have added
fd=open("XYZ.csv","a")
fd.write(str(xx)+","+str(yy)+"\n")
fd.close

So I need help on getting the input dialog for getting the filename and address. Please note, I am new to Python.

1

Here is a file dialog example using the QFileDialog object which gets the selected .csv filename path:

from PyQt4 import QtGui
from PyQt4.QtGui import QFileDialog

# put this statment below at the bottom of the def run(self): function
fileNamePath = QtGui.QFileDialog.getOpenFileName(None, "Choose csv file", 'C:/Temp', '*.csv')

This should open a file dialog each time you click on the plugin button.

  • getopenfilename is useful if the file was previously created, wouldn't getsavefile be a better option? – Sunny Naik Jun 23 '17 at 4:22
  • @SunnyNaik, getsavefile will just save it, I thought you wanted to open it and append to it? getOpenFileName will get you the path which you may then open it and append values to it. – artwork21 Jun 23 '17 at 12:27
  • Yes, but also I should be able to create the file if it does not exist. – Sunny Naik Jun 27 '17 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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