我在Python中使用Qt很新,我需要知道如何从文件对话框中获取一个目录路径并将其显示在我的ui内的文本框中。
class ControllerLibraryUI(QtWidgets.QDialog):
def __init__(self):
super(ControllerLibraryUI, self).__init__()
self.setWindowTitle('Controller Window')
self.buildUI()
def buildUI(self):
layout = QtWidgets.QVBoxLayout(self)
textWidget = QtWidgets.QWidget()
textLayout = QtWidgets.QHBoxLayout(textWidget)
layout.addWidget(textWidget)
self.saveNameField = QtWidgets.QTextEdit()
textLayout.addWidget(self.saveNameField)
btnWidget = QtWidgets.QWidget()
btnLayout = QtWidgets.QHBoxLayout(btnWidget)
layout.addWidget(btnWidget)
importBtn = QtWidgets.QPushButton('Import')
importBtn.clicked.connect(self.load)
btnLayout.addWidget(importBtn)
closeBtn = QtWidgets.QPushButton('Close')
closeBtn.clicked.connect(self.close)
btnLayout.addWidget(closeBtn)
def load(self ):
filename = QtWidgets.QFileDialog.getOpenFileName()[0]
print filename
def showUI():
try:
ui.close()
except:
ui = ControllerLibraryUI()
ui.show()
return ui
ui = showUI()
这是基本概念。
答案 0 :(得分:0)
用于文件对话框:
directory = QtGui.QFileDialog.getExistingDirectory(self, 'Select directory')
然后将目录变量从文本框
分配给setText方法