我对Python非常陌生,所以请耐心等待。 我有一段使用Python 3和PyQt5打开QFileDialog的代码:
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFileDialog import sys class MCVE(QWidget): def __init__(self): super().__init__() self.initialize() def initialize(self): self.setWindowTitle('MCVE') self.setGeometry(50, 50, 400, 200) btn = QPushButton('Examle', self) btn.clicked.connect(self.clicked) self.show() def clicked(self): filename = QFileDialog.getOpenFileName( self, "Open Template", "c:\\", "Templates (*.xml);;All Files (*.*)") print(filename) if __name__ == '__main__': app = QApplication(sys.argv) ex = MCVE() sys.exit(app.exec_())在Python 2中使用PyQt4 print(filename)语句,在按下取消按钮后,输出为空字符串。 当我使用PyQt5在Python 3中运行相同的代码时,我得到:
('','')
注意:引号是单引号
有人可以解释发生了什么吗? 在PyQt4和PyQt5之间的文档中找不到任何东西。 我知道Python 2和Python 3之间的字符串发生了变化,但我不确定这些更改会导致这样的问题。 谢谢!
I have a block of code that opens a QFileDialog using Python3 and PyQt5:
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFileDialog import sys class MCVE(QWidget): def __init__(self): super().__init__() self.initialize() def initialize(self): self.setWindowTitle('MCVE') self.setGeometry(50, 50, 400, 200) btn = QPushButton('Example', self) btn.clicked.connect(self.clicked) self.show() def clicked(self): filename = QFileDialog.getOpenFileName( self, "Open Template", "c:\\", "Templates (*.xml);;All Files (*.*)") print(filename) if __name__ == '__main__': app = QApplication(sys.argv) ex = MCVE() sys.exit(app.exec_())In Python 2 using PyQt4 the print(filename) statement, after pressing the cancel button, outputs as an empty string. When I run the same code in Python 3 using PyQt5 I get:
('', '')
NOTE: The quotes are Single Quotes
Can someone explain what is going on? I couldn't find anything under the documentation between PyQt4 and PyQt5. I know that strings changed between Python 2 and Python 3, but I'm not sure those changes would cause an issue like this. Thanks!
最满意答案
PyQt4中的getOpenFileName函数返回一个字符串,它是所选文件的名称,如果没有被选中,则返回一个空字符串。
filename = QFileDialog.getOpenFileName(self, "Open Template", "c:\\", "Templates (*.xml);;All Files (*.*)")但是在PyQt5中,它返回一个包含2个元素的元组,其中第一个是与PyQt4具有相同行为的字符串,第二个元素是使用的过滤器。
filename, filters = QFileDialog.getOpenFileName(self, "Open Template", "c:\\", "Templates (*.xml);;All Files (*.*)")注意:PyQt5的大多数文档都在Qt5中,因为通常方法的名称,条目和结果是相似的。
The getOpenFileName function in PyQt4 returns a string that is the name of the selected file, and if none is selected then it returns an empty string.
filename = QFileDialog.getOpenFileName(self, "Open Template", "c:\\", "Templates (*.xml);;All Files (*.*)")However in PyQt5 this returns a tuple of 2 elements where the first one is a string that has the same behavior as in PyQt4, and the second element is the filter used.
filename, filters = QFileDialog.getOpenFileName(self, "Open Template", "c:\\", "Templates (*.xml);;All Files (*.*)")Note: The majority of documentation of PyQt5 is in Qt5, since in general the names of the methods, the inputs and the result are similar.
更多推荐
发布评论