Python类别:未定义的全局/局部变量名称 [英] Python Class: Global/Local variable name not defined

查看:43
本文介绍了Python类别:未定义的全局/局部变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两组代码,一组使用类"(第二段代码)来管理我的代码,另一组我只是定义函数,在第二段代码中,我得到一个NameError:global name....' 没有定义.这两段代码是出于相同的目的.

I have two sets of code, one which I use 'Class' (Second piece of code) to manage my code, and the other I just define functions, in my second piece of code I get a NameError: global name '...' is not defined. Both pieces of code are are for the same purpose.

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

那是第二段代码^^^

That is the second piece of code ^^^

# **** 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

Any help would be appreciated :D

推荐答案

在您的第一个代码段中,您在__init __方法中定义了变量"lment1",使其在该单一方法中局部存在.然后,当您尝试在'login_try'中访问相同的变量时,Python并不知道它是什么.

In your first code piece, you define the variable 'lment1' in the __init __ method, making it local to that single method. When you then try to access the same variable in the 'login_try', Python doesn't know what it is.

如果您希望在班级中的任何地方访问变量形式,则应通过在"self"上设置它来在班级级别上对其进行定义

If you wish to access the variable form wherever in the class, you should define it on the class level, by setting it on 'self'

def __init__(self, master):
    [...]
    self.lment1 = StringVar()
    [...]

这样,您以后可以使用:

That way, you can access it later with:

def login_try(self):
    [...]
    ltext1 = self.lment1.get()
    [...]

它在您的第二个代码示例中起作用的原因是因为您在任何类之外定义了它-使其在全球范围内可用

The reason it works in your second code sample, is because you defined it outside of any class - Making it globally available

这篇关于Python类别:未定义的全局/局部变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