Is there a difference between QFileDialog strings in PyQt4 and PyQt5?












2















I'm very new to python, so please be patient. I have a block of code that opens a QFileDialog using Python 3 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!










share|improve this question

























  • It's not clear how the AttributeError in your title is involved in any of the stuff you describe in the body. Are you getting an AttributeError or not?

    – user2357112
    Oct 31 '17 at 17:33











  • @eyllanesc That was totally my bad, when I clicked on 'Ask a Question' it had a previous question I was going to ask before I figured out what was going on. So the title was set from that. I have updated this!!

    – artomason
    Oct 31 '17 at 17:35













  • @eyllanesc It seems empty in Python 2.7.14 and PyQt 4.11.4

    – artomason
    Oct 31 '17 at 17:39
















2















I'm very new to python, so please be patient. I have a block of code that opens a QFileDialog using Python 3 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!










share|improve this question

























  • It's not clear how the AttributeError in your title is involved in any of the stuff you describe in the body. Are you getting an AttributeError or not?

    – user2357112
    Oct 31 '17 at 17:33











  • @eyllanesc That was totally my bad, when I clicked on 'Ask a Question' it had a previous question I was going to ask before I figured out what was going on. So the title was set from that. I have updated this!!

    – artomason
    Oct 31 '17 at 17:35













  • @eyllanesc It seems empty in Python 2.7.14 and PyQt 4.11.4

    – artomason
    Oct 31 '17 at 17:39














2












2








2








I'm very new to python, so please be patient. I have a block of code that opens a QFileDialog using Python 3 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!










share|improve this question
















I'm very new to python, so please be patient. I have a block of code that opens a QFileDialog using Python 3 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!







python pyqt pyqt4 pyqt5 qfiledialog






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 5:55







artomason

















asked Oct 31 '17 at 17:30









artomasonartomason

648415




648415













  • It's not clear how the AttributeError in your title is involved in any of the stuff you describe in the body. Are you getting an AttributeError or not?

    – user2357112
    Oct 31 '17 at 17:33











  • @eyllanesc That was totally my bad, when I clicked on 'Ask a Question' it had a previous question I was going to ask before I figured out what was going on. So the title was set from that. I have updated this!!

    – artomason
    Oct 31 '17 at 17:35













  • @eyllanesc It seems empty in Python 2.7.14 and PyQt 4.11.4

    – artomason
    Oct 31 '17 at 17:39



















  • It's not clear how the AttributeError in your title is involved in any of the stuff you describe in the body. Are you getting an AttributeError or not?

    – user2357112
    Oct 31 '17 at 17:33











  • @eyllanesc That was totally my bad, when I clicked on 'Ask a Question' it had a previous question I was going to ask before I figured out what was going on. So the title was set from that. I have updated this!!

    – artomason
    Oct 31 '17 at 17:35













  • @eyllanesc It seems empty in Python 2.7.14 and PyQt 4.11.4

    – artomason
    Oct 31 '17 at 17:39

















It's not clear how the AttributeError in your title is involved in any of the stuff you describe in the body. Are you getting an AttributeError or not?

– user2357112
Oct 31 '17 at 17:33





It's not clear how the AttributeError in your title is involved in any of the stuff you describe in the body. Are you getting an AttributeError or not?

– user2357112
Oct 31 '17 at 17:33













@eyllanesc That was totally my bad, when I clicked on 'Ask a Question' it had a previous question I was going to ask before I figured out what was going on. So the title was set from that. I have updated this!!

– artomason
Oct 31 '17 at 17:35







@eyllanesc That was totally my bad, when I clicked on 'Ask a Question' it had a previous question I was going to ask before I figured out what was going on. So the title was set from that. I have updated this!!

– artomason
Oct 31 '17 at 17:35















@eyllanesc It seems empty in Python 2.7.14 and PyQt 4.11.4

– artomason
Oct 31 '17 at 17:39





@eyllanesc It seems empty in Python 2.7.14 and PyQt 4.11.4

– artomason
Oct 31 '17 at 17:39












1 Answer
1






active

oldest

votes


















3














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 entries and the result are similar.






share|improve this answer
























  • In this case, what would be the most reliable way to eliminate the filter part so I get a string with only the path name and the file? Basically mimicing PyQt4's behavior when converting to PyQt5. Example C:SomeArbitraryDirectoryfile.xml

    – artomason
    Oct 31 '17 at 17:58













  • Saw that after asking, thank you! Works just as you described

    – artomason
    Oct 31 '17 at 18:04











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47041271%2fis-there-a-difference-between-qfiledialog-strings-in-pyqt4-and-pyqt5%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














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 entries and the result are similar.






share|improve this answer
























  • In this case, what would be the most reliable way to eliminate the filter part so I get a string with only the path name and the file? Basically mimicing PyQt4's behavior when converting to PyQt5. Example C:SomeArbitraryDirectoryfile.xml

    – artomason
    Oct 31 '17 at 17:58













  • Saw that after asking, thank you! Works just as you described

    – artomason
    Oct 31 '17 at 18:04
















3














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 entries and the result are similar.






share|improve this answer
























  • In this case, what would be the most reliable way to eliminate the filter part so I get a string with only the path name and the file? Basically mimicing PyQt4's behavior when converting to PyQt5. Example C:SomeArbitraryDirectoryfile.xml

    – artomason
    Oct 31 '17 at 17:58













  • Saw that after asking, thank you! Works just as you described

    – artomason
    Oct 31 '17 at 18:04














3












3








3







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 entries and the result are similar.






share|improve this answer













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 entries and the result are similar.







share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 31 '17 at 17:45









eyllanesceyllanesc

82.3k103259




82.3k103259













  • In this case, what would be the most reliable way to eliminate the filter part so I get a string with only the path name and the file? Basically mimicing PyQt4's behavior when converting to PyQt5. Example C:SomeArbitraryDirectoryfile.xml

    – artomason
    Oct 31 '17 at 17:58













  • Saw that after asking, thank you! Works just as you described

    – artomason
    Oct 31 '17 at 18:04



















  • In this case, what would be the most reliable way to eliminate the filter part so I get a string with only the path name and the file? Basically mimicing PyQt4's behavior when converting to PyQt5. Example C:SomeArbitraryDirectoryfile.xml

    – artomason
    Oct 31 '17 at 17:58













  • Saw that after asking, thank you! Works just as you described

    – artomason
    Oct 31 '17 at 18:04

















In this case, what would be the most reliable way to eliminate the filter part so I get a string with only the path name and the file? Basically mimicing PyQt4's behavior when converting to PyQt5. Example C:SomeArbitraryDirectoryfile.xml

– artomason
Oct 31 '17 at 17:58







In this case, what would be the most reliable way to eliminate the filter part so I get a string with only the path name and the file? Basically mimicing PyQt4's behavior when converting to PyQt5. Example C:SomeArbitraryDirectoryfile.xml

– artomason
Oct 31 '17 at 17:58















Saw that after asking, thank you! Works just as you described

– artomason
Oct 31 '17 at 18:04





Saw that after asking, thank you! Works just as you described

– artomason
Oct 31 '17 at 18:04




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47041271%2fis-there-a-difference-between-qfiledialog-strings-in-pyqt4-and-pyqt5%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)