Creating a Simple Password Generator with python3 and Qt Designer
Every week I take a day to write some program with some language. Today we are going to use Python3 to create a simple random password generator, but this part of the code I will not develop, I found something ready on this guy's github, and of course before making this post I asked his permission.
The idea is to create a program with a basic form, where the interaction will be through buttons and data input. For this we are going to use Qt Designer, in this guy we are going to create the entire program interface and then we will convert this visual into python code and we will implement it. I will not go into details about basic programming things, of course you will need the least to understand this Post.
For this game we will develop within the linux environment, currently my operating system is Pop OS! 22.04, and we will need the following python libraries.
# derivados do Debian (ubuntu, popOS, elementary, mint etc ..)
pip3 install --user pyqt5
sudo apt install python3-pyqt5
sudo apt install pyqt5-dev-tools
sudo apt install qttools5-dev-tools
# derivados do RHEL(fedora, centos etc ..)
pip3 install --user pyqt5
sudo dnf install python3-pyqt5
sudo dnf install pyqt5-dev-tools
sudo dnf install qttools5-dev-tools
Our editor will be the Intellij IDEA Community, but you can use any other like vscode, sublime, geany etc... Let's first create the visual part, open your Qt Designer
On this screen just click on create
In order not to lengthen the explanations this is our ready form, I will just explain the components used.
we added 1 spinBox and in the component property we set the value of 999, don't worry, I'll explain this guy's function in a little while.
then we insert 2 QPushButton, one that will receive the event to generate the passwords via code and the other that will copy this generated password and send it to the operating system's clipboard.
a TLabel component where we will load the image, this image I got from the web
a QPlainTextEdit component that will receive the generated password.
And finally a QLabel component that will receive the password copy status.
After that, let's save in .ui format
To do this, go to the File menu and then Save_AS.
Now let's convert this file to python, see how easy it is. From the terminal I run this command:
path=$(pwd);pyuic5 $path/qtdesigner/principal.ui -o $path/main.py
Ready, we have all the code converted to python
Now let's go to the cool part that is the code, we will need to load these libraries
import random
import string
import sys
from pathlib import Path
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication
root = Path.cwd()
local = f"{root.parent}"
The first code we are going to do is what generates the password, remembering that the owner of this code is Iury Rosal and the code is from this github https://github.com/iuryrosal/projetos-python/tree/main/level- a/05 The other codes are my own, let's implement the python class
So simple that I won't explain it
Now let's create the class that will call the previous class passing parameters
We create a variable called response, which receives the return value of the function, the function needs the character size to generate the password, the value is passed by the text() property of the spinBox component
then we clean the data from the lbl.info component which is a QLabel, its function is to print whether the password was generated or not. And finally, we return to QEdit the value of the response variable, which is the generated password.
If we run our program and click on the generate button, nothing will happen, because we only declare the functions, now we need to create the event that will do this action, in the main code we declare it like this.
self.btnGenerator.clicked.connect(self.generate)
The generate button when pressed calls the function that generates the password and we will have this result
Cool, it's not. and as many times as we click on generate, it will always generate a random value.
ok, but what if we need to copy this generated password to the clipboard? simple, let's implement the event of the clipboard button, see.
First check if the field of the QEditText component is empty, if yes, it will print a value in the label
Notice in the image above that when pressing the clipboard button it returned a message in red, this happened because in the if above we also changed the color of the message via component.
Otherwise, we instantiate the class that will get the result of the QEditText and export it to the desktop, now we change the message color to green and concatenate the password output in Qlabel.
And again, for the button to do the action, let's create the event with the code.
self.btnClipboard.clicked.connect(self.copyToClipboard)
Very simple, after generating it I clicked on the clipboard button and copied it to my notepad, see.
Cool isn't it? I could also use another GUI kit, but of all, in my brief experience, Qt is much more mature, it's no wonder that KDE itself is written with it, well I hope you like it. the repository with the codes can be found here. https://github.com/romeritomorais/creating-a-simple-password-generator-with-python3-and-Qt-designer