简体   繁体   English

我尝试使用QFileDialog在PyQt上选择一个文件,但它不运行

[英]I try to use QFileDialog for choose a file on PyQt but it don't run

I'm new on stackoverflow. 我是stackoverflow的新手。 I have a MainWindow on PyQt, I want click a button and open a QFileDialog for choose a file. 我在PyQt上有一个MainWindow,我想点击一个按钮并打开一个QFileDialog来选择一个文件。 The problem is: If I use a MainWindow, QFileDialog don't run. 问题是:如果我使用MainWindow,QFileDialog不会运行。 If I use a Dialog, QFileDialog run. 如果我使用Dialog,QFileDialog运行。

This is my code for the MainWindow. 这是我的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. 这是Dialog的代码。 Here I write the code in the .py generate using pyuic4 (QTDesigner) 在这里,我使用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_())

This is Vannucci_Gemignani.py: 这是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

The file-dialog doesn't show, because you did not create an instance of your GUI_fsa class. 文件对话框未显示,因为您没有创建GUI_fsa类的实例。

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. 您还需要在Browse方法中修复另一个问题。 The widgets from Ui_MainWindow need to accessed via self.ui . 来自Ui_MainWindow的小部件需要通过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)

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. 当您使用QtDesignerpyuic4您不需要自己连接事件。 The pyuic4 generated class take care of that for you. pyuic4生成的类为您处理。 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: 例如:如果您有一个名为“button_1”的button并且您希望在单击时执行某些操作,则只需创建如下方法:

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 这里的关键是on_button_1_clicked ,它代表: on_<widget-name>_<signal>button_1作为小部件名称clicked信号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM