【问题标题】:Password field for pyforms?pyforms的密码字段?
【发布时间】:2017-08-08 22:06:24
【问题描述】:
我的 UI 是用 pyforms 编写的。
如何实现密码字段? (例如。而不是 'P@ssW0rd' 它会显示 '********')。
我发现我可以使用 QLineEdit.EchoMode,但不确定如何实现。
提前致谢!
【问题讨论】:
标签:
python-2.7
passwords
pyforms
【解决方案1】:
Pyforms 还包括一个密码框。你也可以使用
self._password = ControlPassword('Password')
这么简单:
import pyforms
from pyforms.basewidget import BaseWidget
from pyforms.controls import ControlText
from pyforms.controls import ControlButton
from pyforms.controls import ControlPassword
class Login(BaseWidget):
def __init__(self):
super(Login,self).__init__('Simple example 1')
#Definition of the forms fields
self._username = ControlText('Username', 'Default value')
self._password = ControlPassword('Password')
self._button = ControlButton('Login')
self._button.value = self.__buttonAction #Define button action
def __buttonAction(self):
"""Button action event"""
username = self._username.value
password = self._password.value
credentials = (username, password)
return credentials
#Execute the application
if __name__ == "__main__":
pyforms.start_app( Login )
【解决方案2】:
您可以在项目文件夹中添加以下模块为ControlPasswordText.py:
from pysettings import conf
from pyforms.Controls import ControlText
from PyQt4.QtGui import QLineEdit
class ControlPasswordText(ControlText):
def __init__(self, *args, **kwargs):
super(ControlPasswordText, self).__init__(*args, **kwargs)
self.form.lineEdit.setEchoMode(QLineEdit.Password)
以下是你将如何使用它:
import pyforms
from pyforms import BaseWidget
from pyforms.Controls import ControlText
from pyforms.Controls import ControlButton
# Importing the module here
from ControlPasswordText import ControlPasswordText
class SimpleExample1(BaseWidget):
def __init__(self):
super(SimpleExample1,self).__init__('Simple example 1')
#Definition of the forms fields
self._username = ControlText('Username')
# Using the password class
self._password = ControlPasswordText('Password')
#Execute the application
if __name__ == "__main__": pyforms.startApp( SimpleExample1 )
结果: