从密码字段[python] [pyqt4]检索文本

时间:2010-01-26 18:56:25

标签: python user-interface passwords pyqt4

def welcomeStage (self):        
    self.test = QtGui.QLineEdit (self)
    self.test.move (50, 150)

    QtCore.QObject.connect (self.test, QtCore.SIGNAL ('returnPressed()'), self.passwordStage)

def passwordStage (self):
    self.email = self.test.text()
    self.test.clear()
    self.test.setEchoMode (QtGui.QLineEdit.Password)
    QtCore.QObject.connect (self.test, QtCore.SIGNAL ('returnPressed()'), self.loginStage)

def loginStage (self):
    self.pwd = self.test.text()
    print self.pwd
    if len (self.pwd) < 0:
        welcomeStage ()
        return

简单地说,我正在制作登录表单。用户输入他们的电子邮件,然后清除文本字段并将回显模式设置为密码模式。 text()函数返回电子邮件很好,但是当我在更改了echo模式后调用text()时,它返回0.我一直在寻找有关text()函数及其运行方式的文档。当密码模式打开时,我还没有找到任何东西。有人知道这是怎么做的吗?

1 个答案:

答案 0 :(得分:0)

我无法提供使用QLineEdit的修复方法,但您可以通过在QInputDialog方法中指定模式,在getText()中获取密码。像这样:

diag = QtGui.QInputDialog
s = None
while s is None:
    qstring, ok = diag.getText(self, QtCore.QString(title), QtCore.QString(text), mode=QtGui.QLineEdit.Password)
    s = str(qstring)
    if ok is False: # user pressed Cancel
        return None
    if s == '':     # user entered nothing
        s = None
return s