无法将tkinter回调值传递给函数

时间:2018-07-19 18:06:40

标签: python tkinter

def browse_button():

    ## Store the path as a global variable
    global folder_path
    filename = filedialog.askdirectory()
    folder_path.set(filename)
    print(filename)
    return folder_path

def outputfile():

    global out_path
    filepath = filedialog.asksaveasfilename(filetypes = (("CSV","*.csv"),("all files","*.*")))
    out_path.set(filepath)
    print (filepath)
    return out_path

## Instantiating root widget
root = Tk()

# StringVar and IntVar are a type of control variable. 
#These store values needed later

folder_path= StringVar()
out_path = StringVar()
h20_yr = IntVar()  

wy_lbl= Label(root, text = "Water Year: ")
wy_lbl.grid( row = 0, column = 1)

wy_entry = Entry(root)
wy_entry.grid(row = 0, column = 2)

lbl1 = Label(root, textvariable = folder_path)
lbl1.grid(row = 1, column=1)

browse = Button(text="Input Folder", command=browse_button)
browse.grid(row=1, column=2)

lbl2 = Label(root, textvariable= out_path)
lbl2.grid(row =2, column = 1)

save_as = Button(text = 'Save Output', command = outputfile)
save_as.grid(row = 2, column = 2)

print( folder_path.get(), out_path.get(), h20_yr.get())

此处是python新手。

通过按钮调用函数调用,标签填充以及所选文件夹和输出文件的文件路径都不会出现问题。那么为什么这些不被全局存储?我需要将这些值传递给运行该程序的现有代码。

谢谢。

0 个答案:

没有答案