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]

1
  • yes, that's a simple typo... it is also Kategorie Commented Apr 18, 2016 at 17:11

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

So... did my answer fix your problem?
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...
ah so you wanted to set a system variable. have also a look at this question: stackoverflow.com/questions/5971312/…
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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.