Advertisement
Guest User

Untitled

a guest
Jun 16th, 2021
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import messagebox
  3. from PIL import ImageTk
  4. import random
  5. import pyperclip
  6. from tkinter import ttk
  7. import settings
  8.  
  9. # ------------- Настройки окна -------------
  10.  
  11. root = Tk()
  12. root.title("Генератор паролей") # Название окна
  13. root.geometry("400x300") # Размер окна
  14. root.iconbitmap(settings.ICON) # Иконка окна
  15. root.resizable(width=False, height=False) # Неизменяемость размера окна
  16.  
  17. C = Canvas(bg="blue", height=250, width=300) # <---
  18. filename = PhotoImage(file=settings.BACKGROUND) # Фон окна
  19. background_label = Label(image=filename)    # <---
  20. background_label.place(x=0, y=0, relwidth=1, relheight=1)   # <---
  21. hex_value = '#6dd5fa'
  22.  
  23. # ------------------------------------------
  24.  
  25.  
  26. # ------------- Logic ----------------------
  27.  
  28. show_pswrd = StringVar()
  29.  
  30.  
  31. # Генерация пароля
  32.  
  33. def create_pswrd():
  34.     global password
  35.     chars = '~!@#$%^&*()+;:<>/|qQwWeErRtTyYuUiIoOpPaAsSdDfFgGhHjJkKlLzZxXcCvVbBnNmM?1234567890'
  36.     words = chars.split()
  37.     for i, word in enumerate(map(list, words)):
  38.         random.shuffle(word)
  39.         words[i] = ''.join(word)
  40.     return *words
  41.  
  42.     password = ''
  43.     length = entry_length_psswrd.get()
  44.  
  45.     for i in range(int(length)):
  46.         password += random.choice(chars)
  47.  
  48.     show_pswrd.set(password)
  49.  
  50.  
  51. # Копирование файла
  52.  
  53. def copy():
  54.     copy_pswrd = show_pswrd.get()
  55.     pyperclip.copy(copy_pswrd)
  56.  
  57.  
  58. # Сохранение пароля в отдельный файл
  59.  
  60. def save_file():
  61.     global directory_folder
  62.     global why
  63.     directory_folder = settings.PASSWORDS
  64.     why = entry_why_pswrd.get()
  65.  
  66.     if why == '':
  67.         save_without_name()
  68.     if why != '':
  69.         save_with_name()
  70.  
  71.  
  72. # Если пользователь ввёл для чего пароль
  73.  
  74. def save_with_name():
  75.     with open(directory_folder, 'a', encoding='utf-8') as f:
  76.         f.write(why + ': ' + str(password) + '\n')
  77.  
  78.  
  79. # Если пользователь НЕ ввёл для чего пароль
  80.  
  81. def save_without_name():
  82.     with open(directory_folder, 'a', encoding='utf-8') as f:
  83.         f.write('Пароль: ' + str(password) + '\n')
  84.  
  85.  
  86. # Запускаем
  87.  
  88. def run():
  89.     try:
  90.         create_pswrd()
  91.         save_file()
  92.         pswrd_ok = messagebox.showinfo("Успешно", "Пароль сгенерирован успешно!") # Всё хорошо, сохраняем в файл
  93.  
  94.     except:
  95.         pswrd_error = messagebox.showerror("Ошибка", "Что-то пошло не так...") # Ошибка, не сохраняем в файл
  96.  
  97. # ------------------------------------------
  98.  
  99.  
  100. # ------------- Button, Label, Entry -------------
  101.  
  102. Label(root, text="Введите длину пароля", font="Whitney-Book 13", bg=hex_value).pack(pady=5)
  103. entry_length_psswrd = ttk.Entry(root, width=20)
  104. entry_length_psswrd.pack(pady=3)
  105.  
  106. Label(root, text="(Необязательно) Для чего нужен пароль?", font="Whitney-Book 13", bg=hex_value).pack(pady=5)
  107. entry_why_pswrd = ttk.Entry(root, width=20)
  108. entry_why_pswrd.pack(pady=3)
  109.  
  110. ttk.Button(root, text="Сгенерировать", command=run).pack(pady=10)
  111.  
  112. ttk.Entry(root, textvariable=show_pswrd).pack(pady=5)
  113. ttk.Button(root, text='Скопировать', command=copy).pack(pady=10)
  114.  
  115. # ------------------------------------------------
  116.  
  117.  
  118. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement