2

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)

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)
.
.
.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.