So, I’m having some trouble to understand how I can access the value of a variable of a function from another function within a class.
import Tkinter as tk, tkFileDialog
class test:
def __init__(self):
root = tk.Tk()
song_button = tk.Button(root, text = 'Select Song', fg = 'blue', command = self.loadfile).pack()
#how do I access the value of filename now?
def loadfile(self):
filename = tkFileDialog.askopenfilename(filetypes=[("allfiles","*"),("pythonfiles","*.py")])
Right now filename is just a local variable in the
loadfilefunction. You need to make filename an attribute of the object. Doself.filename = ..., then in other methods you can access it asself.filename.(In this particular case what you’re asking seems a bit odd, since
loadfilewon’t have been called at the time you seem to want to accessfilename, sofilenamewon’t even exist. But this is the general idea. No matter what, you obviously need to call the function where the variable is defined before you can do anything with it.)