pyqt5条件下关闭窗口

发布于 2025-02-04 20:55:18 字数 1877 浏览 2 评论 0原文

我没有找到任何答案,所以我在这里问这个问题。我创建了2个简化的文件来说明我的问题。第一个是:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys


class Ui_Login(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.setFixedSize(196, 134)

        self.button = QtWidgets.QPushButton(Dialog)
        self.button.setGeometry(QtCore.QRect(10, 100, 61, 21))
        self.button.setObjectName("Button")

        self.button.clicked.connect(self.action)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Login"))
        self.button.setText(_translate("Dialog", "Button"))

    def action(self):
        condition1 = True
        condition2 = True
        condition3 = True
        if condition1 == True and condition2 == True and condition3 == True:
            # Close the window
            Dialog.close()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle("Fusion")
    Dialog = QtWidgets.QDialog()
    ui = Ui_Login()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

第二个文件是:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from demo import Ui_Login # Importing the UI of the other file


app = QtWidgets.QApplication(sys.argv)
app.setStyle("Fusion")
Dialog = QtWidgets.QDialog()
ui = Ui_Login()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())

我想仅在满足某些标准时单击按钮来关闭窗口(如果条件1 == true and andition 2 == true and condition3 == true:> )。如果我只运行第一个文件,它可以完全工作,但是当我通过运行第二个文件(访问第一个文件)尝试时,我会收到错误:第30行,在Action Dialog.close.close.close.close.close()nameError:name name name。 “对话框”不是定义的

我必须通过第二个文件启动我的应用程序,而不是第一个文件(我忘了提及)。 我该如何解决?预先感谢您回答!

I didn't find any answer so I ask this question here. I created 2 simplified files to illustrate my problem. First one is:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys


class Ui_Login(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.setFixedSize(196, 134)

        self.button = QtWidgets.QPushButton(Dialog)
        self.button.setGeometry(QtCore.QRect(10, 100, 61, 21))
        self.button.setObjectName("Button")

        self.button.clicked.connect(self.action)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Login"))
        self.button.setText(_translate("Dialog", "Button"))

    def action(self):
        condition1 = True
        condition2 = True
        condition3 = True
        if condition1 == True and condition2 == True and condition3 == True:
            # Close the window
            Dialog.close()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle("Fusion")
    Dialog = QtWidgets.QDialog()
    ui = Ui_Login()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

Second file is:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from demo import Ui_Login # Importing the UI of the other file


app = QtWidgets.QApplication(sys.argv)
app.setStyle("Fusion")
Dialog = QtWidgets.QDialog()
ui = Ui_Login()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())

I would like to close the window by clicking on a button only when some criterias are met (if condition1 == True and condition2 == True and condition3 == True:). If I only run the first file, it perfectly works but when I try by running the second file (which accesses to the first one), I get the error: line 30, in action Dialog.close() NameError: name 'Dialog' is not defined

I must launch my app by the second file and not the first one (I forgot to mention that).
How can I fix that? Thank you in advance for you answer!

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

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

发布评论

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

