PyQt QtGui.QFileDialog 不起作用? [英] PyQt QtGui.QFileDialog not Working?

查看:49
本文介绍了PyQt QtGui.QFileDialog 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Python2.7 中使用 Pyqt4 构建用户界面,但是...当我单击保存"按钮时,总是出现 TypeError,即

I am Buliding an user interface using Pyqt4 in Python2.7 but...When I clicked Save button there is always a TypeError i.e

类型错误:QFileDialog.getSaveFileName(QWidget parent=None, QStringcaption=QString(), QString 目录=QString(), QStringfilter=QString(), QString selectedFilter=None, QFileDialog.Optionsoptions=0): 参数 1 具有意外类型 'Ui_MainWindow'

TypeError: QFileDialog.getSaveFileName(QWidget parent=None, QString caption=QString(), QString directory=QString(), QString filter=QString(), QString selectedFilter=None, QFileDialog.Options options=0): argument 1 has unexpected type 'Ui_MainWindow'

我的代码如下:

class Ui_MainWindow(object):

def setupUi(self, MainWindow):
    MainWindow.setObjectName(_fromUtf8("MainWindow"))
    MainWindow.resize(640, 400)
    #code-skipped



def save(self):
    filename = QtGui.QFileDialog.getSaveFileName(self, 'Save File', os.getenv('HOME'))
    f = open(filename, 'w')
    filedata = self.textEdit.toPlainText()
    f.write(filedata)
    f.close()

def retranslateUi(self, MainWindow):
   #code-Skipped

class FileDialog(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
    QtGui.QMainWindow.__init__(self)
    self.setupUi(self)

def browse(self):
    filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
    fname = open(filename)
    data = fname.read()
    self.textEdit.setText(data)
    fname.close()

if __name__ == '__main__' :
app = QtGui.QApplication(sys.argv)

mainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(mainWindow)
mainWindow.show()
sys.exit(app.exec_())

请帮帮我,代码片段被删除...

plz help me,Code snippets is aphericiated...

推荐答案

UI_Mainwindow 不是 QtGui.QWidget 的实例.使用

UI_Mainwindow is not an instance of QtGui.QWidget. Use

 filename = QtGui.QFileDialog.getSaveFileName(None, 'Save File', os.getenv('HOME'))

def setupUi(self, MainWindow):
    self.window = MainWindow
    MainWindow.setObjectName(_fromUtf8("MainWindow"))
    MainWindow.resize(640, 400)
    #code-skipped

def save(self):
    filename = QtGui.QFileDialog.getSaveFileName(self.window, 'Save File', os.getenv('HOME'))

将 QWidget 实例作为 QFileDialog 的父级传递.

to pass QWidget instance as QFileDialog's parent.

这篇关于PyQt QtGui.QFileDialog 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