python - 全局变量未定义

标签 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

我错过了什么?

最佳答案

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

class A:
    def something(self, my_arguments)

并且,要从类中调用某些内容,您可以执行以下操作:

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

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

我建议你阅读更多关于类的内容 here 。这只是解决您问题的一个非常简短的解决方案。

关于python - 全局变量未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23637309/

相关文章:

python - 谁能解释为什么这种排序不起作用?

python - 如何根据函数在调用其项目时返回的内容对列表进行排序

file - 如何在同一时间shell中读取两个文件

perl - 使用 Perl 从 2 个文件逐行打开和读取

python - 如何阻止禁用的 tkinter 按钮变灰?

python - 为什么创建弹出窗口时我的 Canvas 上的绘图没有出现?

python - Django 测试返回登录重定向 302,即使用户已登录

python - 如何计算 Pandas 数据框中列中唯一元素的数量

c - 如何在递归函数中跟踪文件路径名?

python - 如何从另一个类中为一个类的对象设置动画?