参数 1 具有意外类型“Ui_mainWindow" [英] argument 1 has unexpected type 'Ui_mainWindow'

查看:25
本文介绍了参数 1 具有意外类型“Ui_mainWindow"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我在一些人的帮助下编写的小程序制作 GUI,无论如何,我在 PyQt 中制作了 GUI,看起来不错.我添加了一个名为 dirButton 的按钮,上面写着选择目录"

I'm trying to make a GUI for a small program I wrote with the help of some people from here, anyway, I made the GUI in PyQt and it looks fine. I added a button called dirButton that says "Choose Directory"

self.dirButton = QtGui.QPushButton(self.buttonWidget)
self.dirButton.setGeometry(QtCore.QRect(0, 0, 91, 61))
self.dirButton.setObjectName(_fromUtf8("dirButton"))
self.dirButton.clicked.connect(self.browse)

在我点击它时,我让它调用了 self.browse,即:

and in the bottom line there I've made it call self.browse when I click it, which is:

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

但是,这是我得到的错误:

However, this is the error I get:

Traceback (most recent call last):
File "C:\Users\Kevin\Desktop\python-tumblr-0.1\antearaGUI.py", line 88, in browse
filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
TypeError: QFileDialog.getOpenFileName(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'

因此,ui_mainWindow 是我所有 GUI 按钮和 GUI 本身都存储在其中的类.

So, ui_mainWindow is the class that all of my GUI buttons and the GUI itself is stored in.

class Ui_mainWindow(object):

我不明白为什么我会收到错误消息,有人有任何想法吗?

I don't understand why I'm getting an error, does anyone have any ideas?

这是整个 GUI 的 pastebin 链接:http://pastebin.com/BWCcXxUW

Here is a pastebin link to the entire GUI: http://pastebin.com/BWCcXxUW

推荐答案

据我所知,您正在使用从 .ui 文件生成的 Ui_mainWindow.如您所见,Ui_mainWindow 只是包含小部件的 Python 类.getOpenFileName 接收 QWidget 实例作为第一个参数.所以你需要继承 QWidgetQMainWindow 并在该类中定义方法.

As I understand, you are using Ui_mainWindow generated from .ui file. As you can see Ui_mainWindow is just python class which contains widgets. getOpenFileName recieves QWidget instance as first parameter. So you need to subclass QWidget or QMainWindow and define methods in that class.

代码如下所示:

import sys

from PyQt4 import QtCore, QtGui

from file_with_ui import Ui_MainWindow

class Main(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)
    window = Main()
    window.show()
    sys.exit(app.exec_())

或者,您可以将 ui 存储为实例属性:

Alternatively you can store ui as instance attribute:

class Main(QtGui.QMainWindow):
    def __init__(self):
         QtGui.QMainWindow.__init__(self)
         self.ui=Ui_MainWindow()
         self.ui.setupUi(self)

并通过 self.ui 访问您的控件,例如:self.ui.textEdit.setText(data)

And acces your controls through self.ui, e.g.: self.ui.textEdit.setText(data)

考虑阅读有关 pyuic 用法的教程 PyQt by Example (Session1)

Consider reading tutorial about pyuic usage PyQt by Example (Session 1)

这篇关于参数 1 具有意外类型“Ui_mainWindow"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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