r/pythonhelp • u/[deleted] • Jun 08 '18
Got some Problems with the whole global/local Variable thing
Well thats my code fullvar is not defined thing what did i wrong?
NameError: name 'fullvar' is not defined the short version with the problem:
fullvar = 1
def togglefull():
global fullvar
if (fullvar==1):
fullvar = 0
else:
fullvar = 1the long version with the problem:
class Page_Options(tk.Frame):
def __init__(self, parent, controller):
fullvar = 1
tk.Frame.__init__(self, parent)
self.controller = controller
button = tk.Button(self, text='back', command=lambda : \
controller.show_frame('Page_MainMenu'))
button.pack()
def togglefull():
global fullvar
if (fullvar==1):
controller.overrideredirect(True)
controller.geometry('{0}x{1}+0+0'.format(controller.winfo_screenwidth(),
controller.winfo_screenheight()))
controller.focus_set()
fullvar = 0
else:
controller.overrideredirect(False)
controller.geometry('{0}x{1}+0+0'.format(500,
500))
controller.focus_set()
fullvar = 1
button2 = tk.Button(self, text='Toggle Fullscreen', command=togglefull)
button2.pack()
label = tk.Label(self, text='Das hier sind die Optionen')
label.pack()
0
Upvotes
1
1
u/Usagi-Nezumi Jun 27 '18
You could throw fullvar into the class definition and use self.fullvar instead. Unless I'm wrong.
class idea():
fullvar = 0
def foo(self,n):
if self.fullvar == 1:
pass # Code goes here
else:
pass # Code goes here
1
u/Cache_of_kittens Jun 09 '18
Does defining fullvar in init allow this to be global?