title: NameError: name 'x' is not defined
body: <p>When I run my code (and press the Enable button on the GUI i made) I get the </p>
  
  <pre><code>NameError: name 'stu' is not defined
  </code></pre>
  
  <p>error. I'm pulling my hair out trying the figure out why this is happening, but I can't, so I decided to post it on Stackoverflow ;-). </p>
  
  <p>I'm guessing that it has to do somthing with global and local variables but no matter where I put <code>global</code> it still fails to run.</p>
  
  <p>Windows 10 Pro 64bit Python 3.4 (Annaconda3 distribution).</p>
  
  <p>I hope you can help and if you decide to, please provide some kind of explenation or source so I don't keep runnng into this stupid problem again.</p>
  
  <p>Thanks!</p>
  
  <p>Full code below:</p>
  
  <pre><code># -*- coding: utf-8 -*-
  
  from PyQt4 import QtCore, QtGui
  from os import getcwd, remove, system, environ
  from os.path import isfile
  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 MyPopup(QtGui.QDialog):
  
      def __init__(self):
          QtGui.QDialog.__init__(self)
          self.setWindowTitle(_translate("MyPopup", "About", None))
          icon = QtGui.QIcon()
          icon.addPixmap(
              QtGui.QPixmap(
                  _fromUtf8("./about.png")),
              QtGui.QIcon.Normal,
              QtGui.QIcon.Off)
          self.setWindowIcon(icon)
          resolution = QtGui.QDesktopWidget().screenGeometry()
          self.move((resolution.width() / 2) - (self.frameSize().width() / 2),
                    (resolution.height() / 2) - (self.frameSize().height() / 2))
          pic = QtGui.QLabel(self)
          pic.setGeometry(20, 20, 128, 128)
          pic.setPixmap(QtGui.QPixmap(getcwd() + "/icon.gif"))
          self.setMinimumSize(QtCore.QSize(420, 300))
          self.setMaximumSize(QtCore.QSize(420, 300))
          labele = QtGui.QLabel(self)
          labele.setGeometry(QtCore.QRect(158, 20, 2000, 50))
          font = QtGui.QFont()
          font.setPointSize(26)
          labele.setText(_translate("MyPopup", "NoSleep", None))
          labele.setFont(font)
          # self.setWindowFlags(Qt.FramelessWindowHint)
          labelel = QtGui.QLabel(self)
          labelel.setGeometry(QtCore.QRect(158, 70, 2000, 25))
          font = QtGui.QFont()
          font.setPointSize(16)
          labelel.setText(_translate("MyPopup", "Jan Murić", None))
          labelel.setFont(font)
  
          labelo = QtGui.QLabel(self)
          labelo.setGeometry(QtCore.QRect(100, 175, 300, 100))
          font = QtGui.QFont()
          font.setPointSize(10)
          labelo.setText(
              _translate(
                  "MyPopup",
                  "FREEWARE\n\nVersion:   1.0\nE-Mail:        janek.muric@gmail.com",
                  None))
          labelo.setFont(font)
  
          exitButton = QtGui.QPushButton(self)
          exitButton.setGeometry(QtCore.QRect(347, 276, 71, 23))
          exitButton.setText(_translate("MyPopup", "Ok", None))
          exitButton.clicked.connect(self.quit)
  
      def quit(self):
          self.close()        
  
  
  class Ui_MainWindow(object):
      user = environ['USERNAME']
      if isfile("C:/Users/"+ user +"/setting.txt") == True:
  
          f = open("C:/Users/"+ user +"/setting.txt", 'r')
          stu = f.read()
          print(stu) #Print confirms that the variable is sucessfully set
          f.close()
      else:
  
          f = open("C:/Users/"+ user +"/setting.txt", 'w')
          f.truncate()
          f.write("0")
          f.close()
          stu = 0
          print(stu) #Print confirms that the variable is sucessfully set
  
  
      def setupUi(self, MainWindow):
          MainWindow.setObjectName(_fromUtf8("MainWindow"))
          MainWindow.resize(363, 199)
          self.centralwidget = QtGui.QWidget(MainWindow)
          self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
          self.about = QtGui.QPushButton(self.centralwidget)
          self.about.setGeometry(QtCore.QRect(330, 0, 31, 31))
          self.about.setObjectName(_fromUtf8("about"))
          self.groupBox = QtGui.QGroupBox(self.centralwidget)
          self.groupBox.setGeometry(QtCore.QRect(10, 80, 131, 111))
          self.groupBox.setObjectName(_fromUtf8("groupBox"))
          self.chrome = QtGui.QCheckBox(self.groupBox)
          self.chrome.setGeometry(QtCore.QRect(20, 20, 91, 17))
          self.chrome.setChecked(True)
          self.chrome.setObjectName(_fromUtf8("chrome"))
          self.firefox_2 = QtGui.QCheckBox(self.groupBox)
          self.firefox_2.setGeometry(QtCore.QRect(20, 50, 91, 17))
          self.firefox_2.setChecked(True)
          self.firefox_2.setObjectName(_fromUtf8("firefox_2"))
          self.ie = QtGui.QCheckBox(self.groupBox)
          self.ie.setGeometry(QtCore.QRect(20, 80, 101, 17))
          self.ie.setChecked(True)
          self.ie.setObjectName(_fromUtf8("ie"))
          self.enable = QtGui.QPushButton(self.centralwidget)
          self.enable.setGeometry(QtCore.QRect(280, 170, 75, 23))
          self.enable.setObjectName(_fromUtf8("enable"))
          self.startup = QtGui.QCheckBox(self.centralwidget)
          self.startup.setGeometry(QtCore.QRect(160, 170, 101, 17))
          self.startup.setChecked(True)
          self.startup.setObjectName(_fromUtf8("startup"))
          self.display = QtGui.QCheckBox(self.centralwidget)
          self.display.setGeometry(QtCore.QRect(160, 140, 131, 17))
          self.display.setObjectName(_fromUtf8("display"))
          self.label = QtGui.QLabel(self.centralwidget)
          self.label_2 = QtGui.QLabel(self.centralwidget)
          self.label_3 = QtGui.QLabel(self.centralwidget)
          self.label.setGeometry(QtCore.QRect(10, 0, 271, 71))
          self.label_2.setGeometry(QtCore.QRect(160, 80, 61, 41))
          self.label_3.setGeometry(QtCore.QRect(225, 80, 131, 41))
          self.label.setObjectName(_fromUtf8("label"))
          self.groupBox.raise_()
          self.about.raise_()
          self.enable.raise_()
          self.startup.raise_()
          self.display.raise_()
          self.label.raise_()
          self.icon = QtGui.QIcon()
          self.icon.addPixmap(QtGui.QPixmap(_fromUtf8("./icon.gif")),QtGui.QIcon.Normal,QtGui.QIcon.Off)
          app.setWindowIcon(self.icon)
          self.about.clicked.connect(self.openAbout)
          self.enable.clicked.connect(self.activate)
          MainWindow.setCentralWidget(self.centralwidget)
          self.retranslateUi(MainWindow)
          QtCore.QMetaObject.connectSlotsByName(MainWindow)
  
      def retranslateUi(self, MainWindow):
          MainWindow.setWindowTitle(_translate("MainWindow", "NoSleep", None))
          self.about.setText(_translate("MainWindow", "?", None))
          self.groupBox.setTitle(_translate("MainWindow", "Browsers", None))
          self.chrome.setText(_translate("MainWindow", "Google Chrome", None))
          self.firefox_2.setText(_translate("MainWindow", "Mozilla Firefox", None))
          self.ie.setText(_translate("MainWindow", "Internet Explorer", None))
          self.enable.setText(_translate("MainWindow", "Enable", None))
          self.startup.setText(_translate("MainWindow", "Run on startup", None))
          self.display.setText(_translate("MainWindow", "Disable display timeout ", None))
          self.label_2.setText(_translate("MainWindow", "Status:", None))
          self.label_3.setText(_translate("MainWindow", "Incative", None))
          font = QtGui.QFont()
          font.setPointSize(15)
          palette = QtGui.QPalette()
          palette.setColor(QtGui.QPalette.Foreground, QtCore.Qt.darkRed)
          self.label_3.setPalette(palette)
          self.label_2.setFont(font)
          self.label_3.setFont(font)
          self.label.setText(_translate("MainWindow", "If the program is enabled auto sleep will be disabled\n"
          "if a download is in progess in selected browsers.\n"
          "\n"
          "This program is compatible with Chrome, Firefox and IE.", None))
  
      def openAbout(self):
          self.w = MyPopup()
          self.w.exec_()
  
  
      def activate(self):
          global user
          global stu
  
          if stu == 0:
  
              #Setting Visuals
              stu = 1
              f = open("C:/Users/"+ user +"/setting.txt", 'w')
              f.truncate()
              f.write("1")
              f.close()
              self.groupBox.setEnabled(False)
              self.display.setEnabled(False)
              self.startup.setEnabled(False)
              self.enable.setText(_translate("MainWindow", "Disable", None))
              self.label_3.setText(_translate("MainWindow", "Active", None))
              palette = QtGui.QPalette()
              palette.setColor(QtGui.QPalette.Foreground, QtCore.Qt.darkGreen)
              self.label_3.setPalette(palette)
  
          elif stu == 1:
  
              #Setting Visuals
              stu = 0
              f = open("C:/Users/"+ user +"/setting.txt", 'w')
              f.truncate()
              f.write("0")
              f.close()
              self.groupBox.setEnabled(True)
              self.display.setEnabled(True)
              self.startup.setEnabled(True)
              self.enable.setText(_translate("MainWindow", "Enable", None))
              self.label_3.setText(_translate("MainWindow", "Inactive", None))
              palette = QtGui.QPalette()
              palette.setColor(QtGui.QPalette.Foreground, QtCore.Qt.darkRed)
              self.label_3.setPalette(palette)
  
      if stu == 0:
          self.groupBox.setEnabled(False)
          self.display.setEnabled(False)
          self.startup.setEnabled(False)
          self.enable.setText(_translate("MainWindow", "Disable", None))
          self.label_3.setText(_translate("MainWindow", "Active", None))
          palette = QtGui.QPalette()
          palette.setColor(QtGui.QPalette.Foreground, QtCore.Qt.darkGreen)
          self.label_3.setPalette(palette)
      elif stu == 1:
          self.groupBox.setEnabled(True)
          self.display.setEnabled(True)
          self.startup.setEnabled(True)
          self.enable.setText(_translate("MainWindow", "Enable", None))
          self.label_3.setText(_translate("MainWindow", "Inactive", None))
          palette = QtGui.QPalette()
          palette.setColor(QtGui.QPalette.Foreground, QtCore.Qt.darkRed)
          self.label_3.setPalette(palette)
  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_())
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Oops, forgot the full error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Users\Jan\Desktop\output.py", line 180, in activate
      if stu == 0:
  NameError: name 'stu' is not defined
  </code></pre>