gpt4 book ai didi

python - 名称错误 : name 'x' is not defined

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:43 24 4
gpt4 key购买 nike

当我运行我的代码(并按下我制作的 GUI 上的启用按钮)时,我得到了

NameError: name 'stu' is not defined

错误。我绞尽脑汁想弄清楚为什么会这样,但我做不到,所以我决定将它发布到 Stackoverflow 上;-)。

我猜它必须对全局变量和局部变量做一些事情,但无论我把 global 放在哪里,它仍然无法运行。

Windows 10 专业版 64 位 Python 3.4(Annaconda3 发行版)。

我希望您能提供帮助,如果您决定提供帮助,请提供某种解释或来源,这样我就不会再次陷入这个愚蠢的问题。

谢谢!

完整代码如下:

# -*- 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_())

编辑:

糟糕,忘了完整的错误:

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

最佳答案

stu 是一个类属性,不是全局的。您在类 block 中定义它:

class Ui_MainWindow(object):
if ...:
# ...
stu = f.read()
else:
# ...
stu = 0

您可以在 Ui_MainWindow.activate() 方法中这样处理它:

if Ui_MainWindow.stu == 0:

请注意,您从未将文件内容从字符串转换为整数,因此 stu 很可能设置为 '0''1' 字符串。您可能希望将字符串转换为整数:

with open("C:/Users/"+ user +"/setting.txt", 'r'):
try:
stu = int(f.read())
except ValueError:
stu = 0

with 语句负责为您关闭文件。

关于python - 名称错误 : name 'x' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32444944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com