属性错误:“我的窗口”对象没有属性“txtFirstName”;
我正在尝试制作一个简单的 PyQT4 应用程序,它可以让我在单个消息框中显示两个文本框中的文本。它非常简单,所以我确信我错过了一些非常小的东西。
感谢您的帮助。
import sys
from PyQt4 import QtGui, QtCore
class myWindow(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
#The setGeometry method is used to position the control.
#Order: X, Y position - Width, Height of control.
self.resize(500,350)
self.center()
self.setWindowTitle("Sergio's QT Application.")
self.setWindowIcon(QtGui.QIcon('menuScreenFolderShadow.png'))
self.setToolTip('<i>Welcome</i> to the <b>first</b> app ever!')
QtGui.QToolTip.setFont(QtGui.QFont('Helvetica', 12))
txtFirstName = QtGui.QLineEdit('', self)
txtFirstName.setGeometry(35, 35, 150, 20)
txtLastName = QtGui.QLineEdit('', self)
txtLastName.setGeometry(35, 60, 150, 20)
btnSubmit = QtGui.QPushButton('Say hello.', self)
btnSubmit.setGeometry(340, 250, 150, 35)
self.connect(btnSubmit, QtCore.SIGNAL("clicked()"), self.clicked)
btnQuit = QtGui.QPushButton('Exit Application', self)
btnQuit.setGeometry(340, 300, 150, 35)
self.connect(btnQuit, QtCore.SIGNAL('clicked()'),
QtGui.qApp, QtCore.SLOT('quit()'))
def clicked(self):
QtGui.QMessageBox.about(self, "Just dropped by to say hi!", "Welcome to this tutorial %s %s!" % (
self.txtFirstName.text(), self.txtLastName.text()))
def center(self):
screen = QtGui.QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)
app = QtGui.QApplication(sys.argv)
mainForm = myWindow()
mainForm.show()
sys.exit(app.exec_())
这是我收到的错误消息:
回溯(最近一次调用最后一次):
文件 “C:\ Users \ Sergio.Tapia \ Documents \ NetBeansProjects \ PyQTTests \ src \ pyqttests.py”, 第 36 行,点击 self.txtFirstName.text(), self.txtLastName.text())) 属性错误:“myWindow”对象有 没有属性“txtFirstName”
I'm trying to make a simple PyQT4 application that will let me show the text from two textboxes in a single message box. It's pretty straight forward, so I'm sure I'm missing something really tiny.
Thanks for your help.
import sys
from PyQt4 import QtGui, QtCore
class myWindow(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
#The setGeometry method is used to position the control.
#Order: X, Y position - Width, Height of control.
self.resize(500,350)
self.center()
self.setWindowTitle("Sergio's QT Application.")
self.setWindowIcon(QtGui.QIcon('menuScreenFolderShadow.png'))
self.setToolTip('<i>Welcome</i> to the <b>first</b> app ever!')
QtGui.QToolTip.setFont(QtGui.QFont('Helvetica', 12))
txtFirstName = QtGui.QLineEdit('', self)
txtFirstName.setGeometry(35, 35, 150, 20)
txtLastName = QtGui.QLineEdit('', self)
txtLastName.setGeometry(35, 60, 150, 20)
btnSubmit = QtGui.QPushButton('Say hello.', self)
btnSubmit.setGeometry(340, 250, 150, 35)
self.connect(btnSubmit, QtCore.SIGNAL("clicked()"), self.clicked)
btnQuit = QtGui.QPushButton('Exit Application', self)
btnQuit.setGeometry(340, 300, 150, 35)
self.connect(btnQuit, QtCore.SIGNAL('clicked()'),
QtGui.qApp, QtCore.SLOT('quit()'))
def clicked(self):
QtGui.QMessageBox.about(self, "Just dropped by to say hi!", "Welcome to this tutorial %s %s!" % (
self.txtFirstName.text(), self.txtLastName.text()))
def center(self):
screen = QtGui.QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)
app = QtGui.QApplication(sys.argv)
mainForm = myWindow()
mainForm.show()
sys.exit(app.exec_())
Here's the error message I receive:
Traceback (most recent call last):
File
"C:\Users\Sergio.Tapia\Documents\NetBeansProjects\PyQTTests\src\pyqttests.py",
line 36, in clicked
self.txtFirstName.text(), self.txtLastName.text()))
AttributeError: 'myWindow' object has
no attribute 'txtFirstName'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在创建
txtLastName
的__init__
中。它不是作为类成员创建的,而是作为 __init__ 方法内的局部变量创建的。要使其成为稍后可以引用的类成员,请使用self.
:The problem is in
__init__
, wheretxtLastName
is created. It's not created as a class member, but rather as a local variable inside the__init__
method. To make it a class member you can later refer to, useself.
: