2

我正在尝试围绕我已经拥有的一些代码构建一个 GUI。我了解如何在手动构建 GUI 时执行此操作,但是在将其添加到由 Qt Designer 和 pyuic 生成的 python 代码时被卡住了。举个例子,我可能需要一个按钮,它允许用户指向一个文件,我手动这样做,这很有效:

import sys
from PyQt4 import QtGui


class Example(QtGui.QWidget):    
    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):        

        btn = QtGui.QPushButton('Open File', self)
        btn.setToolTip('This is a <b>QPushButton</b> widget')
        btn.resize(btn.sizeHint())
        btn.move(50, 50)     
        btn.clicked.connect(self.loadFile)

        self.setGeometry(300, 300, 250, 150)
        self.show()

    def loadFile(self):
        fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
        # some custom code for reading file and storing it

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

但是,当我尝试在 Qt Designer 代码中执行相同操作时,程序会在到达文件对话框之前停止。

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(400, 300)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(130, 100, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))

        self.retranslateUi(Form)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.loadFile)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.pushButton.setText(_translate("Form", "Open File", None))

    def loadFile(self):
        print('loadFile1')
        fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
        print('loadFile2')


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

这只会打印 loadFile() 中的第一条语句,但不会打开文件对话框窗口。我究竟做错了什么?

4

2 回答 2

1

只要可以避免,我真的不喜欢使用 pyuic。您可以尝试一种更简单的方法,它可以减少您的代码。假设您的 UI 文件名为 something.ui,并且您在 QT Designer 中将按钮命名为 somebutton,那么代码将是:

from PyQt4 import QtCore, QtGui, uic
Ui_somewindow, _ = uic.loadUiType("something.ui") #the path to your UI

class SomeWindow(QtGui.QMainWindow, Ui_somewindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_somewindow.__init__(self)
        self.setupUi(self)
        self.somebutton.clicked.connect(self.loadFile)

   def loadFile(self):
        print('loadFile1')
        fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
        print('loadFile2')

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = SomeWindow()
    window.show()
    sys.exit(app.exec_())

请注意,如果您有 QDialog,请将 QMainWindow 替换为 QDialog。希望这可以帮助。

于 2017-06-16T02:32:15.630 回答
1

根据文档

QString getOpenFileName ( QWidget parent = None , QString caption = '', QString directory = '', QString filter = '', Options options = 0)

QString getOpenFileName ( QWidget parent = None , QString caption = '', QString directory = '', QString filter = '', QString selectedFilter = '', Options options = 0)

您需要将小部件或无作为父级传递,在您的情况下,self 不是对象类型。

你必须改变

 QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')

QtGui.QFileDialog.getOpenFileName(None, 'Open file', '/home')
于 2017-01-11T21:51:19.703 回答