Python Forum

Full Version: browse item and assign file to variable PYSIDE
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello,

I would like to parse a file using a GUI dialog window rather than writing the path directly; so I am using pyside to implement the window. I got the code to browse a file and import it's content as a text string; what I would need is instead to assign the file (or actually its path) I have chosen with the window to an object so that I can use another function to parse the file more properly. 

for example, I would need to select the file with the window, then assign the path, let's say,

> /home/gigiux/Documents/myFile.raw

to the variable 'x' and then parse with, for instance, 

> parseFun(x, type=raw)

How should I define the function? So far the code I wrote for browsing the file is:

    def openFile(self):
     self.fileName, self.filterName = QFileDialog.getOpenFileName(self)
     self.textEdit.setText(open(self.fileName).read())

thanks
(Apr-23-2017, 01:11 PM)Gigux Wrote: [ -> ]How should I define the function?
     self.textEdit.setText(open(self.fileName).read())

If you're going to put the contents of the file in the gui that's fine, but you still should close the file after reading it.
Hmm, and I would I do that? and more importantly, how do I do the assignment? Tx
with open(self.fileName) as file:
   self.textEdit.setText(file.read())