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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| #!/usr/bin/env python
# -*- coding: utf-8 -*-
from Tkinter import *
def dispatch_yview (*args):
"""
cette fonction dispatche les commandes de la scrollbar
VERTICALE entre les deux canevas;
"""
canvas.yview(*args)
canvas2.yview(*args)
# end def
def ensure_float (expression):
"""
évalue puis convertit une expression en float,
retourne 0.0 si erreur;
"""
try:
return float(eval(str(expression)))
except Exception as e:
#~ print "une erreur est survenue :", str(e)
return 0.0
# end try
# end def
def ok():
"""
validation des données, calculs et création du nouveau
fichier de sortie;
"""
with open("matrice.txt") as file_in:
lines = file_in.readlines()
# end with
# Sélection de la matrice VERTEX_LIST :
indice_vertexListDebut= lines.index(" VERTEX_LIST\n")
matrice_lines1=lines[indice_vertexListDebut+2:]
indice_vertexListFin= matrice_lines1.index(" }\n")
lines2=matrice_lines1[:indice_vertexListFin]
#fin sélection
# stockage des deux encadrements
str1=''
avant=lines[:indice_vertexListDebut+3]
print 'avant : ', avant
for i in range (len(avant)-1):
str1=str1+avant[i]
str3=''
apres=matrice_lines1[indice_vertexListFin:]
for i in range (len(apres)-1):
str3=str3+apres[i]
# fin stockage
decimals = E4.get()
entries = [E1.get(), E2.get(), E3.get()]
str_data = ""
for line in lines2:
# si line = "[nnn, nnn, nnn]" inutile de réinventer la roue:
# c'est déjà la structure d'une liste Python [nnn, nnn, nnn]
values = eval(line) if line.strip() else []
data = []
for (i, j) in zip(values, entries):
# il faut évaluer chaque valeur séparément !
if '*' in j :
j=j.replace('*','')
sum_up= ensure_float(i) * ensure_float(j)
elif '/' in j :
j=j.replace('/','')
sum_up= ensure_float(i) / ensure_float(j)
else:
sum_up = ensure_float(i) + ensure_float(j)
if decimals:
sum_up = round(sum_up, int(decimals))
# end if
data.append(sum_up)
# end for
str_data += str(data) + '\n'
# end for
str_data=str1+str_data+str3
with open('matrice2.txt', 'w') as file_out:
file_out.write(str_data)
# end with
canvas2.itemconfigure(c2_text_id, text=str_data)
lbl_output.configure(text="Operation succeeded!")
# end def
# Création de fenêtre
root = Tk()
root.title("coordinate change")
root.resizable(width=False, height=False)
# Label, Entry
Message(root, text="""Notice:
This program allows to increase or decrease one \
of the coordinates (X, Y or Z) of a [X,Y,Z] matrice such as:
[ [15, 1.454, 0.1], [12.1, 5.2, 78], ..., [0.3, 0, 45] ]
Please copy your matrice in a file whose the name is 'matrice.txt'.
Put the file in the same directory than this program.
The new matrice will be in the file named 'matrice2.txt'.""",
font="sans 10", width=360).grid(row=0, column=1, padx=5)
my_font = "helvetica 14 underline"
frame = Frame(root)
frame.grid(row=0, column=0)
Label(frame, text='insert values:', font=my_font).grid(row=0, column=1)
Label(frame, text='X:', font=my_font).grid(padx=5, sticky=E)
E1 = Entry(frame)
E1.grid(row=1, column=1)
#~ E1.insert(0, 0) # risque de notation *octale* e.g. 025 = 21 /!\
Label(frame, text='Y:', font=my_font).grid(padx=5, sticky=E)
E2 = Entry(frame)
E2.grid(row=2, column=1)
Label(frame, text='Z:', font=my_font).grid(padx=5, sticky=E)
E3 = Entry(frame)
E3.grid(row=3, column=1)
Label(frame, text='Round values\nup to (decimals):').grid(padx=5, sticky=E)
E4 = Entry(frame)
E4.grid(row=4, column=1, sticky=SW)
Button(frame, text='OK', width=10, command=ok).grid(row=4, column=2, padx=10, sticky=S)
lbl_output = Label(root, fg="red")
lbl_output.grid(row=2, column=1)
frame = Frame(root)
frame.grid(row=1, column=0, columnspan=2)
txt_options = dict(font="sans 12 bold", fill="blue", anchor=NW)
Label(frame, text='matrice.txt contents:', font=my_font).grid(row=0, column=0)
with open("matrice.txt") as file_in:
lines = file_in.read().strip()
# end with
canvas = Canvas(frame, bg="white")
canvas.grid(row=1, column=0, sticky=NW+SE)
canvas.create_text(0, 0, text=lines, **txt_options)
# horizontal scrollbar
hbar = Scrollbar(frame, orient=HORIZONTAL)
hbar.grid(row=2, column=0, sticky=NW+SE)
# vertical scrollbar pour les 2
vbar = Scrollbar(frame, orient=VERTICAL)
vbar.grid(row=1, column=3, sticky=NW+SE)
# connecting people...
canvas.configure(
xscrollcommand=hbar.set,
yscrollcommand=vbar.set,
scrollregion=canvas.bbox(ALL),
)
hbar.configure(command=canvas.xview)
vbar.configure(command=canvas.yview)
frame.columnconfigure(1, minsize=5)
Label(frame, text='matrice2.txt contents:', font=my_font).grid(row=0, column=2)
canvas2 = Canvas(frame, bg="white")
c2_text_id = canvas2.create_text(0, 0, **txt_options)
canvas2.grid(row=1, column=2, sticky=NW+SE)
# horizontal scrollbar
hbar = Scrollbar(frame, orient=HORIZONTAL)
hbar.grid(row=2, column=2, sticky=NW+SE)
# connecting people...
canvas2.configure(
xscrollcommand=hbar.set,
yscrollcommand=vbar.set,
scrollregion=canvas.bbox(ALL),
)
hbar.configure(command=canvas2.xview)
vbar.configure(command=canvas2.yview)
# special dispatch between 2 canvases
vbar.configure(command=dispatch_yview)
Button(root, text="Quit", command=root.destroy).grid(row=2, column=1, padx=5, pady=5, sticky=SE)
root.mainloop() |
Partager