2014-03-24 79 views
4

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

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

这是Dialog的代码。在这里,我写在的.py代码生成使用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_()) 

这是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 
+0

请张贴'Vannucci_Gemignani.Ui_MainWindow'。通常,主窗口的用户界面只包含菜单栏,状态栏等内容,而其他元素则位于另一个由'setCentralWidget'设置的窗口小部件中。 – Hyperboreus

+0

嗨Hyperboreus。喂添加MainWindow的代码。谢谢。 – Gianluca

+0

请尝试使用明确的父级调用文件选择对话框:'QtGui.QFileDialog.getOpenFileName(self)'。这是它在我的盒子上的工作原理。 – Hyperboreus

回答

4

的文件对话框不显示,因为你没有创建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) 
+0

嗨,我改变我的代码,现在运行!非常感谢你,这个应用程序对我和我的大学生涯非常重要。 – Gianluca

1

尝试改变线路:由

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

from PyQt4.QtCore import QObject, pyqtSlot 

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

并除去th e行

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

说明:

当你使用QtDesignerpyuic4你不需要通过你的自我的事件连接。生成的课程将为你照顾。你唯一需要做的是正确编写你的类方法。例如:如果你有一个button称为“button_1”,你想点击时执行一些动作,你只需要创建一个这样的方法:

class Example(QMainWindow): 
    ... 

    def on_button_1_clicked(self): 
     pass 

这里的关键是on_button_1_clicked它代表:与button_1on_<widget-name>_<signal>部件名称clicked信号

+0

当我尝试您所建议的内容时,我选择文件(第一次)后再次打开文件对话窗口,然后再次打开。 –

+0

是的,你必须使用pyqtSlot才能避免这种情况。我编辑了我的答案。检查此:http://pyqt.sourceforge.net/Docs/PyQt4/new_style_signals_slots.html –

相关问题