评论(1

时光与爱终年不遇 2025-02-11 20:55:18

我对您的原始代码进行了一些调整,以适应您的目标,并进行一些较小的调整,以更好地符合惯例并使设置变得更加容易。

现在,从login.py文件中只有一个入口点。如果您想打开注册窗口作为独立对话框,您需要做的就是在login.py.py.py文件的底部取消注册。

现在,除非用户单击取消按钮或满足所有三个条件,否则“注册”对话框将无法关闭。

我认为,与将其存储在文本文件中相比,可能有一个更好且更少的错误方式来存储,阅读和编​​辑新用户和密码,但是如果您有兴趣,这可能是一个不同的问题。

login.py

import os
from PyQt5 import QtCore, QtGui, QtWidgets
from signup import SignupDialog
import sys

class LoginDialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.setupUi()

    def setupUi(self):
        self.setObjectName("Login_Dialog")
        self.setFixedSize(196, 134)
        font = QtGui.QFont()
        font.setFamily("Segoe UI")
        self.username = QtWidgets.QLabel(self)
        self.username.setGeometry(QtCore.QRect(0, 20, 71, 20))
        self.username.setFont(font)
        self.username.setAlignment(QtCore.Qt.AlignCenter)
        self.username.setObjectName("username")

        self.password = QtWidgets.QLabel(parent=self)
        self.password.setGeometry(QtCore.QRect(0, 60, 71, 20))
        self.password.setFont(font)
        self.password.setAlignment(QtCore.Qt.AlignCenter)
        self.password.setObjectName("password")

        self.username_line = QtWidgets.QLineEdit(parent=self)
        self.username_line.setGeometry(QtCore.QRect(67, 20, 113, 20))
        self.username_line.setObjectName("username_line")
        self.password_line = QtWidgets.QLineEdit(parent=self)
        self.password_line.setGeometry(QtCore.QRect(67, 60, 113, 20))
        self.password_line.setObjectName("password_line")
        self.password_line.setEchoMode(QtWidgets.QLineEdit.Password)  # To show text as dots

        self.signup_button = QtWidgets.QPushButton(parent=self)
        self.signup_button.setGeometry(QtCore.QRect(10, 100, 61, 21))
        self.signup_button.setObjectName("signup_button")

        self.login_button = QtWidgets.QPushButton(parent=self)
        self.login_button.setGeometry(QtCore.QRect(85, 100, 91, 21))
        self.login_button.setFont(font)
        self.login_button.setObjectName("login_button")

        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)

        self.invalid_username = QtWidgets.QLabel(parent=self)
        self.invalid_username.setGeometry(QtCore.QRect(-3, 40, 181, 20))
        self.invalid_username.setPalette(palette)
        self.invalid_username.setText("")
        self.invalid_username.setAlignment(QtCore.Qt.AlignCenter)
        self.invalid_username.setObjectName("invalid_username")

        self.invalid_password = QtWidgets.QLabel(parent=self)
        self.invalid_password.setGeometry(QtCore.QRect(-3, 80, 181, 20))
        self.invalid_password.setPalette(palette)
        self.invalid_password.setText("")
        self.invalid_password.setAlignment(QtCore.Qt.AlignCenter)
        self.invalid_password.setObjectName("invalid_password?")

        self.login_button.clicked.connect(self.login)
        self.signup_button.clicked.connect(self.signup)
        self.login_button.setDefault(True)
        self.username.setFocus()

        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)

    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("Login_Dialog", "Login"))
        self.password.setText(_translate("Login_Dialog", "Password"))
        self.username.setText(_translate("Login_Dialog", "Username"))
        self.signup_button.setText(_translate("Login_Dialog", "Sign Up"))
        self.login_button.setText(_translate("Login_Dialog", "Login"))

    def login(self):
        if os.path.exists("accounts.txt"):
            accounts = {}
            with open("accounts.txt", "rt") as a:
                for line in a:
                    if not line.strip(): continue
                    (key, value) = line.split()
                    accounts[key] = value
        username = self.username_line.text()
        password = self.password_line.text()
        # Check if username and password are valid
        if username == "":
            self.invalid_username.setText("Please enter a username")
            return self.invalid_password.setText("")
        if username in accounts.keys():
            self.invalid_username.setText("")
            if password == "":
                return self.invalid_password.setText("Please enter a password")
            if password == accounts[username]:
                self.invalid_password.setText("")
                print("success")
            else:
                return self.invalid_password.setText("Invalid password")
        else:
            return self.invalid_username.setText("Invalid username")

    def showWidget(self):
        self.setHidden(False)

    # Open signup window
    def signup(self):
        self.signupDialog = SignupDialog()
        self.signupDialog.exited.connect(self.showWidget)
        self.signupDialog.show()
        self.setHidden(True)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle("Fusion")
    loginDialog = LoginDialog()
    loginDialog.show()    
    # signupDialog = SignupDialog()
    # signupDialog.show()
    sys.exit(app.exec_())

Ingip.py

import os
from PyQt5 import QtCore, QtGui, QtWidgets


