Question

I'm new on stackoverflow. I have a MainWindow on PyQt, I want click a button and open a QFileDialog for choose a file. The problem is: If I use a MainWindow, QFileDialog don't run. If I use a Dialog, QFileDialog run.

This is my code for the MainWindow.

import sys
from Import_fsa import import_fsa
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QFileDialog
from Vannucci_Gemignani import Ui_MainWindow


class GUI_fsa(QtGui.QMainWindow):
   def __init__(self):

       QtGui.QMainWindow.__init__(self)
       self.ui=Ui_MainWindow()
       self.ui.setupUi(self)

       QtCore.QObject.connect(self.ui.Button_Browse, QtCore.SIGNAL('clicked()'), self.Browse)

   def Browse(self):

       fname=QFileDialog.getOpenFileName()


        self.lineEdit.setText(fname)
        data_set=import_fsa(fname)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

This is the code for Dialog. Here I write the code in the .py generate using pyuic4 (QTDesigner)

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QFileDialog
from ab1 import ABIFReader
import pylab

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(508, 363)
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(10, 40, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.lineEdit = QtGui.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(110, 40, 361, 20))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL('clicked()'), self.selectFile)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None,       QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("Dialog", "PushButton", None, QtGui.QApplication.UnicodeUTF8))


    def selectFile(self):
        fname=QFileDialog.getOpenFileName()
        self.lineEdit.setText(fname)
        reader=ABIFReader(fname)
        dati=reader.getData('DATA',1)
        pylab.plot(dati)







if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

This is Vannucci_Gemignani.py:

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(1445, 744)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.gridLayoutWidget = QtGui.QWidget(self.centralwidget)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 10, 771, 83))
        self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
        self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setMargin(0)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.Button_Browse = QtGui.QPushButton(self.gridLayoutWidget)
        self.Button_Browse.setObjectName(_fromUtf8("Button_Browse"))
        self.gridLayout.addWidget(self.Button_Browse, 0, 0, 1, 1)
        self.Button_Plot = QtGui.QPushButton(self.gridLayoutWidget)
        self.Button_Plot.setObjectName(_fromUtf8("Button_Plot"))
        self.gridLayout.addWidget(self.Button_Plot, 1, 0, 1, 1)
        self.lineEdit = QtGui.QLineEdit(self.gridLayoutWidget)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
        self.Button_Clear = QtGui.QPushButton(self.gridLayoutWidget)
        self.Button_Clear.setObjectName(_fromUtf8("Button_Clear"))
        self.gridLayout.addWidget(self.Button_Clear, 2, 0, 1, 1)
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(20, 130, 61, 31))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(700, 130, 61, 31))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.label_3 = QtGui.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(20, 430, 61, 31))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.label_4 = QtGui.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(700, 430, 61, 31))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.widget = matplotlibWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(90, 130, 571, 251))
        self.widget.setObjectName(_fromUtf8("widget"))
        self.widget_2 = matplotlibWidget2(self.centralwidget)
        self.widget_2.setGeometry(QtCore.QRect(770, 130, 571, 251))
        self.widget_2.setObjectName(_fromUtf8("widget_2"))
        self.widget_3 = matplotlibWidget3(self.centralwidget)
        self.widget_3.setGeometry(QtCore.QRect(90, 430, 571, 251))
        self.widget_3.setObjectName(_fromUtf8("widget_3"))
        self.widget_4 = matplotlibWidget4(self.centralwidget)
        self.widget_4.setGeometry(QtCore.QRect(770, 430, 571, 251))
        self.widget_4.setObjectName(_fromUtf8("widget_4"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1445, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        self.menuFile = QtGui.QMenu(self.menubar)
        self.menuFile.setObjectName(_fromUtf8("menuFile"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)
        self.menubar.addAction(self.menuFile.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "fsa Plotter", None, QtGui.QApplication.UnicodeUTF8))
        self.Button_Browse.setText(QtGui.QApplication.translate("MainWindow", "Browse", None, QtGui.QApplication.UnicodeUTF8))
        self.Button_Plot.setText(QtGui.QApplication.translate("MainWindow", "Plot", None, QtGui.QApplication.UnicodeUTF8))
        self.Button_Clear.setText(QtGui.QApplication.translate("MainWindow", "Clear", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("MainWindow", "Channel 1", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("MainWindow", "Channel 2", None, QtGui.QApplication.UnicodeUTF8))
        self.label_3.setText(QtGui.QApplication.translate("MainWindow", "Channel 3", None, QtGui.QApplication.UnicodeUTF8))
        self.label_4.setText(QtGui.QApplication.translate("MainWindow", "Channel 4", None, QtGui.QApplication.UnicodeUTF8))
        self.menuFile.setTitle(QtGui.QApplication.translate("MainWindow", "File", None, QtGui.QApplication.UnicodeUTF8))

from matplotlibwidget import matplotlibWidget
from matplotlibwidget3 import matplotlibWidget3
from matplotlibwidget2 import matplotlibWidget2
from matplotlibwidget4 import matplotlibWidget4
Was it helpful?

Solution

The file-dialog doesn't show, because you did not create an instance of your GUI_fsa class.

To fix that, make the following changes:

if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    MainWindow = GUI_fsa()
    # the next two lines aren't needed
    # ui = Ui_MainWindow()
    # ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

There is another problem you will also need to fix, in your Browse method. The widgets from Ui_MainWindow need to accessed via self.ui. So also make the following changes:

    def Browse(self):
        fname = QFileDialog.getOpenFileName()
        self.ui.lineEdit.setText(fname)
        ...

One final suggestion: avoid using the old-style syntax for connecting signals, and use the new-style syntax instead:

    # don't do this:
    # QtCore.QObject.connect(self.ui.Button_Browse, QtCore.SIGNAL('clicked()'), self.Browse)
    # do this!
    self.ui.Button_Browse.clicked.connect(self.Browse)

OTHER TIPS

try changing the lines:

def Browse(self):
    fname=QFileDialog.getOpenFileName()

by

from PyQt4.QtCore import QObject, pyqtSlot

@pyqtSlot()
def on_Button_Browse_clicked(self):
    fname=QFileDialog.getOpenFileName()

and remove the line

QtCore.QObject.connect(self.ui.Button_Browse, QtCore.SIGNAL('clicked()'), self.Browse)

Explanation:

When you're using QtDesigner and pyuic4 you don't need to connect the events by your self. The pyuic4 generated class take care of that for you. The only you have to do is write your class methods correctly. For example: IF you have a button called "button_1" and you want perform some action when clicked, you just create a method like this:

class Example(QMainWindow):
    ...

    def on_button_1_clicked(self):
        pass

the key here is on_button_1_clicked which stands for: on_<widget-name>_<signal> with button_1 as widget name and clicked as signal

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top