Issue
My code does exactly what I want it to, generates a random password from int inputs through QInputDialog. But instead of getting two small pop-up boxes I want to get one main window that has the QInputDialog fields with the buttons on one window. How can I achieve this?
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit
import random
class App(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Password Generator')
self.setGeometry(100,100, 400, 400)
self.num_pass()
self.length_pass()
self.execute()
self.show()
def num_pass(self):
self.num_value, okPressed = QInputDialog.getInt(self, "Password Amount:","Number:", 0, 1, 50, 1)
if okPressed:
print(self.num_value)
def length_pass(self):
self.len_value, okPressed = QInputDialog.getInt(self, "Password Length: ","Length:", 0, 1, 50, 1)
if okPressed:
print(self.len_value)
def execute(self):
char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()-_,./?'
number = self.num_value
length = self.len_value
for password in range(self.num_value):
passwords = ''
for chars in range(self.len_value):
passwords += random.choice(char)
print(passwords)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Solution
You can add a horizontal layout, and insert a QButton and its 2 QInputDialog. And connect your button to the execute() method.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QVBoxLayout, QPushButton
import random
class App(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Password Generator')
self.setGeometry(100,100, 400, 400)
self.button1 = QPushButton("Press")
self.button1.clicked.connect(self.execute)
self.dialog1 = QInputDialog()
self.dialog1.setOption(QInputDialog.NoButtons)
self.dialog2 = QInputDialog()
self.dialog2.setOption(QInputDialog.NoButtons)
self.layout = QVBoxLayout()
self.layout.addWidget(self.button1)
self.layout.addWidget(self.dialog1)
self.layout.addWidget(self.dialog2)
self.setLayout(self.layout)
self.show()
def execute(self):
char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()-_,./?'
number_v = int(self.dialog1.textValue()) #.getInt(self, "Password Amount:","Number:", 0, 1, 50, 1)
length_v = int(self.dialog2.textValue()) #.getInt(self, "Password Length: ","Length:", 0, 1, 50, 1)
for password in range(number_v):
passwords = ''
for chars in range(length_v):
passwords += random.choice(char)
print(passwords)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Hope this helps.
Answered By - Jonas Vieira de Souza
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.