如何将这些输出转换成矩阵格式

时间:2019-02-19 03:39:02

标签: python

enter image description here

我编写了一个显示4x4 tkinter条目小部件的代码。因此,当我在每个输入框中输入值并按“矩阵表单”按钮以打印输出后,它会在外壳中打印如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

我想要实现的是这样打印:

[[1,2,3,4],
 [5,6,7,8],
 [9,10,11,12],
 [13,14,15,16]]

下面是我的代码:

from tkinter import *
import numpy as np
import tkinter.font
import tkinter.ttk as ttk

fourbyfour = Tk()
fourbyfour.wm_geometry("420x400+0+0")
fourbyfour.wm_title("4X4 Matrix Calc")
fourbyfour.focus_set()
fourbyfour.grab_set()

myFont = tkinter.font.Font(family = 'Helvetica' , size = 12, weight = 'bold')

def getmatrix():
    for row in rows:
        for col in row:
            m = col.get()
            print(m)

rows = []
for i in range(4):
    cols = []
    for j in range(4):
        e = Entry(fourbyfour,width=10,font=myFont,bd=5)
        e.grid(row=i, column=j, sticky=NSEW)
        cols.append(e)
    rows.append(cols)

Calculate_2 = Button(fourbyfour, text = "Matrix Form",
                 font = myFont, bd=4, command = getmatrix,
                 bg = 'light blue', height = 2 ,width = 8)
Calculate_2.grid(row=5, column=2)

1 个答案:

答案 0 :(得分:0)

例如,对于您的情况,可以使用命令np.reshape

np.reshape(YOUR_ARRAY, (4, 4))

将为您提供所需的输出