我有两组代码,一组使用'Class'(第二段代码)来管理我的代码,另一组我只定义函数,在我的第二段代码中我得到一个NameError:全局名称' ...' 没有定义。 这两段代码都是出于同样的目的。
from Tkinter import *
import ttk
import csv
USER_LOGIN = "user_login.csv"
class Login:
def __init__(self, master):
frame = Frame(master)
frame.pack()
lment1 = StringVar()
lment2 = StringVar()
self.usernameLabel = Label(frame, text="Username:")
self.usernameLabel.grid(row=0, sticky=E)
self.passwordLabel = Label(frame, text="Password:")
self.passwordLabel.grid(row=1, sticky=E)
self.usernameEntry = Entry(frame, textvariable=lment1)
self.usernameEntry.grid(row=0, column=1)
self.passwordEntry = Entry(frame, textvariable=lment2)
self.passwordEntry.grid(row=1, column=1)
self.loginButton = ttk.Button(frame, text="Login", command=self.login_try)
self.loginButton.grid(row=2)
self.cancelButton = ttk.Button(frame, text="Cancel", command=frame.quit)
self.cancelButton.grid(row=2, column=1)
def login_try(self):
ltext1 = lment1.get()
ltext2 = lment2.get()
if in_csv(USER_LOGIN, [ltext1, ltext2]):
login_success()
else:
login_failed()
def in_csv(fname, row, **kwargs):
with open(fname) as inf:
incsv = csv.reader(inf, **kwargs)
return any(r == row for r in incsv)
def login_success():
print 'Login successful'
tkMessageBox.showwarning(title="Login successful", message="Welcome back")
def login_failed():
print 'Failed to login'
tkMessageBox.showwarning(title="Failed login", message="You have entered an invalid Username or Password")
root = Tk()
root.geometry("200x70")
root.title("title")
app = Login(root)
root.mainloop()
这是第二段代码^^^
# **** Import modules ****
import csv
from Tkinter import *
import ttk
import tkMessageBox
# **** Declare Classes ****
lGUI = Tk()
lment1 = StringVar()
lment2 = StringVar()
USER_LOGIN = "user_login.csv"
def in_csv(fname, row, **kwargs):
with open(fname) as inf:
incsv = csv.reader(inf, **kwargs)
return any(r==row for r in incsv)
def login_try():
ltext1 = lment1.get()
ltext2 = lment2.get()
if in_csv(USER_LOGIN, [ltext1, ltext2]):
login_success()
else:
login_failed()
def login_success():
print 'Login successful'
tkMessageBox.showwarning(title="Login successful", message="Welcome back")
def login_failed():
print 'Failed to login'
tkMessageBox.showwarning(title="Failed login", message="You have entered an invalid Username or Password")
lGUI.geometry('200x100+500+300')
lGUI.title('PVH')
lButton = Button(lGUI, text="Login", command=login_try)
lButton.grid(row=3)
label_1 = Label(lGUI, text="Username")
label_2 = Label(lGUI, text="Password")
entry_1 = Entry(lGUI, textvariable=lment1)
entry_2 = Entry(lGUI, textvariable=lment2)
label_1.grid(row=0)
label_2.grid(row=1)
entry_1.grid(row=0, column=1)
entry_2.grid(row=1, column=1)
lGUI.mainloop()
这就是那段有效的代码^
我收到错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
return self.func(*args)
File "C:/Users/User/Desktop/PVH_work/PVH_program/blu.py", line 33, in login_try
ltext1 = lment1.get()
NameError: global name 'lment1' is not defined
任何帮助将不胜感激:D
答案 0 :(得分:2)
在您的第一个代码片段中,您在__init __方法中定义变量'lment1',使其成为该单个方法的本地。 当您尝试访问'login_try'中的相同变量时,Python不知道它是什么。
如果您希望在班级的任何地方访问变量表单,您应该在类级别上定义它,方法是在'self'上设置它
def __init__(self, master):
[...]
self.lment1 = StringVar()
[...]
这样,您可以稍后通过以下方式访问它:
def login_try(self):
[...]
ltext1 = self.lment1.get()
[...]
它在您的第二个代码示例中起作用的原因是因为您在任何类之外定义它 - 使其全局可用