class SignupDialog(QtWidgets.QDialog):

    exited = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.setupUi()

    def setupUi(self):
        self.setObjectName("Signup_Dialog")
        self.setFixedSize(251, 179)
        self.create_button = QtWidgets.QPushButton(parent=self)
        self.create_button.setGeometry(QtCore.QRect(140, 140, 91, 21))
        font = QtGui.QFont()
        font.setFamily("Segoe UI")
        font.setPointSize(8)
        self.create_button.setFont(font)
        self.create_button.setObjectName("create_button")

        self.cancel_button = QtWidgets.QPushButton(parent=self)
        self.cancel_button.setGeometry(QtCore.QRect(30, 140, 91, 21))
        self.cancel_button.setFont(font)
        self.cancel_button.setObjectName("cancel_button")

        self.username = QtWidgets.QLabel(parent=self)
        self.username.setGeometry(QtCore.QRect(10, 20, 101, 20))
        self.username.setFont(font)
        self.username.setAlignment(QtCore.Qt.AlignCenter)
        self.username.setObjectName("username")

        self.password = QtWidgets.QLabel(parent=self)
        self.password.setGeometry(QtCore.QRect(10, 60, 101, 20))
        self.password.setFont(font)
        self.password.setAlignment(QtCore.Qt.AlignCenter)
        self.password.setObjectName("password")

        self.confirm_password = QtWidgets.QLabel(parent=self)
        self.confirm_password.setGeometry(QtCore.QRect(10, 100, 101, 20))
        self.confirm_password.setFont(font)
        self.confirm_password.setAlignment(QtCore.Qt.AlignCenter)
        self.confirm_password.setObjectName("confirm_password")

        self.username_line = QtWidgets.QLineEdit(parent=self)
        self.username_line.setGeometry(QtCore.QRect(120, 20, 121, 20))
        self.username_line.setObjectName("username_line")

        self.password_line = QtWidgets.QLineEdit(parent=self)
        self.password_line.setGeometry(QtCore.QRect(120, 60, 121, 20))
        self.password_line.setText("")
        self.password_line.setObjectName("password_line")
        self.password_line.setEchoMode(QtWidgets.QLineEdit.Password)

        self.confirm_password_line = QtWidgets.QLineEdit(parent=self)
        self.confirm_password_line.setGeometry(QtCore.QRect(120, 100, 121, 20))
        self.confirm_password_line.setText("")
        self.confirm_password_line.setObjectName("confirm_password_line")
        self.confirm_password_line.setEchoMode(QtWidgets.QLineEdit.Password)

        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)

        self.taken_username = QtWidgets.QLabel(parent=self)
        self.taken_username.setGeometry(QtCore.QRect(120, 35, 121, 20))
        self.taken_username.setPalette(palette)
        self.taken_username.setText("")
        self.taken_username.setAlignment(QtCore.Qt.AlignCenter)
        self.taken_username.setObjectName("taken_username")

        self.invalid_criteria_password = QtWidgets.QLabel(parent=self)
        self.invalid_criteria_password.setGeometry(QtCore.QRect(120, 75, 121, 20))
        self.invalid_criteria_password.setPalette(palette)
        self.invalid_criteria_password.setText("")
        self.invalid_criteria_password.setAlignment(QtCore.Qt.AlignCenter)
        self.invalid_criteria_password.setObjectName("invalid_criteria_password")

        self.invalid_notsame_password = QtWidgets.QLabel(parent=self)
        self.invalid_notsame_password.setGeometry(QtCore.QRect(120, 115, 121, 20))
        self.invalid_notsame_password.setPalette(palette)
        self.invalid_notsame_password.setText("")
        self.invalid_notsame_password.setAlignment(QtCore.Qt.AlignCenter)
        self.invalid_notsame_password.setObjectName("invalid_notsame_password")

        self.cancel_button.clicked.connect(self.cancel)
        self.create_button.clicked.connect(self.create)
        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)

    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("Signup_Dialog", "Sign Up"))
        self.create_button.setText(_translate("Signup_Dialog", "Create"))
        self.confirm_password.setText(_translate("Signup_Dialog", "Confirm Password"))
        self.password.setText(_translate("Signup_Dialog", "Create Password"))
        self.username.setText(_translate("Signup_Dialog", "Create Username"))
        self.cancel_button.setText(_translate("Signup_Dialog", "Cancel"))

    def cancel(self):
        self.exited.emit()

    def create(self):
        # Create dictionary with usernames and passwords
        accounts = {}
        if os.path.exists("accounts.txt"):
            with open("accounts.txt", "rt") as a:
                for line in a:
                    if not line.strip(): continue
                    (key, value) = line.split()
                    accounts[key] = value
        username = self.username_line.text()
        password = self.password_line.text()
        confirm_password = self.confirm_password_line.text()
        # Check if username is already taken
        if username in accounts:
            self.taken_username.setText("Username already taken")
            return self.invalid_criteria_password.setText("")
        if username == "":
            self.taken_username.setText("Enter a username")
            return self.invalid_criteria_password.setText("")
        self.taken_username.setText("")
        # Check if password is 8-28 characters
        if len(password) < 8:
            return self.invalid_criteria_password.setText("8 characters min")
        if len(password) > 28:
            return self.invalid_criteria_password.setText("28 characters max")
        self.invalid_criteria_password.setText("")
        if password != confirm_password:
            return self.invalid_notsame_password.setText("Confirm your password")
        self.invalid_notsame_password.setText("")
        with open("accounts.txt", "a") as i:
            i.writelines(f"\n{username} {password}")
        self.exited.emit()
        self.close()

