【发布时间】:2014-01-16 07:08:54
【问题描述】:
我在 mac OSX 上使用 python 2.7.5
在我的代码中,我有一个需要返回值的函数。在函数中,我希望我的回调函数(tkinter)使其成为返回值的父函数,但如果有更好的方法可以做到这一点,请说。对不起,如果我不清楚,因为我不确定如何解释我的问题。
def Open():
class dataC:
lines=[]
data=dataC
def event():
path=pathE.get()
file=fileE.get()
try:
os.chdir(path)
temp=open(file)
lines=temp.readlines()
temp.close()
except:
lines=[]
else:
os.chdir(path)
temp=open(file)
lines=temp.readlines()
temp.close()
data.lines=lines
gui=Tk()
gui.title("Open File")
pathE=Entry(gui)
pathE.pack(padx=5,pady=5)
pathE.insert(INSERT,startingDir)
fileE=Entry(gui)
fileE.pack(padx=5,pady=5)
fileE.insert(INSERT,"File Name")
openE=Button(gui,text="Open File",command=event)
openE.pack(padx=5,pady=5)
gui.mainloop()
【问题讨论】:
-
那你为什么不直接使用
return something_to_return? -
@nabla 因为事件(我得到需要返回的值的函数)是一个回调函数
-
如果不将返回值传递给调用函数,您希望将返回值传递到哪里?
-
我需要父函数 (Open()) 来返回值,但在回调函数运行之前不会创建值。所以我不能只添加 >return stuff
-
你的主循环在哪里?
标签: python function tkinter return parent