codehaus


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

tkinter global variable


Hello,

I am trying to read a file from askopenfilename outside the function. I 
am getting blank file name. What am I doing wrong? Here is my code.

from tkinter import *
from tkinter import filedialog

def on_openfile():
    global pic
    pic = filedialog.askopenfilename()


root = Tk()

menubar = Menu(root)
root.config(menu=menubar)
file_menu = Menu(menubar)
file_menu.add_command(label="Open", command=on_openfile)
file_menu.add_command(label="Exit", command=root.destroy)
menubar.add_cascade(label="File", menu=file_menu)


f = open(pic)
print(f.read())

root.mainloop()


Thanks.