DEV Community

Discussion on: Append multiple strings to a single QLabel in PyQt5

Collapse
 
jasoncruzdev profile image
Jason Cruz

Hi
Idk if you solved this issue. But I found something that might help ya..


import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QVBoxLayout, QPushButton, QLabel
import random


class App(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Password Generator')
        self.setGeometry(100,100, 400, 400)
        self.intro = QLabel("Welcome to PyPass Gen 2.0")
        self.dialog1 = QInputDialog()
        self.dialog1.setOption(QInputDialog.NoButtons)
        self.dialog1.setLabelText('Number of passwords: ')
        self.dialog2 = QInputDialog()
        self.dialog2.setOption(QInputDialog.NoButtons)
        self.dialog2.setLabelText('Length of passwords: ')
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.intro, alignment=Qt.AlignCenter)
        self.layout.addWidget(self.dialog1)
        self.layout.addWidget(self.dialog2)
        self.layout2 = QVBoxLayout()
        self.layout.addLayout(self.layout2)
        self.textBoxes = []
        self.button1 = QPushButton("Generate Passwords")
        self.button1.clicked.connect(self.execute)
        self.layout.addWidget(self.button1)

        self.setLayout(self.layout)
        self.show()

    def execute(self):
        char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()-_,./?'
        number_v = int(self.dialog1.textValue())
        length_v = int(self.dialog2.textValue())
        self.clearTextBoxes()
        for password in range(number_v):
            self.passwords = ''
            for chars in range(length_v):
                self.passwords += random.choice(char)
                textBox = QLabel(self.passwords)
            print(self.passwords)
            self.textBoxes.append(textBox)
            self.layout2.addWidget(textBox)
            # self.passwords.append(self.textBox)

    def clearTextBoxes(self):
        for textBox in self.textBoxes:
            textBox.setParent(None)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

Enter fullscreen mode Exit fullscreen mode

You guys are genuis to figure all this coding out.. I really want to make an app and sell it. but lately feels like its more trouble then its worth.. Take Care.