0

I tried to set a global variable in a funktion. global the variable is set to Kategorie = ''

In one of my function I would like to set it to a other value:

elif methode=='show_select_dialog':
writeLog('Methode: show select dialog', level=xbmc.LOGDEBUG)
dialog = xbmcgui.Dialog()
cats = [__LS__(30120), __LS__(30121), __LS__(30122), __LS__(30123), __LS__(30116)]
ret = dialog.select(__LS__(30011), cats)

if ret == 6:
    refreshWidget()
elif 0 <= ret <= 5:
    writeLog('%s selected' % (cats[ret]), level=xbmc.LOGDEBUG)

    global Kategorie
    Kategorie = (cats[ret])        
    refreshWidget()

If I log the variable Kategorie in function refreshWidget the value is correct (cats[ret]), but after that if the function refreshedWidget is called again the value is gone...

elif methode == 'get_item_serienplaner':
sp_items = refreshWidget()

Once I have changed the variable to cats[ret] I would need it as cats[ret]

user294015
  • 65
  • 7

3 Answers3

1

You have to declare your var outside your functions and everytime you want to use it inside a function you need to specify the global varName. As i see your global var name at declaration is Kategory and after you use Kategorie.

arcticless
  • 664
  • 5
  • 14
0

Python wants to make sure that you really know that you are using global variables by explicitly requiring the global keyword.

To use a global variable within a function you have to explicitly delcare you are using it.

Here a small example:

myGlobalVar= 0

def set_globvar():
    global myGlobalVar    # Needed to modify global copy of globvar
    myGlobalVar = 5

def print_globvar():
    print myGlobalVar     # No need for global declaration to read value of myGlobalVar

set_globvar()
print_globvar()           # Prints 5

in your case I see that you declare that you want to access global Kategorie but you do not declare access to global Kategory. I do not know if this is a simple typo or if they are 2 different variable. In any case you need to write global yourGlobalVariable

Stefano
  • 3,981
  • 8
  • 36
  • 66
  • So... did my answer fix your problem? – Stefano Apr 19 '16 at 08:40
  • unfortunatally not... maybe I do not explain right. But I found a other way to solve the proplem. Now I use ``WINDOW.setProperty()`` and ``WINDOW.getProperty()`` and it works perfect for me... – user294015 Apr 20 '16 at 04:34
  • ah so you wanted to set a system variable. have also a look at this question: http://stackoverflow.com/questions/5971312/how-to-set-environment-variables-in-python – Stefano Apr 20 '16 at 06:26
0

Are there really any globals in python? Yes there is a global keyword that allows you to access varibles in file scope. Another way of accessing the variable is to use the import statement. Lets say you have a file named file.py. In order to write to the variable you could. You could access another files variables just as easy.

import file

anyVariable = 42

# ...
def aFunc():
    file.anyVarible = file.anyVarible + 1
Robert Jacobs
  • 3,266
  • 1
  • 20
  • 30