gpt4 book ai didi

python - 全局变量未定义

转载 作者:行者123 更新时间:2023-12-01 05:13:42 26 4
gpt4 key购买 nike

我正在尝试通过在输入行中发布地址来使用 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/

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