将变量从Tkinter传递到主程序

时间:2014-10-20 09:31:29

标签: python tkinter

也许这很容易,但我只是不明白......

在下面的代码中,“文件打开”按钮将文件名保存为“名称” - 但如何在Tkinter之外访问此变量? “回调”中的返回语句,但是如果回调在“Button”命令中,我将如何访问?

from Tkinter import *
from tkFileDialog   import askopenfilename      

def callback():
    name= askopenfilename() 
    print name

Button(text='File Open', command=callback).pack(fill=X)
mainloop()

#HOW DO I ACCESS FILENAME AFTER MAINLOOP?
name = ????

1 个答案:

答案 0 :(得分:0)

呃,这个怎么样?

from Tkinter import *
from tkFileDialog import askopenfilename      

value_list = []

def callback():
    name = askopenfilename() 
    value_list.append(name)
    print name

Button(text='File Open', command=callback).pack(fill=X)
mainloop()

# value_list[0] is filename