环境
- PyQt 5.11.2
- Python 3.6
分析
海康摄像头开发,其中一部分需要用户手动输入IP、端口号、用户名、密码等信息,这些信息可以单独写成一个设置窗口,从窗口获取信息,并传入主窗口,方便后续开发。
窗口UI
实现功能
- 对于IP地址、端口号、用户名、密码,能够根据正则表达式控制用户的输入类型,设置输入长度;
- 对于密码,可以设置明文、暗文显示
- 当用户输入完成后,判断IP地址、端口号、用户名、密码信息是否按要求类型载入到系统变量,为后续传参做准备。
- 对于某一项输入类型不合要求,弹窗提示,不会载入系统变量
代码
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
import threading
window_width = 1920
window_height = 1080
button_width = 48
class Setting(QWidget):
valid_signal = pyqtSignal()
def __init__(self):
super(Setting, self).__init__()
self.ip = ''
self.port = ''
self.user = ''
self.passwd = ''
self.widgets = []
self.ip_label = QLabel(self)
self.ip_label.setFixedHeight(32)
self.ip_label.setText("ip地址:")
self.widgets.append(self.ip_label)
self.ip_edit = QLineEdit(self)
self.ip_edit.setFixedWidth(button_width*3)
self.ip_edit.setFixedHeight(32)
self.ip_edit.textChanged.connect(self.set_text_ip)
ip_re_str = '^((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))$'
ip_re = QRegExp(ip_re_str)
self.ip_edit.setValidator(QRegExpValidator(ip_re,self))
self.widgets.append(self.ip_edit)
self.port_label = QLabel(self)
self.port_label.setFixedHeight(32)
self.port_label.setText("端口号:")
self.widgets.append(self.port_label)
self.port_edit = QLineEdit(self)
self.port_edit.setFixedWidth(button_width)
self.port_edit.setFixedHeight(32)
self.port_edit.textChanged.connect(self.set_text_port)
self.port_edit.setValidator(QIntValidator(0,65535))
self.widgets.append(self.port_edit)
self.user_label = QLabel(self)
self.user_label.setFixedHeight(32)
self.user_label.setText("用户名:")
self.widgets.append(self.user_label)
self.user_edit = QLineEdit(self)
self.user_edit.setFixedWidth(button_width * 4)
self.user_edit.setFixedHeight(32)
self.user_edit.textChanged.connect(self.set_text_user)
user_re = QRegExp('^\w+$')
self.user_edit.setValidator(QRegExpValidator(user_re,self))
self.user_edit.setMaxLength(23)
self.widgets.append(self.user_edit)
self.passwd_label = QLabel(self)
self.passwd_label.setFixedHeight(32)
self.passwd_label.setText("密码:")
self.widgets.append(self.passwd_label)
self.passwd_edit = QLineEdit(self)
self.passwd_edit.setFixedWidth(button_width * 4)
self.passwd_edit.setFixedHeight(32)
self.passwd_edit.setEchoMode(QLineEdit.Password)
self.passwd_edit.textChanged.connect(self.set_text_passwd)
passwd_re = QRegExp('^\w+$')
self.passwd_edit.setValidator(QRegExpValidator(passwd_re))
self.passwd_edit.setMaxLength(12)
self.widgets.append(self.passwd_edit)
self.ok_button = QPushButton(self)
self.ok_button.setFixedHeight(32)
self.ok_button.setFixedWidth(button_width*2)
self.ok_button.setText('确定')
self.ok_button.clicked.connect(self.process_all_text)
self.widgets.append(self.ok_button)
self.show_passwd_button = QPushButton(self)
self.show_passwd_button.setFixedHeight(32)
self.show_passwd_button.setFixedWidth(button_width*2)
self.show_passwd_button.setText('显示密码')
self.show_passwd_button.clicked.connect(self.show_passwd)
self.widgets.append(self.show_passwd_button)
self.setGeometry(window_width / 2, window_height / 2, 300, 180)
self.setWindowFlags(self.windowFlags()&~Qt.WindowMaximizeButtonHint)
self.grid_layout = QGridLayout()
self.setLayout(self.grid_layout)
positions = [(i, j) for i in range(5) for j in range(2)]
for position, widget in zip(positions, self.widgets):
self.grid_layout.addWidget(widget,*position)
def set_text_ip(self):
self.ip = self.ip_edit.text()
def set_text_port(self):
self.port = self.port_edit.text()
def set_text_user(self):
self.user = self.user_edit.text()
def set_text_passwd(self):
self.passwd = self.passwd_edit.text()
def process_all_text(self):
ip_re = QRegExp('^((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))$')
user_re = QRegExp('^\w+$')
passwd_re = QRegExp('^\w+$')
ip_validator = QRegExpValidator(ip_re,self)
port_validator = QIntValidator(0,65535)
user_validator = QRegExpValidator(user_re,self)
passwd_validator = QRegExpValidator(passwd_re,self)
if ip_validator.validate(self.ip,0)[0] != QValidator.Acceptable:
QMessageBox.information(self,"注意","ip地址输入类型错误,请重新输入!",QMessageBox.Yes)
return
if port_validator.validate(self.port,0)[0] != QValidator.Acceptable:
QMessageBox.information(self, "注意", "端口号输入类型错误,请重新输入!", QMessageBox.Yes)
return
if user_validator.validate(self.user,0)[0] != QValidator.Acceptable:
QMessageBox.information(self, "注意", "用户名输入类型错误,请重新输入!", QMessageBox.Yes)
return
if passwd_validator.validate(self.passwd,0)[0] != QValidator.Acceptable:
QMessageBox.information(self, "注意", "密码输入类型错误,请重新输入!", QMessageBox.Yes)
return
print('info right,ip:{}, port:{}, user:{}, passwd:{}'.format(self.ip,self.port,self.user,self.passwd))
self.valid_signal.emit()#类型正确,发送信号通知主窗口
def show_passwd(self):
if self.show_passwd_button.text() == '显示密码':
self.passwd_edit.setEchoMode(QLineEdit.Normal)
self.show_passwd_button.setText('隐藏密码')
else:
self.passwd_edit.setEchoMode(QLineEdit.Password)
self.show_passwd_button.setText('显示密码')
def return_info(self):
return self.ip,int(self.port),self.user,self.passwd
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setApplicationName("登录设置")
window = Setting()
window.setFixedWidth(540)
window.setFixedHeight(400)
window.show()
sys.exit(app.exec_())
实现结果
输入成功,单击确定,成功检查并获取信息