Ошибка с переменными в функции
from tkinter import *
def pin():
pinq = Tk()
pinq.title('Enter pin')
pinq.geometry('400x250')
pinq['bg'] = 'cyan'
pinw = '1196'
inputq = '2'
def qqww(): print(inputq)
def add(n):
inputq += n
Button(pinq, command=qqww, text='More', bg='yellow').place(x=350, y=220)
Button(pinq, command=lambda: add(1), text='1', width=5, height=3).place(x=10, y=10)
Почему функция add не видит переменную inputq, хотя qqww её видит?
Ответы (1 шт):
Автор решения: insolor
→ Ссылка
Чтобы вложенная функция могла менять значение переменной внешней функции, нужно во вложенной функции объявить эту переменную как nonlocal ("нелокальную"):
def pin():
...
inputq = '2'
def qqww(): print(inputq)
def add(n):
nonlocal inputq
inputq += n
Button(pinq, command=qqww, text='More', bg='yellow').place(x=350, y=220)
Button(pinq, command=lambda: add(1), text='1', width=5, height=3).place(x=10, y=10)