olanre
olanre

Reputation: 31

PyQt Open File Dialog - Display Path Name

Using PyQt, I have created a button that can choose and upload a file from any directory. How do I create a text box next to the button that would display the path of the file selected or opened?

Here is my sample code:

    self.uploadButton = QtGui.QPushButton('UPLOAD SDF', self)
    self.runfilterButton = QtGui.QPushButton('Run Filter', self)
    self.printimagesButton = QtGui.QPushButton('Display Matches Images', self)
    self.listmatchesButton = QtGui.QPushButton('List Matches', self)

    self.uploadButton.move (100, 50)
    self.runfilterButton.move (400,50)
    self.printimagesButton.move (200, 100)
    self.printimagesButton.resize (200, 50)
    self.listmatchesButton.move (200, 150)
    self.listmatchesButton.resize (200, 50)
    hBoxLayout = QtGui.QHBoxLayout()

    hBoxLayout.addWidget(self.uploadButton)
    hBoxLayout.addWidget(self.runfilterButton)
    self.setLayout(hBoxLayout)
    # Signal Init.
    self.connect(self.uploadButton, QtCore.SIGNAL('clicked()'), self.open)
    self.runfilterButton.clicked.connect(runfilterx)
    self.printimagesButton.clicked.connect(printimages)
    self.listmatchesButton.clicked.connect(listmatches)

Upvotes: 2

Views: 20866

Answers (2)

Digvijayad
Digvijayad

Reputation: 534

First you need to create a TextEdit object, like self.myTextBox = QtGui.QTextEdit(self). After that place it wherever you want it on the screen. Then you need to connect it with your open function. It should look something like this.

def open(self):
    fileName = QtGui.QFileDialog.getOpenFileName(self, 'OpenFile')
    self.myTextBox.setText(fileName)
    print(fileName)

This will display the path of chosen file on your textBox.

Upvotes: 8

M.SEL
M.SEL

Reputation: 113

You can use only one method to connect TextEdit(or LineEdit) with QPushButton in case you have multiple QFileDialog jobs in your project :

def getPath(self , targetTextEdit , targetPushButton):
        targetPushButton.clicked.connect(lambda: targetTextEdit.setText(QtWidgets.QFileDialog.getOpenFileName(self, 'OpenFile')[0]))  

self.getPath( self.textEdit1 ,self.pushButton1)
self.getPath( self.textEdit2 ,self.pushButton2)
.
.
.

Upvotes: 0

Related Questions