如何获取一个pyqt5窗口,而不是两个小Qinputdialong窗口

发布于 2025-02-12 20:23:52 字数 1395 浏览 0 评论 0原文

我的代码准确地完成了我想要的功能,通过QinputDialog从INT输入中生成一个随机密码。但是,我没有获得两个小弹出框,我想获得一个主窗口,其中一个带有QinputDialog字段的主窗口上有一个窗口上的按钮。我该如何实现?

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_())

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_())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女七分熟 2025-02-19 20:23:52

您可以添加水平布局,并插入QBUTTON及其2个QinputDialog。并将您的按钮连接到execute()方法。

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_())

希望这会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文