Ввод матрицы Python

artgor

Новичок
Пользователь
Янв 11, 2023
9
0
1
Код:
from tkinter import Tk, Label, StringVar, Button, Entry
window = Tk()
window.title("Matrix")
window.geometry("650x500+120+120")
window.configure(bg='bisque2')
window.resizable(True, True)
A=[]
# empty arrays for your Entrys and StringVars
text_var = []
entries = []

# callback function to get your StringVars
def get_mat():
    matrix = []
    for i in range(rows):
        matrix.append([])
        for j in range(cols):
            if j== 'None':
                del cols[j]
            else:
                matrix[i].append(text_var[i][j].get())

    print(matrix)

Label(window, text="Enter matrix :", font=('arial', 10, 'bold'),
      bg="bisque2").place(x=20, y=20)

x2 = 0
y2 = 0
rows, cols = (20,20)
for i in range(rows):
    # append an empty list to your two arrays
    # so you can append to those later
    text_var.append([])
    entries.append([])
    for j in range(cols):
        # append your StringVar and Entry
        text_var[i].append(StringVar())
        entries[i].append(Entry(window, textvariable=text_var[i][j],width=3))
        entries[i][j].place(x=60 + x2, y=50 + y2)
        x2 += 30

    y2 += 30
    x2 = 0
button= Button(window,text="Submit", bg='bisque3', width=15, command=get_mat)
button.place(x=160,y=140)
window.mainloop()


Люди добрые, помогите, надо сделать так, чтобы при вводе матрицы пустые элементы удалялись и выводились только числа. Ясам ничего не могу придумать, в интернете полазил, поискал, не помогло.
 

regnor

Модератор
Команда форума
Модератор
Июл 7, 2020
2 580
457
83
не уверен, что понял вопрос, но наверно так
Python:
from tkinter import Tk, Label, StringVar, Button, Entry
window = Tk()
window.title("Matrix")
window.geometry("650x500+120+120")
window.configure(bg='bisque2')
window.resizable(True, True)
A=[]
# empty arrays for your Entrys and StringVars
text_var = []
entries = []

# callback function to get your StringVars
def get_mat():
    matrix = []
    for i in range(rows):
        matrix.append([])
        for j in range(cols):
            if text_var[i][j].get() == '':
                break
            else:
                matrix[i].append(text_var[i][j].get())

    print(matrix)

Label(window, text="Enter matrix :", font=('arial', 10, 'bold'),
      bg="bisque2").place(x=20, y=20)

x2 = 0
y2 = 0
rows, cols = (20,20)
for i in range(rows):
    # append an empty list to your two arrays
    # so you can append to those later
    text_var.append([])
    entries.append([])
    for j in range(cols):
        # append your StringVar and Entry
        text_var[i].append(StringVar())
        entries[i].append(Entry(window, textvariable=text_var[i][j],width=3))
        entries[i][j].place(x=60 + x2, y=50 + y2)
        x2 += 30

    y2 += 30
    x2 = 0
button= Button(window,text="Submit", bg='bisque3', width=15, command=get_mat)
button.place(x=160,y=140)
window.mainloop()
 

artgor

Новичок
Пользователь
Янв 11, 2023
9
0
1
Да, именное так, но мне нужно чтобы пустых массивов в массиве тоже не было, а оставались только числа
 

regnor

Модератор
Команда форума
Модератор
Июл 7, 2020
2 580
457
83
Да, именное так, но мне нужно чтобы пустых массивов в массиве тоже не было, а оставались только числа
Python:
from tkinter import Tk, Label, StringVar, Button, Entry
window = Tk()
window.title("Matrix")
window.geometry("650x500+120+120")
window.configure(bg='bisque2')
window.resizable(True, True)
A=[]
# empty arrays for your Entrys and StringVars
text_var = []
entries = []

# callback function to get your StringVars
def get_mat():
    matrix = []
    for i in range(rows):
        matrix.append([])
        for j in range(cols):
            if text_var[i][j].get() == '':
                break
            else:
                matrix[i].append(text_var[i][j].get())

    matrix = list(filter(None, matrix))
    print(matrix)

Label(window, text="Enter matrix :", font=('arial', 10, 'bold'),
      bg="bisque2").place(x=20, y=20)

x2 = 0
y2 = 0
rows, cols = (20,20)
for i in range(rows):
    # append an empty list to your two arrays
    # so you can append to those later
    text_var.append([])
    entries.append([])
    for j in range(cols):
        # append your StringVar and Entry
        text_var[i].append(StringVar())
        entries[i].append(Entry(window, textvariable=text_var[i][j],width=3))
        entries[i][j].place(x=60 + x2, y=50 + y2)
        x2 += 30

    y2 += 30
    x2 = 0
button= Button(window,text="Submit", bg='bisque3', width=15, command=get_mat)
button.place(x=160,y=140)
window.mainloop()
 

artgor

Новичок
Пользователь
Янв 11, 2023
9
0
1
Спасибо большое, очень выручили
 

Форум IT Специалистов