I made some adjustments to you original code to accommodate what your goal was as well as make some minor tweaks that better conform to convention and make setup much easier.

Now there is only one entry point from the login.py file. If you wanted to open the Signup window as a standalone dialog all you need to do is uncomment the 2 lines at the bottom of the login.py file.

Now the signup dialog will not close unless the user either clicks the cancel button or all three of the criteria have been met.

I think there is probably a much better and less error prone way of storing, reading, and editing new users and passwords than storing them in a text file, but maybe that can be a different question if you are interested.

login.py

import os
from PyQt5 import QtCore, QtGui, QtWidgets
from signup import SignupDialog
import sys

class LoginDialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.setupUi()

    def setupUi(self):
        self.setObjectName("Login_Dialog")
        self.setFixedSize(196, 134)
        font = QtGui.QFont()
        font.setFamily("Segoe UI")
        self.username = QtWidgets.QLabel(self)
        self.username.setGeometry(QtCore.QRect(0, 20, 71, 20))
        self.username.setFont(font)
        self.username.setAlignment(QtCore.Qt.AlignCenter)
        self.username.setObjectName("username")

        self.password = QtWidgets.QLabel(parent=self)
        self.password.setGeometry(QtCore.QRect(0, 60, 71, 20))
        self.password.setFont(font)
        self.password.setAlignment(QtCore.Qt.AlignCenter)
        self.password.setObjectName("password")

        self.username_line = QtWidgets.QLineEdit(parent=self)
        self.username_line.setGeometry(QtCore.QRect(67, 20, 113, 20))
        self.username_line.setObjectName("username_line")
        self.password_line = QtWidgets.QLineEdit(parent=self)
        self.password_line.setGeometry(QtCore.QRect(67, 60, 113, 20))
        self.password_line.setObjectName("password_line")
        self.password_line.setEchoMode(QtWidgets.QLineEdit.Password)  # To show text as dots

        self.signup_button = QtWidgets.QPushButton(parent=self)
        self.signup_button.setGeometry(QtCore.QRect(10, 100, 61, 21))
        self.signup_button.setObjectName("signup_button")

        self.login_button = QtWidgets.QPushButton(parent=self)
        self.login_button.setGeometry(QtCore.QRect(85, 100, 91, 21))
        self.login_button.setFont(font)
        self.login_button.setObjectName("login_button")

        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)

        self.invalid_username = QtWidgets.QLabel(parent=self)
        self.invalid_username.setGeometry(QtCore.QRect(-3, 40, 181, 20))
        self.invalid_username.setPalette(palette)
        self.invalid_username.setText("")
        self.invalid_username.setAlignment(QtCore.Qt.AlignCenter)
        self.invalid_username.setObjectName("invalid_username")

        self.invalid_password = QtWidgets.QLabel(parent=self)
        self.invalid_password.setGeometry(QtCore.QRect(-3, 80, 181, 20))
        self.invalid_password.setPalette(palette)
        self.invalid_password.setText("")
        self.invalid_password.setAlignment(QtCore.Qt.AlignCenter)
        self.invalid_password.setObjectName("invalid_password?")

        self.login_button.clicked.connect(self.login)
        self.signup_button.clicked.connect(self.signup)
        self.login_button.setDefault(True)
        self.username.setFocus()

        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)

    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("Login_Dialog", "Login"))
        self.password.setText(_translate("Login_Dialog", "Password"))
        self.username.setText(_translate("Login_Dialog", "Username"))
        self.signup_button.setText(_translate("Login_Dialog", "Sign Up"))
        self.login_button.setText(_translate("Login_Dialog", "Login"))

    def login(self):
        if os.path.exists("accounts.txt"):
            accounts = {}
            with open("accounts.txt", "rt") as a:
                for line in a:
                    if not line.strip(): continue
                    (key, value) = line.split()
                    accounts[key] = value
        username = self.username_line.text()
        password = self.password_line.text()
        # Check if username and password are valid
        if username == "":
            self.invalid_username.setText("Please enter a username")
            return self.invalid_password.setText("")
        if username in accounts.keys():
            self.invalid_username.setText("")
            if password == "":
                return self.invalid_password.setText("Please enter a password")
            if password == accounts[username]:
                self.invalid_password.setText("")
                print("success")
            else:
                return self.invalid_password.setText("Invalid password")
        else:
            return self.invalid_username.setText("Invalid username")

    def showWidget(self):
        self.setHidden(False)

    # Open signup window
    def signup(self):
        self.signupDialog = SignupDialog()
        self.signupDialog.exited.connect(self.showWidget)
        self.signupDialog.show()
        self.setHidden(True)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle("Fusion")
    loginDialog = LoginDialog()
    loginDialog.show()    
    # signupDialog = SignupDialog()
    # signupDialog.show()
    sys.exit(app.exec_())

