Append multiple strings to a single QLabel in PyQt5

Hello everyone!

I’ve been working on this project for a while now, everything is working as should be except the final piece.

Here is my code. The app takes a ‘number of passwords’ input and a ‘length of passwords’ input. When you clicked the ‘generate passwords’ button it generates random passwords that should be appended to my app (self.textBox). Instead of appending all the generated passwords it only appends the last password that was generated in the loop.

Question: If I input ’10’ or ’20’ as my desired number of passwords, how can I get it to append them all in my app?

Thank you everyone!

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.textBox = QLabel()
self.layout.addWidget(self.textBox, alignment=Qt.AlignCenter)
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())
for password in range(number_v):
self.passwords = ''
for chars in range(length_v):
self.passwords += random.choice(char)
self.textBox.setText(self.passwords)
print(self.passwords)
# self.passwords.append(self.textBox)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
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.textBox = QLabel()
        self.layout.addWidget(self.textBox, alignment=Qt.AlignCenter)
        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())
        for password in range(number_v):
            self.passwords = ''
            for chars in range(length_v):
                self.passwords += random.choice(char)

                self.textBox.setText(self.passwords)

            print(self.passwords)
            # self.passwords.append(self.textBox)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())
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.textBox = QLabel() self.layout.addWidget(self.textBox, alignment=Qt.AlignCenter) 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()) for password in range(number_v): self.passwords = '' for chars in range(length_v): self.passwords += random.choice(char) self.textBox.setText(self.passwords) print(self.passwords) # self.passwords.append(self.textBox) if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec_())

Enter fullscreen mode Exit fullscreen mode

原文链接:Append multiple strings to a single QLabel in PyQt5

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
I'm not lazy, I'm on energy saving mode.
我不懒,我只是开启了节能模式
评论 抢沙发

请登录后发表评论

    暂无评论内容