954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

python global name 'self' is not defined

hey all I've searched all over the web and i didn't get a solution!
what should i do to take the text from the 'urlT' line edit? is there another way?
Nameerror
"global name 'self' is not defined"
whats wrong with it?

from PyQt4 import QtCore, QtGui

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setWindowModality(QtCore.Qt.NonModal)
        MainWindow.resize(640, 483)
        MainWindow.setMinimumSize(QtCore.QSize(640, 483))
        MainWindow.setMaximumSize(QtCore.QSize(640, 483))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/logo/welcome-logo.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        MainWindow.setTabShape(QtGui.QTabWidget.Triangular)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.urlT = QtGui.QLineEdit(self.centralwidget)
        self.urlT.setEnabled(True)
        self.urlT.setGeometry(QtCore.QRect(10, 20, 521, 27))
        self.urlT.setObjectName("urlT")
        self.checkurlB = QtGui.QPushButton(self.centralwidget)
        self.checkurlB.setGeometry(QtCore.QRect(540, 20, 91, 27))
        self.checkurlB.setObjectName("checkurlB")



        QtCore.QObject.connect(self.checkurlB, QtCore.SIGNAL("clicked()"), checkurl())
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

def checkurl():
    import urllib2

    url=str(self.urlT.text()) #the error line
    try:
        response = urllib2.urlopen(url)
    except URLError,  e:
        #pass or do something
        pass
memomk
Newbie Poster
23 posts since Oct 2011
Reputation Points: 10
Solved Threads: 1
 

The way you wrote it, checkurl() is not a method of Ui_MainWindow class, but a standalone function. Give it a proper indentation.

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 

There is no way to tell without a complete error message which the contains the errant line. Also, do you instantiate the class and then call the setupUI function with a parameter? As the code stands now, nothing will happen even if the code does not contain errors.

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

@woooee : the error line as i said is

url=str(self.urlT.text())

and for sure

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())



:)
@nezachem: then the 'self' defined in the class only!
thanks a lot it worked perfectly :)

memomk
Newbie Poster
23 posts since Oct 2011
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: