Advertisement
pearos

cifrario di cesare by lokk3d [GUI]

Oct 13th, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. try:
  2.     from tkinter import *
  3. except:
  4.     from Tkinter import *
  5.  
  6. def leggifile(nomefile):
  7.         lines = []
  8.         try:
  9.             miofile = open(nomefile)
  10.             miofile.close()
  11.         except FileNotFoundError:
  12.             fatto.grid(column = 0, row = 5,columnspan = 2, padx = 10, pady = 10)
  13.             fatto.configure(text = "File non trovato, file creato")
  14.         else:
  15.             for l in open(nomefile):
  16.                 lines.append(l.strip())
  17.             return lines
  18.  
  19. def scrivifile(nomefile, lista):
  20.         miofile = open(nomefile, "w")
  21.         for cont in lista:
  22.             miofile.writelines(cont + "\n")
  23.         miofile.close()
  24.         fatto.grid(column = 0, row = 5,columnspan = 2, padx = 10, pady = 10)
  25.  
  26. def codifica(riga, chiave):
  27.     return ''.join([chr(ord(lettera) + chiave) for lettera in riga])
  28.  
  29. def decodifica(riga, chiave):
  30.     return ''.join([chr(ord(lettera) - chiave) for lettera in riga])
  31.  
  32. def main():
  33.     linee = leggifile(nomefile.get())
  34.     nuovofile = []
  35.     if scelta.get() == 0:
  36.         for x in linee:
  37.             stringa = codifica(x,int(chiave.get()))
  38.             nuovofile.append(stringa)
  39.     elif scelta.get() == 1:
  40.         for x in linee:
  41.             stringa = decodifica(x, int(chiave.get()))
  42.             nuovofile.append(stringa)
  43.     scrivifile(nomefile.get(), nuovofile)
  44.  
  45. root = Tk()
  46. root.geometry("350x240")
  47. root.resizable(False, False)
  48. root.title("Cifrario di Cesare")
  49. root.configure(bg = "white")
  50. l1= Label(root,text = "Inserisci il percorso del file:",bg = "white")
  51. nomefile = StringVar(value = "")
  52. e1 = Entry(root, textvariable = nomefile,width= 40,bg = "white")
  53. l2 = Label(root, text = "Inserisci la chiave:",bg = "white")
  54. chiave = StringVar(value = "")
  55. s1 = Spinbox(root, from_= 0,to= 255,width= 10, textvariable = chiave,bg = "white")
  56. scelta = IntVar()
  57. r1 = Radiobutton(root, text = "Cripta", value = 0, variable = scelta,bg = "white")
  58. r2 = Radiobutton(root, text = "Decripta", value = 1,variable = scelta,bg = "white")
  59. bt1 = Button(root, text = "Vai",bg = "white", command = main)
  60. fatto = Label(root, text = "File modificato con successo!!", bg = "white" )
  61.  
  62. l1.grid(column = 0, row = 0, columnspan = 2, padx = 10, pady = 10)
  63. e1.grid(column = 0, row = 1, columnspan = 2, padx = 10, pady = 10)
  64. l2.grid(column = 0, row = 2, padx = 10, pady = 10)
  65. s1.grid(column = 1, row = 2, padx = 10, pady = 10)
  66. r1.grid(column = 0, row = 3, padx = 10, pady = 10)
  67. r2.grid(column = 1, row = 3, padx = 10, pady = 10)
  68. bt1.grid(column = 0, row = 4,columnspan = 2, padx = 10, pady = 10)
  69. fatto.grid_forget()
  70. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement