from tkinter import *

def matrix_print(matrix):
    print("______")
    for i in matrix:
        for el in i:
            print(el, end=" ")
        print()

def matrix_metric(matrix):
    m = len(matrix)
    n = len(matrix[0])
    metric = (m,n)
    return metric

def transpose_matrix(matrix):
    metric = matrix_metric(matrix)
    transposed = []
    for i in range(metric[1]):
        column = []
        transposed.append(column)

    for i in range(metric[0]):
        for c in range(len(transposed)):
            transposed[c].append(matrix[i][c])
    return transposed

def minor_matrix_i(matrix,m1,n1):
    result = [row[:] for row in matrix]
    result.pop(m1)
    for m in range(len(result)):
        result[m].pop(n1)
    return result

def minor_matrix_ii(matrix):
    m = matrix_metric(matrix)[0]
    if m == 3:
        minorred_matrix = [[] for i in range(matrix_metric(matrix)[0])]
        for i in range(len(matrix)):
            for b in range(len(matrix[i])):
                minorred_matrix[i].append(det_matrix_ii(minor_matrix_i(matrix, i, b)))
        return minorred_matrix
    elif m == 2:
        minorred_matrix = [[0, 0], [0, 0]]
        minorred_matrix[0][0] = matrix[1][1]
        minorred_matrix[0][1] = matrix[1][0]
        minorred_matrix[1][0] = matrix[0][1]
        minorred_matrix[1][1] = matrix[0][0]
        return minorred_matrix


def det_matrix_ii(matrix):
    m_sum = 0
    for i in matrix:
        m_sum += 1
    #print('сумма элементов',m_sum)
    determ_num = 0
    for n in range(len(matrix)):

        if m_sum == 2:
            determ_num += matrix[0][n] * (minor_matrix_i(matrix,0,n))[0][0] * algebraic_add(matrix)[0][n]
        if m_sum == 3:
            determ_num += matrix[0][n] * det_matrix_ii(minor_matrix_i(matrix, 0, n)) * algebraic_add(matrix)[0][n]
    return determ_num


def algebraic_add(matrix):
    algebraic_added = [[((-1)**(2+m+n)) for n in range(matrix_metric(matrix)[0])] for m in range(matrix_metric(matrix)[0])]
    return algebraic_added

def create_matrix(m, n):
    matrix = []
    for i in range(m):
        matrix.append([0] * n)
    return matrix

def create_and_print_matrix():
    # Get the matrix dimensions from the entry_fields list
    m = len(entry_fields)
    n = len(entry_fields[0])

    matrix = create_matrix(m, n)

    # Get the values from the entry_fields and update the matrix
    for i in range(m):
        for j in range(n):
            entry_value = entry_fields[i][j].get()
            try:
                matrix[i][j] = int(entry_value)
            except ValueError:
                # Handle non-integer values entered in the entry fields
                print(f"Invalid value entered at row {i+1}, column {j+1}")

    # Display the matrix
    return matrix

def add_or_remove_row():
    metric_sum = len(entry_fields)
    if metric_sum == 2:
        entry_fields.append([])
        for j in range(len(entry_fields[0])):
            entry = Entry(root, width=5)
            entry.grid(row=len(entry_fields), column=j+1)
            entry_fields[-1].append(entry)
    elif metric_sum == 3:
        if len(entry_fields) > 2:
            for entry in entry_fields[-1]:
                entry.destroy()
            entry_fields.pop()

def add_or_remove_column():
    metric_sum = len(entry_fields[0])
    if metric_sum == 2:
        for row in entry_fields:
            entry = Entry(root, width=5)
            entry.grid(row=entry_fields.index(row)+1, column=len(row)+1)
            row.append(entry)
    elif metric_sum > 2:
        for row in entry_fields:
            if len(row) > 2:
                entry = row[-1]
                entry.destroy()
                row.pop()

root = Tk()
root.title("Матричный калькулятор by AlexAnderDeath")
root.geometry("300x300")

entry_fields = [[] for _ in range(2)] # Список для хранения полей ввода

def transpose_tk():
    matrix_a = create_and_print_matrix()
    matrix_print(transpose_matrix(matrix_a))

def det_tk():
    matrix_a = create_and_print_matrix()
    metric = matrix_metric(matrix_a)
    if metric[0] != metric[1]:
        print("Матрица должна быть квадратной")
    else:
        print(det_matrix_ii(matrix_a))
def alg_add_tk():
    matrix_a = create_and_print_matrix()
    matrix_print(algebraic_add(matrix_a))

def minor_tk():
    matrix_a = create_and_print_matrix()
    matrix_print(minor_matrix_ii(matrix_a))

def inverse_matrix():

    matrix_a = create_and_print_matrix()

    metric = matrix_metric(matrix_a)
    if metric[0] != metric[1]:
        print("Матрица должна быть квадратной")
    else:
        det = det_matrix_ii(matrix_a)
        if det == 0:
            print( "The matrix is not invertible.")
        else:
            alg_add = algebraic_add(matrix_a)
            adjugate = transpose_matrix(minor_matrix_ii(matrix_a))
            inverse = [[adjugate[i][j] * alg_add[i][j] for j in range(len(adjugate[0]))] for i in range(len(adjugate))]
            matrix_print(inverse)
            print(f'1/{det}')

# Создание полей ввода для двумерного списка 2x2
for i in range(2):
    for j in range(2):
        entry = Entry(root, width=5)
        entry.grid(row=i+1, column=j+1)
        entry_fields[i].append(entry)

# Создание кнопок
add_button = Button(root, text="“n+”", command=add_or_remove_row)
add_button.grid(row=0, column=1)

add_button = Button(root, text="“m+”", command=add_or_remove_column)
add_button.grid(row=1, column=0)

btn_create_matrix = Button(root, text="A^t", command=transpose_tk)
btn_create_matrix.grid(row=4, column=0, columnspan=2)

btn_create_matrix = Button(root, text="| A |", command=det_tk)
btn_create_matrix.grid(row=5, column=0, columnspan=2)

btn_create_matrix = Button(root, text="(-1)^i+j", command=alg_add_tk)
btn_create_matrix.grid(row=4, column=2, columnspan=2)

btn_create_matrix = Button(root, text="A(min)", command=minor_tk)
btn_create_matrix.grid(row=5, column=2, columnspan=2)

btn_create_matrix = Button(root, text="A^-1", command=inverse_matrix)
btn_create_matrix.grid(row=6, column=1, columnspan=1)

root.mainloop() придумай как можно оптимизировать программу