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

时间:2017-09-07 03:39:44

标签: python pyqt pyqt5

如何将全名(来自记事本用户;传递;全名)放在第二个类的Qlineedit小部件中显示(这也是第二个GUI)

第一类

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

这里是QlineEdit,对象名称是来自第二个gui的usershowname_line:

here's the QlineEdit which object name is usershowname_line from the second gui

1 个答案:

答案 0 :(得分:0)

你必须创建一个可以发送字符串的信号(pyqtSignal(str)),这将在关闭窗口之前发出。要显示它,我们必须将它连接到QLineEdit的setText(在您的情况下,它将your_lineedit替换为您的QLineEdit的名称)。以上所有内容如下所示:

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