signup.py

import os
from PyQt5 import QtCore, QtGui, QtWidgets


class SignupDialog(QtWidgets.QDialog):

    exited = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.setupUi()

    def setupUi(self):
        self.setObjectName("Signup_Dialog")
        self.setFixedSize(251, 179)
        self.create_button = QtWidgets.QPushButton(parent=self)
        self.create_button.setGeometry(QtCore.QRect(140, 140, 91, 21))
        font = QtGui.QFont()
        font.setFamily("Segoe UI")
        font.setPointSize(8)
        self.create_button.setFont(font)
        self.create_button.setObjectName("create_button")

        self.cancel_button = QtWidgets.QPushButton(parent=self)
        self.cancel_button.setGeometry(QtCore.QRect(30, 140, 91, 21))
        self.cancel_button.setFont(font)
        self.cancel_button.setObjectName("cancel_button")

        self.username = QtWidgets.QLabel(parent=self)
        self.username.setGeometry(QtCore.QRect(10, 20, 101, 20))
        self.username.setFont(font)
        self.username.setAlignment(QtCore.Qt.AlignCenter)
        self.username.setObjectName("username")

        self.password = QtWidgets.QLabel(parent=self)
        self.password.setGeometry(QtCore.QRect(10, 60, 101, 20))
        self.password.setFont(font)
        self.password.setAlignment(QtCore.Qt.AlignCenter)
        self.password.setObjectName("password")

        self.confirm_password = QtWidgets.QLabel(parent=self)
        self.confirm_password.setGeometry(QtCore.QRect(10, 100, 101, 20))
        self.confirm_password.setFont(font)
        self.confirm_password.setAlignment(QtCore.Qt.AlignCenter)
        self.confirm_password.setObjectName("confirm_password")

        self.username_line = QtWidgets.QLineEdit(parent=self)
        self.username_line.setGeometry(QtCore.QRect(120, 20, 121, 20))
        self.username_line.setObjectName("username_line")

        self.password_line = QtWidgets.QLineEdit(parent=self)
        self.password_line.setGeometry(QtCore.QRect(120, 60, 121, 20))
        self.password_line.setText("")
        self.password_line.setObjectName("password_line")
        self.password_line.setEchoMode(QtWidgets.QLineEdit.Password)

        self.confirm_password_line = QtWidgets.QLineEdit(parent=self)
        self.confirm_password_line.setGeometry(QtCore.QRect(120, 100, 121, 20))
        self.confirm_password_line.setText("")
        self.confirm_password_line.setObjectName("confirm_password_line")
        self.confirm_password_line.setEchoMode(QtWidgets.QLineEdit.Password)

        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)

        self.taken_username = QtWidgets.QLabel(parent=self)
        self.taken_username.setGeometry(QtCore.QRect(120, 35, 121, 20))
        self.taken_username.setPalette(palette)
        self.taken_username.setText("")
        self.taken_username.setAlignment(QtCore.Qt.AlignCenter)
        self.taken_username.setObjectName("taken_username")

        self.invalid_criteria_password = QtWidgets.QLabel(parent=self)
        self.invalid_criteria_password.setGeometry(QtCore.QRect(120, 75, 121, 20))
        self.invalid_criteria_password.setPalette(palette)
        self.invalid_criteria_password.setText("")
        self.invalid_criteria_password.setAlignment(QtCore.Qt.AlignCenter)
        self.invalid_criteria_password.setObjectName("invalid_criteria_password")

        self.invalid_notsame_password = QtWidgets.QLabel(parent=self)
        self.invalid_notsame_password.setGeometry(QtCore.QRect(120, 115, 121, 20))
        self.invalid_notsame_password.setPalette(palette)
        self.invalid_notsame_password.setText("")
        self.invalid_notsame_password.setAlignment(QtCore.Qt.AlignCenter)
        self.invalid_notsame_password.setObjectName("invalid_notsame_password")

        self.cancel_button.clicked.connect(self.cancel)
        self.create_button.clicked.connect(self.create)
        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)

    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("Signup_Dialog", "Sign Up"))
        self.create_button.setText(_translate("Signup_Dialog", "Create"))
        self.confirm_password.setText(_translate("Signup_Dialog", "Confirm Password"))
        self.password.setText(_translate("Signup_Dialog", "Create Password"))
        self.username.setText(_translate("Signup_Dialog", "Create Username"))
        self.cancel_button.setText(_translate("Signup_Dialog", "Cancel"))

    def cancel(self):
        self.exited.emit()

    def create(self):
        # Create dictionary with usernames and passwords
        accounts = {}
        if os.path.exists("accounts.txt"):
            with open("accounts.txt", "rt") as a:
                for line in a:
                    if not line.strip(): continue
                    (key, value) = line.split()
                    accounts[key] = value
        username = self.username_line.text()
        password = self.password_line.text()
        confirm_password = self.confirm_password_line.text()
        # Check if username is already taken
        if username in accounts:
            self.taken_username.setText("Username already taken")
            return self.invalid_criteria_password.setText("")
        if username == "":
            self.taken_username.setText("Enter a username")
            return self.invalid_criteria_password.setText("")
        self.taken_username.setText("")
        # Check if password is 8-28 characters
        if len(password) < 8:
            return self.invalid_criteria_password.setText("8 characters min")
        if len(password) > 28:
            return self.invalid_criteria_password.setText("28 characters max")
        self.invalid_criteria_password.setText("")
        if password != confirm_password:
            return self.invalid_notsame_password.setText("Confirm your password")
        self.invalid_notsame_password.setText("")
        with open("accounts.txt", "a") as i:
            i.writelines(f"\n{username} {password}")
        self.exited.emit()
        self.close()

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