global variable not passing, saying its not recognized? global variable not passing, saying its not recognized? tkinter tkinter

global variable not passing, saying its not recognized?


Lot's of stuff going on.

You shouldn't be nesting functions like this. If you're trying to encapsulate data / structure it then just use a class. You seem to be wanting to use one by naming Tk() self and calling self all over the place. Which is really just setting a bunch of attributes to Tk.

You're creating multiple instances of Tk(). If you want new windows then use the Toplevel() widget. Otherwise, just use pack_forget / grid_forget to essentially hide the frame / widget you want and "unhide" the others using pack / grid to replace them on the grid.

No need to have multiple Tk() instances running around.

You never call mainloop.

You're calling global on your variables which is unnecessary you can restructure. If you're using globals 9/10 you have an XY problem. They're really not even global to begin with. They're local to the function(s) you have nested.You can use nonlocal if you really want this structure. Again, if you're going for a structure like this just use a class and keep these as attributes.

There's no need to use bind on all of your buttons just to call a function on click. This is what the command argument is for.


Defining a global variable inside a function simply tells that function to use the specified global variable of the same name. It does not assign a variable in itself. If you are using Python3 you can use the declaration nonlocal instead of global inside the embedded functions in the shop function to tell the embedded functions to use the functions local to shop.

I see that you are want to use the variables in the singleplayergame function so consider defining these variables outside of all functions and then importing them through the global declaration.

In this case, moving the normal, fire and laser variables outside the shop function should do the trick.

Here are some links to help with scope in Python.

Python scoping: understanding LEGB

Setting a global variable in a function