0

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?

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

5 Comments

What if I'm trying to put it into a label by self.artistLabel["text"] = "Artist: {}".format(self.Display)? @abarnert
Because when I'm trying to do that, I keep getting the error: 'NoneType' object does not support item assignment. I usually know what causes this error, and I've done all of the fixes I can think of, but I still can't get it to work? @BryanOakley
@HunterWatkins: You can't use self unless you create a class, and move your code into methods of that class. As I said in the answer, you're not going to be able to learn how to do that from an SO answer, but the official Python tutorial and the Tkinter tutorial are both very nice.
@HunterWatkins: Meanwhile, the 'NoneType' object does not support item assignment error can only mean one thing: you've written spam[eggs], and spam is None. In Tkinter, that often means you've done something like spam = Button(…).pack(); since pack() returns None, this means you've lost the reference to the Button object, and instead have a reference to None.
I am using classes. But nonetheless, I found out what I was doing wrong and got it working. Thanks for all your help. @abarnert

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.