Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Password Generator
#1
So, I'm still studying the basics of Python 3, but I decided to put some of it in practice and make my own password generator.
It's Python 3 + PyQt5, and it gave me a huge headache to get it done the way I wanted visually. It's my very own Frankenstein, since I've Crtl + C | Crtl + V most of the code, but I'm very proud of the result.

If I can get critics and suggestions, I'd really appreciate. I'd just like to have not only what I should or need to change but also why.

import sys, string, random
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5 import QtWidgets, QtCore


class App(QWidget):
    def __init__(self):
        super().__init__()
        self.title = 'Password Generator'
        self.left = QtWidgets.QDesktopWidget().screenGeometry().width() * 0.1
        self.top = QtWidgets.QDesktopWidget().screenGeometry().height() * 0.1
        self.width = 340
        self.setMinimumSize(380, 170)
        self.setMaximumSize(380, 170)
        # self.height = QtWidgets.QDesktopWidget().screenGeometry().height() * 0.7
        self.setWindowTitle(self.title)
        self.initUI()

    def initUI(self):
        self.btn = QtWidgets.QPushButton("Generate")
        self.btn.setFixedSize(300, 30)
        self.btn2 = QtWidgets.QPushButton("Setup")
        self.btn2.setFixedSize(50, 30)
        self.txtEdit = QtWidgets.QTextEdit()
        self.txtEdit.setFixedHeight(90)
        self.txtEdit.setFontPointSize(16)
        self.txtEdit.setAlignment(QtCore.Qt.AlignCenter)

        self.chk_box1 = QtWidgets.QCheckBox("UPPER")
        self.chk_box2 = QtWidgets.QCheckBox("LOWER")
        self.chk_box3 = QtWidgets.QCheckBox("NUMBERS")
        self.chk_box4 = QtWidgets.QCheckBox("SPECIAL CHARS")
        self.chk_box1.setChecked(True)
        self.chk_box2.setChecked(True)
        self.chk_box3.setChecked(True)
        self.chk_box4.setChecked(True)

        layout = QtWidgets.QVBoxLayout()
        self.layout_1 = QtWidgets.QStackedLayout()
        layout_1_1 = QtWidgets.QHBoxLayout()
        layout_1_2 = QtWidgets.QGridLayout()

        self.frame1 = QtWidgets.QFrame()
        layout_1_1.addWidget(self.txtEdit)
        self.frame1.setLayout(layout_1_1)
        self.layout_1.addWidget(self.frame1)

        self.frame2 = QtWidgets.QFrame()
        self.s1 = QtWidgets.QSlider(QtCore.Qt.Horizontal)
        self.s1.setMinimum(6)
        self.s1.setMaximum(24)
        self.s1.setValue(12)
        self.s1.setPageStep(2)
        self.s1.setTickInterval(6)
        self.s1.setTickPosition(QtWidgets.QSlider.TicksBelow)
        self.lbl = QtWidgets.QLabel(" {} ".format(str(self.s1.value())))
        self.lbl.setStyleSheet("QLabel { background-color : #FFFFFF;}")
        layout_1_2.addWidget(self.chk_box1, 0, 0)
        layout_1_2.addWidget(self.chk_box2, 0, 1)
        layout_1_2.addWidget(self.chk_box3, 0, 2)
        layout_1_2.addWidget(self.chk_box4, 0, 3)
        layout_1_2.addWidget(self.s1, 1, 0, 1, 4)
        layout_1_2.addWidget(self.lbl, 1, 4)
        self.frame2.setLayout(layout_1_2)
        self.layout_1.addWidget(self.frame2)
        layout_1_2.setAlignment(QtCore.Qt.AlignTop)

        layout_2 = QtWidgets.QHBoxLayout()
        layout_2.addWidget(self.btn)
        layout_2.addWidget(self.btn2)
        layout_2.setAlignment(QtCore.Qt.AlignBottom)

        layout.addLayout(self.layout_1)
        layout.addLayout(layout_2)
        self.setLayout(layout)
        self.s1.valueChanged.connect(self.slider_change)
        self.show()

        self.btn2.clicked.connect(lambda: self.layout_1.setCurrentIndex(1))
        self.btn.clicked.connect(self.btn_clicked)

    def slider_change(self):
        self.lbl.setText(" {} ".format(str(self.s1.value())))

    def btn_clicked(self):
        chars = ""
        size = self.s1.value()
        if self.chk_box1.isChecked():
            chars += string.ascii_uppercase
        if self.chk_box2.isChecked():
            chars += string.ascii_lowercase
        if self.chk_box3.isChecked():
            chars += string.digits
        if self.chk_box4.isChecked():
            special_chars = ("!#$/%&*+-:;?@\_")
            chars += special_chars

        temp_value = ""
        for i in range(size):
            temp_value += random.choice(chars)
        self.txtEdit.setText(str(temp_value))
        self.btn.clicked.connect(lambda: self.layout_1.setCurrentIndex(0))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())
Thank you any help,
Patrick
Reply
#2
Hello Pdelnegro..
I appreciate your interest in getting into practice so early before completion of basics.

In general I directly used some of the following codes to generate password.

I'm not sure of your code, but i just want to share if this helps you in any manner. Ignore if it doesn't.

string.ascii_letters
Concatenation of the ascii (upper and lowercase) letters

string.digits
The string '0123456789'.

string.punctuation
String of ASCII characters which are considered punctuation characters in the C
locale.

print string.ascii_letters
print string.digits
print string.punctuation
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  handy password generator ichabod801 4 6,989 Dec-10-2016, 01:33 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020