PyQt4和PyQt5中的QFileDialog字符串有区别吗?(Is there a difference between QFileDialog strings in PyQt4 and PyQt

编程入门 行业动态 更新时间:2024-10-09 13:30:10
PyQt4和PyQt5中的QFileDialog字符串有区别吗?(Is there a difference between QFileDialog strings in PyQt4 and PyQt5?)

我对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.

更多推荐

本文发布于:2023-04-29 07:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335606.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   区别   QFileDialog   difference   strings

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!