简体   繁体   English

如何在Python中的不同函数之间使用Tkinter的'file'变量?

[英]How to use Tkinter's 'file' variable across different functions in Python?

I'm using Python 2.7. 我正在使用Python 2.7。 I'm also using a library known as id3reader to get metadata from mp3 files. 我还使用一个名为id3reader的库来从mp3文件中获取元数据。 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. 我收到来自id3reader脚本的错误。 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? 如何在不同功能之间使用内置的“文件”变量?

You're confusing a bunch of different things. 您会混淆许多不同的东西。

First, the built-in file variable is the actual type of file objects. 首先,内置file变量是文件对象的实际类型。 You don't want to use that, you're trying to hide it with the filename you got back from askopenfile() . 您不想使用它,而是尝试使用从askopenfile()返回的文件askopenfile()其隐藏。

And file is not a Tkinter variable—neither the builtin, nor the one you're creating, have anything to do with Tkinter. file不是Tkinter变量-内置函数或您要创建的变量都与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. 您的代码无法正常工作的原因是,在Load函数中,当您编写file = tkFileDialog.askopenfile() ,您正在创建一个局部变量。 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. 您尝试使用self.file是一个很好的解决方案-除非您没有任何类。 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. 如果您想了解一般如何使用类,尤其是使用Tkinter的惯用方式,那是一件很不错的事情,但是在StackOverflow答案中要教的太多了,Python已经附带了一个很棒的功能教程。

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. 如果要使用全局变量,则可以这样做,但是(a)必须使用global file ,而不是global fileName ,如果要使file具有全局性,并且(b)必须将其放在Load函数中 ,而不是顶层。 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. 如果您同时执行了这两个操作,则该file = tkFileDialog.askopenfile()现在将重新分配全局变量file ,而不是使用局部变量隐藏它,因此一旦完成,该值仍可用于任何其他函数想要访问它。

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 . 只需return该值,并让调用者保留该值并将其传递给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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM