0

I need help to understand why my python variable is not changing?

Here is the code:

from tkinter import filedialog
from tkinter import *

selectedRootFolder = ""  #<-------------------------------------here is the variable declared

# get any folder to be a root folder
def add_dir():
    root = Tk()
    root.withdraw()
    dirname = filedialog.askdirectory(parent=root,initialdir="/",title='Please select a directory')
    selectedRootFolder = dirname
    print("Here: " + selectedRootFolder)#<-----------------------here is the variable changed

# print the root folder 
def print_root_dir_path():
    print (selectedRootFolder)  #<-----------------------------here is the variable empty -> =""

# in case a wrong number is taken
def invalid():
    print("---------------------")
    print ("INVALID CHOICE!")
    print("---------------------")

# exit program
def exit_prog():
    print ("Thank you, come back soon!")
    exit()

# define the menu options
menu = {"1":("Choose Directory:", add_dir),
    "2":("Print Root Directory Path",print_root_dir_path),
    "9":("Exit",exit_prog)
   }

while True:    
    # list the menu
    for key in sorted(menu.keys()):
        print (key + ":" + menu[key][0])

    # pick a number 
    print("---------------------")
    ans = input("Make A Choice: ")
    print("---------------------")

    #get the number, if none, call invalid function
    menu.get(ans,[None,invalid])[1]()

This is just a part of the script, but it should be able to show my problem, which is that when I pick option 1, to choose a directory, it is successful, and it prints the selectedRootFolder, but when I choose option 2 after that, the printed value is just as declared in the beginning, empty.

I do not understand why is that, can u help me?

Thank you.

Edit 1:

Changed:

selectedRootFolder = print(dirname)

To:

selectedRootFolder = dirname
print("Here: " + selectedRootFolder)
Elydasian
  • 2,016
  • 5
  • 23
  • 41
  • 1
    Possible dupe: [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – Paritosh Singh Jan 08 '20 at 08:57

1 Answers1

3

Use global

Ex:

selectedRootFolder = ""

def add_dir():
    global selectedRootFolder
    selectedRootFolder = "Update"

add_dir()    
print(selectedRootFolder)

Repl: https://repl.it/repls/SoreLankyAttributes

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • thx, it works, but why was my first code wrong? I ll accept the answer in 5 mins :) – Elydasian Jan 08 '20 at 08:57
  • Because the scope of the variable is only inside the function. `global` allows you to modify the variable outside of the current scope – Rakesh Jan 08 '20 at 08:59
  • @Elydasian note, it is generally best practice to *avoid global variables*, particular *mutable global variables*, so a global variable that will never change is less bad, a "pretend constant" (because there are no constant variables in Python) – juanpa.arrivillaga Jan 08 '20 at 09:20