未定义全局变量

时间:2014-05-13 16:55:30

标签: python file tkinter global defined

我试图通过在输入行中发布地址来尝试使用python读取文件。在我的计划中,当我按下按钮时,程序将读取文件,使用第一个文件中的文本进行所有需要的工作,并将结果写入第二个文件:

import Tkinter

class Generator(Tkinter.Tk):

    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent=parent
        self.initialize()

    def initialize(self):
        self.grid()
        self.addressLink = Tkinter.StringVar()
        self.entry=Tkinter.Entry(self,textvariable=self.addressLink)
        self.entry.grid(column=0,row=0,sticky='EW')

        self.entry.bind("<Return>", self.OnPressEnter)
        self.entry.bind(".", self.OnPressDot) # verify that address was accepted
        self.addressLink.set(u"Enter your input file's address here!")

        button=Tkinter.Button(self,text=u'Generate the list!',command=self.OnButtonClick)
        button.grid(column=1,row=0)

        self.labelVariable = Tkinter.StringVar()
        label = Tkinter.Label(self, textvariable=self.labelVariable,
                              anchor="w",fg="white",bg="blue")
        label.grid(column=0,row=1,columnspan=2,sticky='EW')
        self.labelVariable.set(u"Enter Address !")


        self.grid_columnconfigure(0,weight=1) 
        self.resizable(True,False)

    def ProgramBody(readlink):
        excelWrite=open('/Users/BUR/Desktop/final_TK.txt','w')

        z=0
        for index, line in enumerate(readlink, start=0):
            keywrds=[]
            title=line.split("+")
            title=[lines.strip()for lines in title]
            print title[0]
            print index

            header="Title"+"\t"+"Price equal to title:"+"\t"+"keyword1"+"\t"+"keyword2"+"   \t"+"keyword3"+"\t"+"keyword4"+"\t"+"keyword5\t"+"Manufacturer Part Number\n"
            exclWrt(header)

        excelWrite.close()

    def FileRead(tsink):
        excelRead=open(tsink,'r')
        print tsink
        ProgramBody(tsink)

    def OnButtonClick(self): 
        link=(self.addressLink.get())
        # print link
        self.labelVariable.set(link+" (Here is your button press!) ")
        FileRead(link)

    def OnPressEnter(self,event):
        self.labelVariable.set(self.addressLink.get()+" (Here is your address!)")

    def OnPressDot(self,event):
        self.labelVariable.set(self.addressLink.get()+" (Here is your address!!!)")

if __name__=="__main__":

app=Generator(None)
app.title('Converter')
app.mainloop()

#excelRead=open('/Users/BUR/Desktop/listings/data.txt','r')


def exclWrt(typo):
    excelWrite.write(typo)

程序运行,但是当我按下按钮时,它会给我:

> Exception in Tkinter callback Traceback (most recent call last):  
> File
> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
> line 1470, in __call__
>     return self.func(*args)   File "/Users/BUR/Documents/Python/Temp+price+keyw.1", line 114, in
> OnButtonClick
>     FileRead(link) NameError: global name 'FileRead' is not defined

我错过了什么?

1 个答案:

答案 0 :(得分:2)

您正在使用课程。首先,您将有一个类的实例传递给每个函数。通常它名为self

class A:
    def something(self, my_arguments)

而且,要从课程中拨打something,请执行以下操作:

def something_else(self, another_arguments):
    self.something(another_arguments)

第一个参数将自动传递。此外,在创建类的实例时会调用__init__,因此您无需单独的initialize函数。

我建议您阅读有关课程here的更多信息。对于您的问题,这只是一个非常简短的解决方案。