【问题标题】:I try to use QFileDialog for choose a file on PyQt but it don't run我尝试使用 QFileDialog 在 PyQt 上选择一个文件,但它不运行
【发布时间】:2014-05-02 07:03:08
【问题描述】:

我是 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

【问题讨论】:

  • 请发帖Vannucci_Gemignani.Ui_MainWindow。通常主窗口的 ui 只包含菜单栏、状态栏等内容,其他元素来自另一个小部件,由 setCentralWidget 设置。
  • 嗨,Hyperboreus。嗨添加MainWindow的代码。谢谢。
  • 请尝试使用明确的父级调用文件选择对话框:QtGui.QFileDialog.getOpenFileName (self)。这就是它在我的盒子上的工作方式。

标签: python pyqt qmainwindow filechooser qfiledialog


【解决方案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)

说明:

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

class Example(QMainWindow):
    ...

    def on_button_1_clicked(self):
        pass

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

【讨论】:

【解决方案2】:

文件对话框没有显示,因为您没有创建 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)
        ...

最后一个建议:避免使用旧式语法连接信号,而改用new-style syntax

    # 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)

【讨论】:

  • 嗨,我更改了我的代码,现在运行!非常感谢,这个应用程序对我和我的大学生涯都非常重要。
猜你喜欢
  • 1970-01-01
  • 2023-02-05
  • 1970-01-01
  • 2018-07-25
  • 2015-08-01
  • 2017-08-20
  • 2019-05-08
  • 2021-09-17
  • 1970-01-01
相关资源
最近更新 更多