参数1有一个出乎意料的类型'Ui_mainWindow'

时间:2022-09-07 23:01:05

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"

我试着为我写的一个小程序做一个GUI,在一些人的帮助下,我在PyQt做了GUI,看起来很好。我添加了一个按钮叫做dirButton,它表示"选择目录"

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)

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

在底线中,我把它叫做self。当我点击它时,浏览:

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'

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

ui_mainWindow是所有我的GUI按钮和GUI本身的类。

class Ui_mainWindow(object):

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

我不明白我为什么会犯错误,有人有什么想法吗?

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

这里是一个到整个GUI的pastebin链接:http://pastebin.com/BWCcXxUW。

2 个解决方案

#1


7  

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.

据我所知,您使用的是由.ui文件生成的Ui_mainWindow。您可以看到Ui_mainWindow是一个包含小部件的python类。getOpenFileName接收QWidget实例作为第一个参数。因此,您需要子类化QWidget或QMainWindow,并在该类中定义方法。

Code will look like this:

代码如下所示:

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_())

Alternatively you can store ui as instance attribute:

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

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

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

并通过自我控制你的控制。用户界面,例如:self.ui.textEdit.setText(数据)

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

考虑阅读关于pyuic使用PyQt的教程(第1节)

#2


1  

import the following:

导入:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtGui, QtCore

In class Ui_MainWindow(object), replace object with QWidget:

在类Ui_MainWindow(对象)中,用QWidget替换对象:

Ui_MainWindow(QWidget)

#1


7  

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.

据我所知,您使用的是由.ui文件生成的Ui_mainWindow。您可以看到Ui_mainWindow是一个包含小部件的python类。getOpenFileName接收QWidget实例作为第一个参数。因此,您需要子类化QWidget或QMainWindow,并在该类中定义方法。

Code will look like this:

代码如下所示:

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_())

Alternatively you can store ui as instance attribute:

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

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

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

并通过自我控制你的控制。用户界面,例如:self.ui.textEdit.setText(数据)

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

考虑阅读关于pyuic使用PyQt的教程(第1节)

#2


1  

import the following:

导入:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtGui, QtCore

In class Ui_MainWindow(object), replace object with QWidget:

在类Ui_MainWindow(对象)中,用QWidget替换对象:

Ui_MainWindow(QWidget)

相关文章