user11669334
user11669334

Reputation:

python tkinter SyntaxError

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

Upvotes: 1

Views: 261

Answers (2)

user11669334
user11669334

Reputation:

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

Upvotes: 0

ForceBru
ForceBru

Reputation: 44838

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.

Upvotes: 3

Related Questions