我想获取目录路径。
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QFileDialog, QMainWindow
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
uic.loadUi('view.ui', self)
self.btn = self.pushButton
self.btn.clicked.connect(self.get_directory)
def get_directory(self):
path = QFileDialog.getExistingDirectory(
self,
'Open a folder',
'H:/',
QFileDialog.ShowDirsOnly
)
print(path)
if __name__ == '__main__':
app = QApplication(sys.argv)
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())
它运作完美!
现在,我想使用MVC原理进行同样的操作。
view.py
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QFileDialog, QMainWindow
from controler import GetDirectory
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
uic.loadUi('view.ui', self)
self.qfile_dialog = QFileDialog
self.btn = self.pushButton
self.cnt = GetDirectory(self)
self.btn.clicked.connect(self.cnt.get_directory)
if __name__ == '__main__':
app = QApplication(sys.argv)
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())
controler.py
class GetDirectory:
def __init__(self, window):
self.w = window
def get_directory(self):
path = self.w.qfile_dialog.getExistingDirectory(
self,
'Open a folder',
'H:/',
self.w.qfile_dialog.ShowDirsOnly
)
print(path)
然后我得到这个错误:
TypeError:getExistingDirectory(父级:QWidget =无,标题:str = ”,目录:str =”,选项:Union [QFileDialog.Options, QFileDialog.Option] = QFileDialog.ShowDirsOnly):参数1具有 意外的类型“ GetDirectory”
我怎么了?