5

我是stackoverflow的新手。我在 PyQt 上有一个 MainWindow,我想单击一个按钮并打开一个 QFileDialog 来选择一个文件。问题是:如果我使用 MainWindow,QFileDialog 不会运行。如果我使用 Dialog,QFileDialog 运行。

这是我的 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_())

这是对话框的代码。在这里,我使用 pyuic4 (QTDesigner) 在 .py 生成中编写代码

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

这是 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
4

2 回答 2

5

文件对话框没有显示,因为您没有创建GUI_fsa类的实例。

要解决此问题,请进行以下更改:

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

Browse在您的方法中,您还需要解决另一个问题。Ui_MainWindow需要通过 访问的小部件self.ui。因此,还要进行以下更改:

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

最后一个建议:避免使用旧式语法来连接信号,而使用新式语法

    # 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)
于 2014-03-24T20:49:00.623 回答
1

尝试更改线路:

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

经过

from PyQt4.QtCore import QObject, pyqtSlot

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

并删除该行

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

解释:

使用时QtDesigner无需pyuic4自行连接事件。生成的pyuic4类会为您处理这些问题。你唯一需要做的就是正确地编写你的类方法。例如:如果您有一个button名为“button_1”并且您想在单击时执行一些操作,您只需创建一个这样的方法:

class Example(QMainWindow):
    ...

    def on_button_1_clicked(self):
        pass

这里的关键是on_button_1_clicked代表:作为小部件名称on_<widget-name>_<signal>和作为信号button_1clicked

于 2014-03-24T19:42:39.723 回答