#!/usr/bin/python
#-*-coding:utf-8-*-
#mymoney.py
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
class loginW(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.resize(250,250)
self.setWindowTitle('login')
userLabel=QLabel('username:')
pwdLabel=QLabel('password:')
userle=QLineEdit()
pwdle=QLineEdit()
okButton=QPushButton('OK')
cnlButton=QPushButton('Cancel')
grid = QGridLayout()
grid.addWidget(userLabel,0,1)
grid.addWidget(userle,0,2)
grid.addWidget(pwdLabel,1,1)
grid.addWidget(pwdle,1,2)
grid.addWidget(okButton, 2, 1)
grid.addWidget(cnlButton, 2, 2)
self.setLayout(grid)
#okButton.clicked.connect(self.okcheck)
self.connect(okButton,SIGNAL("clicked()"),self,self.okcheck)
self.connect(cnlButton,SIGNAL("clicked()"),self,SLOT("close()"))
def okcheck(self):
print 'a'
#class MainWindow(QMainWindow):
"""docstring for MainWindow"""
def __init__(self):
QMainWindow.__init__(self)
self.resize(250, 150)
self.setWindowTitle('mymoney')
self.statusBar().showMessage('Ready')
app=QApplication(sys.argv)
#main=MainWindow()
main=loginW()
main.show()
app.exec_()
初学PYQT,在运行以上代码时,报错:
Traceback (most recent call last):
File "H:\pyqt\mymoney.py", line 54, in <module>
main=loginW()
File "H:\pyqt\mymoney.py", line 35, in __init__
self.connect(okButton,SIGNAL("clicked()"),self,self.okcheck)
AttributeError: 'loginW' object has no attribute 'okcheck'
[Finished in 1.0s with exit code 1]
求指导
------解决方案--------------------
self.connect(okButton,SIGNAL("clicked()"),self.okcheck)
中间多了一个self
楼主要养成结贴的好习惯