pyqt5如何提取LineEdit中的文字
Tofloor
poster avatar
星外之神
deepin
2017-05-20 05:08
Author
如题,初入Python,问问
想把两个LineEdit中用户输入的文字设为两个量,求教一下

源码怎么改?

#!/usr/bin/python3
#coding:utf-8
from subprocess import Popen, PIPE
from PyQt5.QtWidgets import *
import sys

class LoginDlg(QDialog):
    def __init__(self, parent=None):
        super(LoginDlg, self).__init__(parent)
        usr = QLabel("用户:")
        pwd = QLabel("密码:")
        self.usrLineEdit = QLineEdit()
        self.pwdLineEdit = QLineEdit()
        self.pwdLineEdit.setEchoMode(QLineEdit.Password)

        gridLayout = QGridLayout()
        gridLayout.addWidget(usr, 0, 0, 1, 1)
        gridLayout.addWidget(pwd, 1, 0, 1, 1)
        gridLayout.addWidget(self.usrLineEdit, 0, 1, 1, 3);
        gridLayout.addWidget(self.pwdLineEdit, 1, 1, 1, 3);

        okBtn = QPushButton("确定")
        cancelBtn = QPushButton("取消")
        btnLayout = QHBoxLayout()

        btnLayout.setSpacing(60)
        btnLayout.addWidget(okBtn)
        btnLayout.addWidget(cancelBtn)

        dlgLayout = QVBoxLayout()
        dlgLayout.setContentsMargins(40, 40, 40, 40)
        dlgLayout.addLayout(gridLayout)
        dlgLayout.addStretch(40)
        dlgLayout.addLayout(btnLayout)

        self.setLayout(dlgLayout)
        okBtn.clicked.connect(self.accept)
        cancelBtn.clicked.connect(self.reject)
        self.setWindowTitle("登录")
        self.resize(300, 200)

    def accept(self):
        self.usrLineEdit.text().strip() == $username and self.pwdLineEdit.text() == $password :
            super(LoginDlg, self).accept()
        p = Popen('pcs login --username=$username --password=$password', shell = True, stdout = PIPE)
        p.wait()

            QMessageBox.warning(self,
                    "警告",
                    "用户名或密码错误!",
                    QMessageBox.Yes)
            self.usrLineEdit.setFocus()

app = QApplication(sys.argv)
dlg = LoginDlg()
dlg.show()
dlg.exec_()
app.exit()

Reply Favorite View the author
All Replies
avatar
leixiaozeng
deepin
2017-05-20 05:24
#1
.setText(string)#设置输入框内容

.text()#返回输入框内容

.clear()#清除输入框内容
Reply View the author