I have a GUI which performs the function of file encryption. There are 3 fields and 3 buttons which are interconnected, using them, the user can choose the path from which file to which encryption will occur and also the key to do encryption. And the main button which does the encryption. So what I want to do is to do it so that if the encryption was successful, then the line ''Succes'' would appear, or otherwise '' Error ''.
This is my code :
import tkinter
from tkinter import *
from tkinter import filedialog as fd
from cryptography.fernet import Fernet
root = Tk()
root.iconbitmap('/Users/Rostyslav Levytskyi/Downloads/logo_orange.ico')
root.title('MS4Bike')
root.geometry('800x580')
root.geometry('+200+100')
root.resizable(width=False, height=False)
root.configure(bg='#beb0e8')
# blue top line
color_text_top = Label(
bg='#9900cc',
height=3
)
color_text_top.pack(fill=X)
text_top = Label(
text='Update Generator',
fg='white',
bg='#9900cc',
font='Arial 12'
)
text_top.place(height=40, x=15, y=4)
# first label
label1 = tkinter.Label(
text="Programmodner auswählen(ms4bikeHost)",
bg="#beb0e8",
font=('Arial', 10, 'bold')
)
label1.place(x=25, y=125)
# first button
def open_text_file1():
text_first.delete('1.0', END)
# file type
filetypes = (
('zip files', '*.zip'),
('All files', '*.*')
)
# show the open file dialog
f = fd.askopenfilename(filetypes=filetypes)
text_first.insert('1.0', f)
f = open(f) # or tf = open(tf, 'r')
f.close()
open_button1 = Button(
root,
bd=0,
relief=GROOVE,
text='Ändern',
bg="#c8c4d4",
width=20,
height=2,
command=open_text_file1
).place(x=600, y=147)
# second label
label2 = tkinter.Label(
text="Speicherort für Update-Datei auswählen",
bg="#beb0e8",
font=('Arial', 10, 'bold'),
)
label2.place(x=25, y=250)
# second button
def open_text_file2():
text_second.delete('1.0', END)
# file type
filetypes = (
('text files', '*.txt'),
('All files', '*.*')
)
# show the open file dialog
f = fd.askopenfilename(filetypes=filetypes)
# read the text file and show its content on the Text
text_second.insert('1.0', f)
f = open(f) # or tf = open(tf, 'r')
f.close()
open_button2 = Button(
root,
bd=0,
relief=GROOVE,
text='Ändern',
bg="#c8c4d4",
width=20,
height=2,
command=open_text_file2
).place(x=600, y=272)
# third label
label3 = tkinter.Label(
text="Schlüsseldatel auswählen",
bg="#beb0e8",
font=('Arial', 10, 'bold'),
)
label3.place(x=25, y=375)
# third button
def open_text_file3():
text_third.delete('1.0', END)
# file type
filetypes = (
('key files', '*.key'),
('All files', '*.*')
)
# show the open file dialog
f = fd.askopenfilename(filetypes=filetypes)
# read the text file and show its content on the Text
text_third.insert('1.0', f)
f = open(f) # or tf = open(tf, 'r')
f.close()
open_button3 = Button(
bd=0,
relief=GROOVE,
bg="#c8c4d4",
width=20,
height=2,
text='Ändern',
command=open_text_file3
).place(x=600, y=397)
# file name fields
text_first = Text(
font='Arial 14',
highlightthickness=1,
highlightcolor='black',
highlightbackground='black'
)
text_first.place(x=20, y=150, height=33, width=520)
text_second = Text(
font='Arial 14',
highlightthickness=1,
highlightcolor='black',
highlightbackground='black'
)
text_second.place(x=20, y=275, height=33, width=520)
text_third = Text(
font='Arial 14',
highlightthickness=1,
highlightcolor='black',
highlightbackground='black'
)
text_third.place(x=20, y=400, height=33, width=520)
# functions for update button
def encrypt_file_data(key, data):
fn = Fernet(key)
return fn.encrypt(data)
def encrypt_file(key, source, destination):
with open(source, 'rb') as file:
file_data = file.read()
encrypted = encrypt_file_data(key, file_data)
with open(destination, 'wb') as file:
file.write(encrypted)
# function to get key
def open_content(path):
with open(path) as file:
key = file.read()
return key
# update button
tkinter.Button(
bd=0,
relief=GROOVE,
text='Update erstellen',
bg="#c2d6d6",
width=93,
height=3,
font=('Arial', 10, 'bold'),
command=lambda: encrypt_file(key=open_content(text_third.get('1.0', 'end-1c')),
source=text_first.get('1.0', 'end-1c'),
destination=text_second.get('1.0', 'end-1c'))
).place(x=20, y=490)
tkinter.mainloop()