1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| #!/usr/bin/env python
# -*- coding: utf-8 -*-
# this script is Python2.6 compatible
def get_matrix_data (path):
"""
extrait des données d'un fichier texte contenant des lignes
de matrices [x, y, z];
"""
with open(path) as fichier:
return map(eval, fichier.readlines())
# end with
# end def
def matrix_add (matrix, vector, round_n=None):
"""
additionne les valeurs du tableau @vector à chaque ligne de
la matrice @matrix en appliquant un arrondi @round_n au
résultat final si @round_n != None;
"""
# sous-fonction
def sum_up (tuple_ab):
a, b = tuple_ab
s = float(a) + float(b)
if round_n != None:
return round(s, round_n)
else:
return s
# end if
# end def
return [map(sum_up, zip(row, vector)) for row in matrix]
# end def
def fancy_matrix (matrix):
"""
formate l'apparence d'une matrice pour qu'elle présente bien
en sortie console;
"""
return "[\n {0},\n]".format(",\n ".join(map(str, matrix)))
# end def
# test demo
data = get_matrix_data("matrice.txt")
print "données du fichier :"
print fancy_matrix(data)
print "\non additionne", vector
print "\navec un arrondi de", arrondi, "chiffres après la virgule"
data = matrix_add(data, vector, arrondi)
print "\nrésultat :"
print fancy_matrix(data)
# fin test demo
# programme originel :
#--- Programme qui change une coordonnée (x,y ou z) de toute une matrice ---#
from Tkinter import *
from math import *
def ok():
arrondi = int(E4.get())
vector = [float(E1.get()), float(E2.get()), float(E3.get())]
data = get_matrix_data("matrice.txt")
data = matrix_add(data, vector, arrondi)
data =fancy_matrix(data)
with open('matrice2.txt','w') as outf:
outf.write(data)
L4.config(text = data)
L.config(text = "Finished !")
fe=Tk()
fe.title("coordinate change")
fe.geometry('1250x1000')
L=Label(fe,text='This program allows to increase or decrease one of the coordinates (X, Y or Z) of a [X,Y,Z] matrice such as: \n [15,1.454,0.1]\n[12.1,5.2,78]\n[0.3,0,45]\n(...)\n\n Please copy your matrice in a file whose the name is matrice.txt.\nPut the file in the same directory than this program. The new matrice will be in the file named matrice2.txt.',font=('Arial',17,'bold'))
L.place(x=20,y=50)
LX=Label(fe,text='X',font=('Helvetica',15,'underline'))
LX.place(x=200,y=450)
LY=Label(fe,text='Y',font=('Helvetica',15,'underline'))
LY.place(x=200,y=550)
LZ=Label(fe,text='Z',font=('Helvetica',15,'underline'))
LZ.place(x=200,y=650)
L1=Label(fe,text='Give the value(s)',font=('Helvetica',15,'underline'))
L1.place(x=400,y=410)
E1=Entry(fe)
E1.place(x=400,y=450)
E1.insert(0,0)
E2=Entry(fe)
E2.place(x=400,y=550)
E2.insert(0,0)
E3=Entry(fe)
E3.place(x=400,y=650)
E3.insert(0,0)
L2=Label(fe,text='Contenu de matrice.txt :',font=('Helvetica',15,'underline'))
L2.place(x=1000,y=320)
with open("matrice.txt") as inf:
lines = inf.readlines()
L3=Label(fe,text=lines)
L3.place(x=1000,y=350)
L2=Label(fe,text='Contenu de matrice2.txt :',font=('Helvetica',15,'underline'))
L2.place(x=1000,y=620)
L4=Label(fe,text='')
L4.place(x=1000,y=650)
B=Button(fe,text='OK',width=25,command=ok)
B.place(x=600,y=550)
L5=Label(fe,text='Voulez-vous arrondir ? Si oui, mettez le nombre de chiffres après la virgule que vous souhaitez faire apparaître :')
L5.place(x=0,y=320)
E4=Entry(fe)
E4.place(x=600,y=320)
fe.mainloop() |
Partager