Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try:
- from tkinter import *
- except:
- from Tkinter import *
- def leggifile(nomefile):
- lines = []
- try:
- miofile = open(nomefile)
- miofile.close()
- except FileNotFoundError:
- fatto.grid(column = 0, row = 5,columnspan = 2, padx = 10, pady = 10)
- fatto.configure(text = "File non trovato, file creato")
- else:
- for l in open(nomefile):
- lines.append(l.strip())
- return lines
- def scrivifile(nomefile, lista):
- miofile = open(nomefile, "w")
- for cont in lista:
- miofile.writelines(cont + "\n")
- miofile.close()
- fatto.grid(column = 0, row = 5,columnspan = 2, padx = 10, pady = 10)
- def codifica(riga, chiave):
- return ''.join([chr(ord(lettera) + chiave) for lettera in riga])
- def decodifica(riga, chiave):
- return ''.join([chr(ord(lettera) - chiave) for lettera in riga])
- def main():
- linee = leggifile(nomefile.get())
- nuovofile = []
- if scelta.get() == 0:
- for x in linee:
- stringa = codifica(x,int(chiave.get()))
- nuovofile.append(stringa)
- elif scelta.get() == 1:
- for x in linee:
- stringa = decodifica(x, int(chiave.get()))
- nuovofile.append(stringa)
- scrivifile(nomefile.get(), nuovofile)
- root = Tk()
- root.geometry("350x240")
- root.resizable(False, False)
- root.title("Cifrario di Cesare")
- root.configure(bg = "white")
- l1= Label(root,text = "Inserisci il percorso del file:",bg = "white")
- nomefile = StringVar(value = "")
- e1 = Entry(root, textvariable = nomefile,width= 40,bg = "white")
- l2 = Label(root, text = "Inserisci la chiave:",bg = "white")
- chiave = StringVar(value = "")
- s1 = Spinbox(root, from_= 0,to= 255,width= 10, textvariable = chiave,bg = "white")
- scelta = IntVar()
- r1 = Radiobutton(root, text = "Cripta", value = 0, variable = scelta,bg = "white")
- r2 = Radiobutton(root, text = "Decripta", value = 1,variable = scelta,bg = "white")
- bt1 = Button(root, text = "Vai",bg = "white", command = main)
- fatto = Label(root, text = "File modificato con successo!!", bg = "white" )
- l1.grid(column = 0, row = 0, columnspan = 2, padx = 10, pady = 10)
- e1.grid(column = 0, row = 1, columnspan = 2, padx = 10, pady = 10)
- l2.grid(column = 0, row = 2, padx = 10, pady = 10)
- s1.grid(column = 1, row = 2, padx = 10, pady = 10)
- r1.grid(column = 0, row = 3, padx = 10, pady = 10)
- r2.grid(column = 1, row = 3, padx = 10, pady = 10)
- bt1.grid(column = 0, row = 4,columnspan = 2, padx = 10, pady = 10)
- fatto.grid_forget()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement