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

Dani AI

Generated

nailed the root cause: self only exists inside instance methods. Your checkurl is a standalone function, so Python has no self to resolve. There is a second issue too: by writing checkurl() in the connect call, you are calling the function immediately during setup, not passing it as a slot. That combination triggers the NameError on first run. is also right that wiring matters; after you instantiate the UI, you need to connect the button to a bound method on that instance.

A minimal, robust pattern in PyQt4 is to move the slot into the class and connect it as a bound method (note: no parentheses when connecting):

# inside setupUi, after creating the button
self.checkurlB.clicked.connect(self.checkurl)

# still inside the class
def checkurl(self):
    from urllib2 import urlopen, URLError
    url = unicode(self.urlT.text())  # PyQt4 on Py2 often returns QString
    try:
        urlopen(url)
    except URLError as e:
        # handle invalid URL / network error
        pass

Since you already call QtCore.QMetaObject.connectSlotsByName(MainWindow), you can also skip the manual connect and use Qt’s auto-connection by naming the method after the objectName and signal:

def on_checkurlB_clicked(self):
    # same body as checkurl()
    pass

Quick checks:

  • Do not write checkurl() in the connect; use self.checkurl.
  • Catch the right exception: urllib2.URLError (or from urllib2 import URLError).
  • In Python 2 + PyQt4, convert QLineEdit.text() to a Python string with unicode(...) (or encode as needed) before passing it to networking code.

This keeps the slot bound to the UI instance and avoids the global self problem.

Recommended Answers

All 4 Replies

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

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.

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

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

i didn't post this post O.o is my account hacked or something !!?? O.o

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.