Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter import messagebox
- from PIL import ImageTk
- import random
- import pyperclip
- from tkinter import ttk
- import settings
- # ------------- Настройки окна -------------
- root = Tk()
- root.title("Генератор паролей") # Название окна
- root.geometry("400x300") # Размер окна
- root.iconbitmap(settings.ICON) # Иконка окна
- root.resizable(width=False, height=False) # Неизменяемость размера окна
- C = Canvas(bg="blue", height=250, width=300) # <---
- filename = PhotoImage(file=settings.BACKGROUND) # Фон окна
- background_label = Label(image=filename) # <---
- background_label.place(x=0, y=0, relwidth=1, relheight=1) # <---
- hex_value = '#6dd5fa'
- # ------------------------------------------
- # ------------- Logic ----------------------
- show_pswrd = StringVar()
- # Генерация пароля
- def create_pswrd():
- global password
- chars = '~!@#$%^&*()+;:<>/|qQwWeErRtTyYuUiIoOpPaAsSdDfFgGhHjJkKlLzZxXcCvVbBnNmM?1234567890'
- words = chars.split()
- for i, word in enumerate(map(list, words)):
- random.shuffle(word)
- words[i] = ''.join(word)
- return *words
- password = ''
- length = entry_length_psswrd.get()
- for i in range(int(length)):
- password += random.choice(chars)
- show_pswrd.set(password)
- # Копирование файла
- def copy():
- copy_pswrd = show_pswrd.get()
- pyperclip.copy(copy_pswrd)
- # Сохранение пароля в отдельный файл
- def save_file():
- global directory_folder
- global why
- directory_folder = settings.PASSWORDS
- why = entry_why_pswrd.get()
- if why == '':
- save_without_name()
- if why != '':
- save_with_name()
- # Если пользователь ввёл для чего пароль
- def save_with_name():
- with open(directory_folder, 'a', encoding='utf-8') as f:
- f.write(why + ': ' + str(password) + '\n')
- # Если пользователь НЕ ввёл для чего пароль
- def save_without_name():
- with open(directory_folder, 'a', encoding='utf-8') as f:
- f.write('Пароль: ' + str(password) + '\n')
- # Запускаем
- def run():
- try:
- create_pswrd()
- save_file()
- pswrd_ok = messagebox.showinfo("Успешно", "Пароль сгенерирован успешно!") # Всё хорошо, сохраняем в файл
- except:
- pswrd_error = messagebox.showerror("Ошибка", "Что-то пошло не так...") # Ошибка, не сохраняем в файл
- # ------------------------------------------
- # ------------- Button, Label, Entry -------------
- Label(root, text="Введите длину пароля", font="Whitney-Book 13", bg=hex_value).pack(pady=5)
- entry_length_psswrd = ttk.Entry(root, width=20)
- entry_length_psswrd.pack(pady=3)
- Label(root, text="(Необязательно) Для чего нужен пароль?", font="Whitney-Book 13", bg=hex_value).pack(pady=5)
- entry_why_pswrd = ttk.Entry(root, width=20)
- entry_why_pswrd.pack(pady=3)
- ttk.Button(root, text="Сгенерировать", command=run).pack(pady=10)
- ttk.Entry(root, textvariable=show_pswrd).pack(pady=5)
- ttk.Button(root, text='Скопировать', command=copy).pack(pady=10)
- # ------------------------------------------------
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement