python - I try to use QFileDialog for choose a file on PyQt but it don't run -


i'm new on stackoverflow. have mainwindow on pyqt, want click button , open qfiledialog choose file. problem is: if use mainwindow, qfiledialog don't run. if use dialog, qfiledialog run.

this code mainwindow.

import sys import_fsa import import_fsa pyqt4 import qtcore, qtgui pyqt4.qtgui import qfiledialog 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 code dialog. here write code in .py generate using pyuic4 (qtdesigner)

from pyqt4 import qtcore, qtgui pyqt4.qtgui import qfiledialog 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 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))  matplotlibwidget import matplotlibwidget matplotlibwidget3 import matplotlibwidget3 matplotlibwidget2 import matplotlibwidget2 matplotlibwidget4 import matplotlibwidget4 

the file-dialog doesn't show, because did not create instance of gui_fsa class.

to fix that, make following changes:

if __name__ == "__main__":      app = qtgui.qapplication(sys.argv)     mainwindow = gui_fsa()     # next 2 lines aren't needed     # ui = ui_mainwindow()     # ui.setupui(mainwindow)     mainwindow.show()     sys.exit(app.exec_()) 

there problem need fix, in browse method. widgets ui_mainwindow need accessed via self.ui. make following changes:

    def browse(self):         fname = qfiledialog.getopenfilename()         self.ui.lineedit.settext(fname)         ... 

one final suggestion: avoid using old-style syntax connecting signals, , use new-style syntax instead:

    # don't this:     # qtcore.qobject.connect(self.ui.button_browse, qtcore.signal('clicked()'), self.browse)     # this!     self.ui.button_browse.clicked.connect(self.browse) 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -