1

Why I can't make nonlocal variable in Class.

here is code --->

    from tkinter import *
     class Note:
         root = Tk()
         nonlocal font_size = 16
         def bigger(event):
             font_size+=5
         root.bind("<Shift-Up>", bigger)
         root.mainloop()

output --->

           nonlocal font_size = 16
                       ^
           SyntaxError: invalid syntax

2 Answers 2

3

Yes, nonlocal variable = value is not valid syntax. nonlocal, just like global, is used to "mark" names as nonlocal and global, respectively. It's not a special form of variable definition. You can "mark" a name as nonlocal like this:

nonlocal variable

And then use variable somewhere in your code.

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

Comments

0

Now I know the answer you must to use init(self)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.