QFileDialog 将目录传递给 python 脚本
我正在编写一个小 python 程序,它会遍历 XML 文件并替换一些标签。 它需要三个参数:创建目录树的路径、读取的 XML 文件以及输出到的 xml 文件。 只需传递参数即可在命令行中正常工作。 因为它不仅仅适合我,我想我应该在它上面放一个 Qt 前端。 下面是Qt前端的大部分内容。 MOVtoMXF 是完成所有替换的类。 所以你可以看到,我基本上只是抓住字符串并将它们输入到我已经制作和测试的类中。
class Form(QDialog):
def ConnectButtons(self):
self.connect(self.pathBrowseB, SIGNAL("clicked()"), self.pathFileBrowse)
self.connect(self.xmlFileBrowseB, SIGNAL("clicked()"), self.xmlFileBrowse)
self.connect(self.outputFileBrowseB, SIGNAL("clicked()"), self.outputFileBrowse)
def accept(self):
path = self.pathBox.displayText()
xmlFile = self.xmlFileBox.displayText()
outFileName = self.outfileNameBox.displayText()
print path + " " + xmlFile + " " + outFileName
mov1 = MOVtoMXF.MOVtoMXF(path, xmlFile, outFileName)
mov1.ScanFile()
self.done()
def pathFileBrowse(self):
file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
self.pathBox.setText(file)
def xmlFileBrowse(self):
file = str(QFileDialog.getOpenFileName(self, "Save File"))
self.xmlFileBox.setText(file)
def outputFileBrowse(self):
file = str(QFileDialog.getSaveFileName(self, "Save File"))
self.outfileNameBox.setText(file)
问题是,当我输入路径时,它现在返回错误,要么目录不存在,要么文件
“/System/Library/Frameworks/Python.framework/Versions” 末尾有一个尾部斜杠/2.5/lib/python2.5/posixpath.py”,第 62 行,在 join 中 elif path == '' 或 path.endswith('/'):
我认为 QFileDialog、其传回的 QString 和我的 python 期望的字符串之间可能存在一些不匹配。 但我不知道如何修复它。
我在 Max OS X 10.5.6 上运行 pyQt 4.4.4 QT 4.4.0
感谢您提供的任何帮助。
标记
Im writing a little python program that goes through an XML file and does some replacement of tags. It takes three arguments, a path from whcih it creates a directory tree, the XML file its reading and the xml file its outputting to. It works fine from the command line just passing in arguments. As its not just for me, i thought id put a Qt front on it. Below is the majority of the Qt front. MOVtoMXF is the class that does all the replacement. So you can see that im basically just grabbing strings and feeding them into the class that ive already made and tested.
class Form(QDialog):
def ConnectButtons(self):
self.connect(self.pathBrowseB, SIGNAL("clicked()"), self.pathFileBrowse)
self.connect(self.xmlFileBrowseB, SIGNAL("clicked()"), self.xmlFileBrowse)
self.connect(self.outputFileBrowseB, SIGNAL("clicked()"), self.outputFileBrowse)
def accept(self):
path = self.pathBox.displayText()
xmlFile = self.xmlFileBox.displayText()
outFileName = self.outfileNameBox.displayText()
print path + " " + xmlFile + " " + outFileName
mov1 = MOVtoMXF.MOVtoMXF(path, xmlFile, outFileName)
mov1.ScanFile()
self.done()
def pathFileBrowse(self):
file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
self.pathBox.setText(file)
def xmlFileBrowse(self):
file = str(QFileDialog.getOpenFileName(self, "Save File"))
self.xmlFileBox.setText(file)
def outputFileBrowse(self):
file = str(QFileDialog.getSaveFileName(self, "Save File"))
self.outfileNameBox.setText(file)
the probelm is that when i feed in a Path, it now comes back with an error, either the directory doesnt exist, or if I have a trailing slash on the end that
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/posixpath.py", line 62, in join
elif path == '' or path.endswith('/'):
I think its probably some mismatch between the QFileDialog, the QString its passing back and the string the my python expects. but im not sure how to go about fixing it.
Im running on Max OS X 10.5.6
pyQt 4.4.4
QT 4.4.0
thanks for any help you can give.
Mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两个潜在的解决方案。
方法 1:
如果必须使用 displayText() 方法,我建议您使用显式字符串转换包装对 displayText() 的调用:
原因是 displayText() 返回我认为是 C++ 级别的常量内存引用,这意味着您不会返回 QString 的副本,而是实际上返回内存引用中可用的任何 QString。
当您调用 displayText() 函数时,它是您期望的字符串,但最终当内存引用的内容发生更改时,它会是其他内容。 我注意到不同控件上的几种方法的这种特殊性,最明显的是 QDateEdit/QDateTimeEdit/QTimeEdit 控件,在这些控件中,我通常必须通过将 QDateEdit 的 date() 函数返回的 QDate 包装在一个显式副本中,将其包装在QDate 构造函数。
方法 2:
否则,请使用 text() 方法。 返回的 QString 是一个常量值,而不是常量内存引用。 请参阅此文档:
http://doc.trolltech.com/4.4/qlineedit。 html#text-prop
更新:
看来 Riverbank 将在 PyQt 的未来版本中解决此问题,以防有人仍然遇到此问题:
PyQt4 路线图
Two potential solutions.
Method 1:
If you must use the displayText() method, I suggest you wrap the call to displayText() with an explicit string cast:
The reason is that displayText() returns what I believe is a constant memory reference at the C++ level, meaning that you are not being returned a copy of the QString, but actually whatever QString is available at the memory reference.
When you call the displayText() function, it is the string you expected, but eventually it is something else when the contents at the memory reference are changed. I have noticed this peculiarity with several methods on different controls, most notably QDateEdit/QDateTimeEdit/QTimeEdit controls, where I typically have to make an explicit copy of, say, the QDate returned by the date() function of QDateEdit by wrapping it in a QDate constructor.
Method 2:
Otherwise, use the text() method instead. The QString returned is a constant value, instead of a constant memory reference. See this doc:
http://doc.trolltech.com/4.4/qlineedit.html#text-prop
Update:
It looks like Riverbank will be addressing this problem in future versions of PyQt in case anybody is still having this problem:
PyQt4 Roadmap