How to use Tkinter's 'file' variable across different functions in Python?
Question: I'm using Python 2.7. I'm also using a library known as id3reader to get
metadata from mp3 files. If I use this code:

    
    
    import tkFileDialog
    import id3reader
    
    file = tkFileDialog.askopenfile()
    id3r = id3reader.Reader(file)
    print(id3r.getValue('performer')
    

everything works just fine, and the artist of the song's name will be printed
out in the console.

However, I am trying to do this across different functions. So if I use this
code:

    
    
    import tkFileDialog
    import id3reader
    
    def Load(self):
      file = tkFileDialog.askopenfile()
    
    def Display(self):
      id3r = id3reader.Reader(file)
      print(id3r.getValue('performer')
    

I get an error coming from within the id3reader script. If I use:

    
    
    self.file
    

or

    
    
    fileName = file
    global fileName
    

I get a global variable not defined error.

How would I be able to use the built-in 'file' variable across different
functions?


Answer: You're confusing a bunch of different things.

First, the built-in `file` variable is the actual type of file objects. You
don't want to use that, you're trying to hide it with the filename you got
back from `askopenfile()`.

And `file` is not a Tkinter variable—neither the builtin, nor the one you're
creating, have anything to do with Tkinter.

The reason your code isn't working is that, inside the `Load` function, when
you write `file = tkFileDialog.askopenfile()`, you're creating a local
variable. That local variable hides the name of the global variable of the
same name, until the function exits, at which point it goes away.

Your attempt to use `self.file` is a great solution—except you don't have any
classes. If you want to learn about how to use classes in general, and the
idiomatic way to use them with Tkinter in particular, that's a great thing to
learn, but it's too much to teach in a StackOverflow answer, and Python
already comes with a great tutorial.

If you want to use a global variable, you can do that, but (a) you have to use
`global file`, not `global fileName`, if you want `file` to be global, and (b)
you have to put that _inside the`Load` function_, not at the top level. If you
do both of those, then that `file = tkFileDialog.askopenfile()` will now
reassign the global variable `file`, instead of hiding it with a local
variable, so the value will still be available once you're done, to any other
function that wants to access it.

However, a better solution is to not try to share a global variable. Just
`return` the value, and have the caller hold onto it and pass it into
`Display`. Since I can't see the code you're using to call those functions,
I'll have to make something up, but hopefully you can understand it and apply
it to your own code:

    
    
    def Load():
        return tkFileDialog.askopenfile()
    
    def Display(file):
        id3r = id3reader.Reader(file)
        print(id3r.getValue('performer')
    
    f = Load()
    Display(f)
    

