Name Error: name 'QFileDialog' is not defined
<p>I am working on coursework for computer science and can't work out why the piece of code isn't working. I am trying to connect a button that I've created in PyQt4 so that when it is pressed it shows a directory dialogue:</p>

<pre><code>self.Browse_Button_1 = QtGui.QToolButton(self.tab)
    self.Browse_Button_1.setGeometry(QtCore.QRect(360, 30, 61, 20))
    self.Browse_Button_1.setObjectName(_fromUtf8("Browse_Button_1"))
    file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
    self.Browse_Button_1.clicked.connect(self, file)
</code></pre>

<p>However every time I run the program I just get this error:</p>

<pre><code>Traceback (most recent call last):
   File "D:\NEA Project\NEA_UI.py", line 194, in &lt;module&gt;
     ui = Ui_Dialog()
   File "D:\NEA Project\NEA_UI.py", line 30, in __init__
     self.setupUi(self)
   File "D:\NEA Project\NEA_UI.py", line 55, in setupUi
     file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
NameError: name 'QFileDialog' is not defined
</code></pre>

<p>Any help with the problem would be greatly appreciated.</p>

<p><code>QFileDialog</code> is in the <a href="http://doc.qt.io/qt-4.8/qfiledialog.html" rel="nofollow">QtGui module</a>, so you need to append that to beginning of your line, e.g.:</p>

<pre><code>file = str(QtGui.QFileDialog.getExistingDirectory(self, "Select Directory"))
</code></pre>

<p>Alternatively, if you want to use <code>QFileDialog</code> without the <code>QtGui</code> in front, you need to import it from the module (at the top of your file), with: </p>

<pre><code>from PyQt4.QtGui import QFileDialog
</code></pre>

<p>Or for Qt5 (note that in Qt5, <code>QFileDialog</code> moved to the <code>QtWidgets</code> module):</p>

<pre><code>from PyQt5.QtWidgets import QFileDialog
</code></pre>

python pyqt qfiledialog