Reputation: 67
I'm trying to create an app which should allow to choose a directory and after pressing the button send the selected path to another function outside the App class. Also there is a predefined directory set as default path and displayed in the QLineEdit widget. This is what I've tried:
import os
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
from functools import partial
class App (QtGui.QMainWindow):
def __init__(self):
super(App, self).__init__()
self.initUI()
def initUI(self):
default_path = os.path.dirname(os.path.abspath(__file__))
def selectDir():
labelSavePath.setText(QtGui.QFileDialog.getExistingDirectory(self,
'Select Dir:'))
self.setFixedSize(450, 150)
self.setWindowTitle('App')
labelSavePath = QtGui.QLineEdit(default_path, self)
labelSavePath.setGeometry(15, 72, 340, 20)
labelSavePath.setAlignment(QtCore.Qt.AlignLeft)
labelSavePath.setReadOnly(True)
btnBrowse = QtGui.QPushButton('Browse', self)
btnBrowse.setGeometry(365, 67, 80, 30)
btnBrowse.clicked.connect(selectDir)
btnStart = QtGui.QPushButton('Start', self)
btnStart.move(345, 115)
btnStart.clicked.connect(partial(startRun,
str(labelSavePath.text())))
self.show()
def startRun(log_path):
print(log_path) #and do smth useful
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = App()
sys.exit(app.exec_())
However, the log_path is always equal to default_path no matter if it is changed by the user or not. I have been looking for the answer for a couple of hours already and still haven't managed to find the bug, since I'm new to PyQt.
How should I edit/pass the text in the labelSavedPath widget correctly, minding possible changes?
Upvotes: 1
Views: 1948
Reputation: 120578
You seem to have misunderstood what partial does. It allows you to call a function with some its arguments pre-filled with fixed values - which is exactly what you don't want. Use a lambda
instead:
btnStart.clicked.connect(lambda: startRun(labelSavePath.text()))
NB: if you're using Python 3, there's no need to wrap everything with str()
.
Upvotes: 1