2

i have this variable outside of my function and when i try to modify it inside the function ther's just a new variable defined inside the function.

accessPath = ""

def AccessButton_Func():
    text=filedialog.askopenfile().name
    if not text == None: 
        accessPath = text
        AccessLabel.configure(text=text)

my function is used in a tkinter button and there's no return for it.

i tried using a global variable but that does not work with other functions i have.

2 Answers2

2

`To modify a global variable inside a function, you need to use the global keyword to indicate that you want to modify the global variable, rather than creating a new local variable with the same name

def AccessButton_Func():
    global accessPath  
    text = filedialog.askopenfile().name
    if text:
       accessPath = text  
       AccessLabel.configure(text=text)
Harish
  • 21
  • 2
2

Try using global keyword in order to change your variable from the function scope:

accessPath = ""

def AccessButton_Func():
    global accessPath # <-------------------- HERE

    text=filedialog.askopenfile().name
    if not text == None: 
        accessPath = text
        AccessLabel.configure(text=text)
Alex
  • 21
  • 2