AttributeError: 'bool' 对象没有属性 'le' -- pyqt -- 将文本放入 python 变量中 [英] AttributeError: 'bool' object has no attribute 'le' -- pyqt -- getting text into python variable

查看:87
本文介绍了AttributeError: 'bool' 对象没有属性 'le' -- pyqt -- 将文本放入 python 变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Qt Designer 创建了一个 UI,它有一个简单的行输入和一个按钮.我尝试在 python 变量中获取行编辑的输入,但它抛出了一个错误.这是我的python代码:

I have created a UI using Qt Designer which has a simple line input and a push button. I tried getting the input of line edit in a python variable but it is throwing me an error. This is my python code:

from PyQt4 import QtGui
import sys
import prog
import MySQLdb

class ExampleApp(QtGui.QMainWindow, prog.Ui_Program):
    def __init__(self, parent=None):
        super(ExampleApp, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(functioni)

def functioni (self):
    db = MySQLdb.connect(host="localhost", 
                     user="root",       
                     passwd="*****",    
                     db="testpy")   

    cur = db.cursor()

    cur.execute("INSERT INTO Name (Name) VALUES (?)", self.le.text()) #self.le.text() is giving me trouble...

    db.commit()
    cur.close()
    db.close()


def main():
    app = QtGui.QApplication(sys.argv)
    form = ExampleApp()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

我的用户界面代码是这样的:

My ui code has this:

self.le = QtGui.QLineEdit(self.centralwidget)
self.le.setObjectName(_fromUtf8("le"))

当我运行python程序时,出现这个错误:

When I run the python program, I get this error:

Traceback (most recent call last):
  File "main.py", line 20, in functioni
  cur.execute("INSERT INTO Name (Name) VALUES (?)", self.le.displayText())
AttributeError: 'bool' object has no attribute 'le'

这个问题以前可能有人问过,但我已经尝试了所有方法,但它不起作用!我无法弄清楚这里的 bool 是什么.解决方案可能很简单,但如果有人能指出我的错误,我将不胜感激.谢谢.

This question might have been asked before but I've tried everything and it won't work! I can't figure what is bool here. The solution might be trivial but I'd appreciate it if someone could point my error. Thank you.

这是我的完整用户界面代码:

Here is my full ui code:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'prog.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

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_Program(object):
    def setupUi(self, Program):
        Program.setObjectName(_fromUtf8("Program"))
        Program.resize(351, 138)
        Program.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.centralwidget = QtGui.QWidget(Program)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.formLayout = QtGui.QFormLayout(self.centralwidget)
        self.formLayout.setObjectName(_fromUtf8("formLayout"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setObjectName(_fromUtf8("label"))
        self.formLayout.setWidget(0, QtGui.QFormLayout.LabelRole, self.label)
        self.le = QtGui.QLineEdit(self.centralwidget)
        self.le.setObjectName(_fromUtf8("le"))
        self.formLayout.setWidget(0, QtGui.QFormLayout.FieldRole, self.le)
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.formLayout.setWidget(1, QtGui.QFormLayout.FieldRole, self.pushButton)
        Program.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(Program)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 351, 25))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        self.menuSubmit = QtGui.QMenu(self.menubar)
        self.menuSubmit.setObjectName(_fromUtf8("menuSubmit"))
        Program.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(Program)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        Program.setStatusBar(self.statusbar)
        self.toolBar = QtGui.QToolBar(Program)
        self.toolBar.setObjectName(_fromUtf8("toolBar"))
        Program.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
        self.actionSubmit = QtGui.QAction(Program)
        self.actionSubmit.setObjectName(_fromUtf8("actionSubmit"))
        self.menuSubmit.addAction(self.actionSubmit)
        self.menubar.addAction(self.menuSubmit.menuAction())

        self.retranslateUi(Program)
        QtCore.QMetaObject.connectSlotsByName(Program)

    def retranslateUi(self, Program):
        Program.setWindowTitle(_translate("Program", "Program", None))
        self.label.setText(_translate("Program", "Name", None))
        self.pushButton.setText(_translate("Program", "Submit", None))
        self.menuSubmit.setTitle(_translate("Program", "Submit", None))
        self.toolBar.setWindowTitle(_translate("Program", "toolBar", None))
        self.actionSubmit.setText(_translate("Program", "Submit", None))

推荐答案

您误用了self"参数,因为函数本身应该在类缩进中(因此,它应该是类的方法)或接收实例作为论据.

You are misusing the "self" argument, as the function itself should be in the class indentation (thus, it should be a class's method) or receive the instance as argument.

在您的代码中,functioni 将只接收信号参数(所有按钮,包括 QPushButtons,都有一个选中的参数).这意味着您正在调用 functioniFalse 作为参数(点击后 QPushButton 的选中状态),而 self 实际上将是一个布尔变量.记住 self 只是 Python 中的一个约定,它实际上是一个普通的位置参数,你可以随意调用它.

In your code, functioni will only receive the signal argument (all buttons, including QPushButtons, have a checked argument). This means that you are calling functioni with False as argument (QPushButton's checked state after clicking), and self will actually be a bool variable. Remember that self is just a convention in python, it actually is a normal positional argument, you could call it as you want.

要解决您的问题,只需添加正确的缩进(并在信号连接中调用 self.functioni),就足以使 functioni 函数成为类方法.如果出于任何原因,您需要将函数留在类之外,您可以添加文本或类实例作为参数:

To solve your problem, it's enough to make the functioni function a class method by adding the right indentation (and calling self.functioni in the signal connection). If, for any reason, you need to leave the function outside the class, you could either add the text or the class instance as an argument:

    self.pushButton.clicked.connect(lambda checked: functioni(self.le.text())
    self.pushButton.clicked.connect(lambda checked: functioni(self))


不相关,但很重要.请记住,sqlite 的第二个 execute 参数必须是可迭代的(元组、列表等).如果只有一个参数,可以通过在括号前加逗号得到单值元组:

Unrelated, but important. Remember that the second execute argument for sqlite has to be an iterable (tuple, list, etc.). If you only have one parameter, a single value tuple can be obtained by adding a comma before closing the parenthesis:

cur.execute("INSERT INTO Name (Name) VALUES (?)", (self.le.text(), ))

这篇关于AttributeError: 'bool' 对象没有属性 'le' -- pyqt -- 将文本放入 python 变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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