PyQt4和PyQt5中的QFileDialog字符串之间有区别吗? [英] Is there a difference between QFileDialog strings in PyQt4 and PyQt5?

查看:154
本文介绍了PyQt4和PyQt5中的QFileDialog字符串之间有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码使用Python3和PyQt5打开QFileDialog:

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_())

在使用PyQt4的Python 2中,按下取消按钮后,print(filename)语句输出为空字符串.当我使用PyQt5在Python 3中运行相同的代码时,我得到:

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:

('','')

('', '')

注意:引号是单引号

有人可以解释发生了什么吗?我在PyQt4和PyQt5之间的文档下找不到任何内容.我知道在Python 2和Python 3之间字符串发生了变化,但是我不确定这些变化是否会引起这样的问题.谢谢!

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 函数返回一个字符串,该字符串是所选文件的名称,如果未选择任何文件,则它将返回一个空字符串.

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 (*.*)")

但是在PyQt5中,它返回一个由2个元素组成的元组,其中第一个是与PyQt4具有相同行为的字符串,第二个元素是所使用的过滤器.

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 (*.*)")

注意:PyQt5的大多数文档都在Qt5中,因为通常方法的名称,输入和结果都是相似的.

这篇关于PyQt4和PyQt5中的QFileDialog字符串之间有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