简体   繁体   English

PyQt5-显示从记事本到QLineEdit的字符串

[英]PyQt5 - show a string from notepad to a QLineEdit

How do you put the fullname (from notepad user; pass; fullname) to show at the Qlineedit widget in the second class (which is also the second GUI) 如何将全名(来自记事本用户;通过;全名)显示在第二个类(也是第二个GUI)的Qlineedit小部件上

FIRST CLASS 头等舱

from inventoryform import InventoryDialog

class LoginDialog(QtWidgets.QDialog, Ui_loginform):

    isLogged = QtCore.pyqtSignal()

    def __init__(self, parent=None):


    def login_button_clicked(self):

        import csv

        with open('user.txt', newline='') as f:
            reader = csv.reader(f, delimiter=';')
            for line in reader:
                us, pa, fn = line
                if self.username_line.text() == us and self.password_line.text() == pa:
                    QtWidgets.QMessageBox.information(self, "LOGIN", "LOGIN SUCCESSFUL!")
                    self.isLogged.emit()
                    a = QLineEdit()
                    a.setText(fn)
                    usershowname_line.append(a)
                    self.close()
                    return
            else:
                    QtWidgets.QMessageBox.information(self, "LOGIN FAILED", "LOGIN FAILED!")

    def exit_button_clicked(self):
        self.close()

if __name__ == '__main__':

    import sys

    app = QtWidgets.QApplication(sys.argv)

    loginform_window = LoginDialog()

    x = InventoryDialog()

    loginform_window.isLogged.connect(x.show)

    x.isLogout.connect(loginform_window.show)

    loginform_window.show()
    sys.exit(app.exec_())

SECOND CLASS 第二类

class InventoryDialog(QtWidgets.QDialog, Ui_inventoryform):

    isLogout = QtCore.pyqtSignal()

    def __init__(self, parent=None):

        QtWidgets.QDialog.__init__(self, parent)
        self.setupUi(self)

        self.signout_button.clicked.connect(self.onSignout_button)

    def onSignout_button(self):
        self.isLogout.emit()
        self.close()

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    inventoryform_window = InventoryDialog()
    inventoryform_window.show()
    sys.exit(app.exec_())

Here's the QlineEdit which object name is usershowname_line from the second gui: 这是QlineEdit,其对象名称是第二个gui中的usershowname_line:

这是QlineEdit,其对象名称是第二个gui中的usershowname_line

You have to create a signal that can send a string (pyqtSignal (str)), this will emit before closing the window. 您必须创建一个可以发送字符串的信号(pyqtSignal(str)),该字符串将在关闭窗口之前发出。 To show it we must connect it to setText of QLineEdit (In your case it replaces your_lineedit with the name of your QLineEdit). 为了显示它,我们必须将它连接到QLineEdit的setText(在您的情况下,它用QLineEdit的名称替换your_lineedit)。 All of the above is shown in the following part: 以下部分显示了以上所有内容:

class LoginDialog(QtWidgets.QDialog, Ui_loginform):
    isLogged = QtCore.pyqtSignal()
    dataSignal = QtCore.pyqtSignal(str)
    [...]
    def login_button_clicked(self):
        [...]
        self.dataSignal.emit("user: {}, pass: {}, fullname: {}".format(us, pa, fn))
        self.isLogged.emit()

    [...]

if __name__ == '__main__':

    import sys
    app = QtWidgets.QApplication(sys.argv)
    loginform_window = LoginDialog()
    x = InventoryDialog()
    loginform_window.isLogged.connect(x.show)

    loginform_window.dataSignal.connect(x.your_lineedit.setText)
    #                                     ^^^^^^^^^^^^^

    x.isLogout.connect(loginform_window.show)

    loginform_window.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM