Código fonte Gerador de senhas

Como gerar senhas com python

Fala pessoal, bem vindo ao site da Dev Aprender, copie o código abaixo e faça suas modificações.

Pré-requisitos

  • Instale Python
  • Instale um editor de texto
  • instale a biblioteca playsound pip install playsound
  • instale a biblioteca pysimplegui pip install PySimpleGUI
  • [opcional] Use o tema dracula para quem estiver usando VS Code
# Fala Dev, pronto para aprender a criar seu gerador de senha?
# Vídeo original: https://www.youtube.com/watch?v=ULdrL6fVyts
import random
import PySimpleGUI as sg
import os
from playsound import playsound


class PassGen:
    def __init__(self):
        # Layout
        sg.theme('Black')
        playsound('secret.mp3', block=False)
        layout = [
            [sg.Text('Site/Software', size=(10, 1)),
             sg.Input(key='site', size=(20, 1))],
            [sg.Text('E-mail/Usuário', size=(10, 1)),
             sg.Input(key='usuario', size=(20, 1))],
            [sg.Text('Quantidade de caracteres'), sg.Combo(values=list(
                range(30)), key='total_chars', default_value=1, size=(3, 1))],
            [sg.Output(size=(32, 5))],
            [sg.Button('Gerar Senha')]
        ]
        # Declarar janela
        self.janela = sg.Window('Password Generator', layout)

    def Iniciar(self):
        while True:
            evento, valores = self.janela.read()
            if evento == sg.WINDOW_CLOSED:
                break
            if evento == 'Gerar Senha':
                nova_senha = self.gerar_senha(valores)
                print(nova_senha)
                self.salvar_senha(nova_senha, valores)

    def gerar_senha(self, valores):
        char_list = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%¨&*'
        chars = random.choices(char_list, k=int(valores['total_chars']))
        new_pass = ''.join(chars)
        return new_pass

    def salvar_senha(self, nova_senha, valores):
        with open('senhas.txt', 'a', newline='') as arquivo:
            arquivo.write(
                f"site: {valores['site']}, usuario: {valores['usuario']}, nova senha: {nova_senha}")

        print('Arquivo salvo')


gen = PassGen()
gen.Iniciar()

Se vale a pena, você deve aprender!

Curtiu esse projeto?
Peço que compartilhe o vídeo com seu colega(link abaixo)
https://www.youtube.com/watch?v=ULdrL6fVyts

1 comentário em “Código fonte Gerador de senhas”

  1. O meu está dando este erro ao dar play no python, no vscode..

    Windows PowerShell

    Experimente a nova plataforma cruzada PowerShell https://aka.ms/pscore6

    PS D:\Documentos\Web Curso> & C:/Users/Munir/AppData/Local/Programs/Python/Python38-32/python.exe “d:/Documentos/Projeto gerador de senhas/password_gen.py” e “d:/Documentos/
    sg.Text(‘E-mail/Usuario’ ,size=(10,1)),
    ^
    IndentationError: unexpected indent
    PS D:\Documentos\Web Curso> & C:/Users/Munir/AppData/Local/Programs/Python/Python38-32/python.exe “d:/Documentos/Projeto gerador de senhas/password_gen.py”
    sg.Text(‘E-mail/Usuario’ ,size=(10, 1)),
    ^
    IndentationError: unexpected indent
    PS D:\Documentos\Web Curso> & C:/Users/Munir/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/Munir/Desktop/password_gen.py
    File “c:/Users/Munir/Desktop/password_gen.py”, line 13
    sg.Text(‘E-mail/Usuario’ ,size=(10, 1)),
    ^
    PS D:\Documentos\Web Curso> & C:/Users/Munir/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/Munir/Desktop/password_gen.py
    c:/Users/Munir/Desktop/password_gen.py:13: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a comma?
    [sg.Text(‘E-mail/Usuario’, size=(10, 1)),
    File “c:/Users/Munir/Desktop/password_gen.py”, line 2, in
    import PySimpleGUI as sg
    ModuleNotFoundError: No module named ‘PySimpleGUI’
    PS D:\Documentos\Web Curso> & C:/Users/Munir/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/Munir/Desktop/password_gen.py
    File “c:/Users/Munir/Desktop/password_gen.py”, line 2, in
    import PySimpleGUI as sg
    ModuleNotFoundError: No module named ‘PySimpleGUI’
    PS D:\Documentos\Web Curso> & C:/Users/Munir/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/Munir/Desktop/password_gen.py
    File “c:/Users/Munir/Desktop/password_gen.py”, line 2, in
    import PySimpleGUI as sg
    ModuleNotFoundError: No module named ‘PySimpleGUI’
    PS D:\Documentos\Web Curso> & C:/Users/Munir/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/Munir/Desktop/password_gen.py
    File “c:/Users/Munir/Desktop/password_gen.py”, line 2, in
    import PySimpleGUI as sg
    ModuleNotFoundError: No module named ‘PySimpleGUI’
    PS D:\Documentos\Web Curso> & C:/Users/Munir/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/Munir/Desktop/password_gen.py
    Traceback (most recent call last):
    File “c:/Users/Munir/Desktop/password_gen.py”, line 2, in
    import PySimpleGUI as sg
    PS D:\Documentos\Web Curso> & C:/Users/Munir/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/Munir/Desktop/password_gen.py
    Traceback (most recent call last):
    File “c:/Users/Munir/Desktop/password_gen.py”, line 2, in
    import PySimpleGUI as sg
    ModuleNotFoundError: No module named ‘PySimpleGUI’
    PS D:\Documentos\Web Curso> & C:/Users/Munir/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/Munir/Desktop/password_gen.py
    Traceback (most recent call last):
    File “c:/Users/Munir/Desktop/password_gen.py”, line 2, in
    import PySimpleGUI as sg
    ModuleNotFoundError: No module named ‘PySimpleGUI’
    PS D:\Documentos\Web Curso> & C:/Users/Munir/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/Munir/Desktop/password_gen.py
    Traceback (most recent call last):
    File “c:/Users/Munir/Desktop/password_gen.py”, line 2, in
    import PySimpleGUI as sg
    ModuleNotFoundError: No module named ‘PySimpleGUI’
    PS D:\Documentos\Web Curso>

Deixe uma resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *