工具/版本
(1)安装环境:Windows7 64bit
(2)使用版本Python3.6
(3)PYQT5
(4)eric6
主要涉及的内容:
(1)tableWidget的使用
(2)QLineEdit的的的的的的的使用
(3)各个位置增加图标
(4)从登陆届面跳到主窗口
(5)TCP / IP通信
(6)访问数据库
一,登陆界面
1,界面设置图标和标题
self.resize(500, 300) # 设置大小
self.setWindowTitle("XX登录界面") # 设置标题
self.setFixedSize(self.width(), self.height()) # 禁止最大化
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("..\\Image\\10.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.setWindowIcon(icon) # 设置窗口的图标
设置完成后,我们发现电脑任务栏的图标并没有修改,setWindowIcon这个函数起了一半的作用,学的Qt的的的的的都知道这个函数同时设置了程序的两个地方的图标显示,修改方法看参考如下链接
需要让窗户知道
import ctypes
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("myappid")
2,按钮美化
参考这篇文章
Python的PyQt5-图形可视化界面(3) - 按钮--Qpushbutton
self.pushButton_2.setFlat(True) # 设置按键边框取消
效果如下
#!/usr/bin/env python
# encoding: utf-8
"""
+----------------------------------------------------------------------------+
╱◥██◣ ∧_∧ ∧_∧ ∧_∧ ∧_∧ ╱◥██◣
|田︱田田| (^ .^) (^ 、^) (^ 0^) (^ Д^) |田︱田田|
╬╬╬╬╬╬╬╬╬╬-------∪-∪-------∪-∪--------∪-∪-------∪-∪---╬╬╬╬╬╬╬╬╬╬╬
+----------------------------------------------------------------------------+
License (C) Copyright 2017-2017, Corporation Limited.
File Name : UI_progressBar.py
Auther : samenmoer
Software Version : Python3.6
Email Address : gpf192315@163.com
Creat Time : 2018-10-28 17:27:02
优快云 blog : https://blog.youkuaiyun.com/samenmoer
Description :
------------------------------------------------------------------------------
Modification History
Data By Version Change Description
==============================================================================
${time} | | |
==============================================================================
¤╭⌒╮ ╭⌒╮¤╭⌒╮ ╭⌒╮¤╭⌒╮ ╭⌒╮¤╭⌒╮ ╭⌒╮¤╭⌒╮╭⌒╮¤╭⌒╮╭⌒╮¤╭⌒╮╭⌒╮¤╭⌒╮╭⌒╮¤╭⌒╮╭⌒╮¤╭⌒╮
------------------------------------------------------------------------------
"""
import sys
from PyQt5 import QtGui
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QPushButton
from PyQt5.QtGui import QPixmap, QIcon, QMovie
from PyQt5.QtCore import QThread, pyqtSignal
class UI_LoginBar(QWidget):
def __init__(self, parent=None):
super(UI_LoginBar, self).__init__(parent)
self.setupUi()
self.pushButton.clicked.connect(self.run)
self.pushButton2.clicked.connect(self.run)
def setupUi(self):
self.setObjectName("MainWindow")
self.resize(500, 300) # 设置大小
self.setWindowTitle("XX登录界面") # 设置标题
self.setFixedSize(self.width(), self.height()) # 禁止最大化
icon = QIcon()
icon.addPixmap(QPixmap("..\\Image\\10.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.setWindowIcon(icon) # 设置窗口的图标
self.pushButton = QPushButton(self)
self.pushButton.setGeometry(135, 230, 230, 30)
self.pushButton2 = QPushButton(self)
self.pushButton2.setGeometry(135, 265, 230, 30)
self.pushButton.setObjectName("pushButton")
self.pushButton.setText("登录")
self.lable = QLabel(self)
self.lable.setGeometry(75, 100, 350, 50)
self.movie = QMovie("1.gif")
def run(self):
self.lable.setMovie(self.movie)
if self.sender() == self.pushButton:
self.movie.start()
else:
self.movie.stop()
self.lable.setPixmap(QPixmap(""))
"""
class Thread(QThread):
_signal = pyqtSignal(bool)
def __init__(self, parent=None):
super(Thread, self).__init__(parent)
self.working = True
def __del__(self):
self.wait()
self.working = False
def run(self):
# 线程相关代码
while self.working is True:
self._signal.emit(True)
self.sleep(0.8)
self._signal.emit(False)
self.sleep(0.8)
class UI_LoginBar(QWidget):
def __init__(self, parent=None):
super(UI_LoginBar, self).__init__(parent)
self.setupUi()
#self.Runthread()
# 创建一个新的线程
self.pushButton.clicked.connect(self.Runthread)
self.pushButton2.clicked.connect(lambda: self.thread.terminate())
def Runthread(self):
self.thread = Thread()
self.thread._signal.connect(self.callback)
self.thread.start()
def threadstop(self):
self.thread.terminate()
def setupUi(self):
self.setObjectName("MainWindow")
self.resize(500, 300) # 设置大小
self.setWindowTitle("XX登录界面") # 设置标题
self.setFixedSize(self.width(), self.height()) # 禁止最大化
icon = QIcon()
icon.addPixmap(QPixmap("..\\Image\\10.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.setWindowIcon(icon) # 设置窗口的图标
self.pushButton = QPushButton(self)
self.pushButton.setGeometry(135, 230, 230, 30)
self.pushButton2 = QPushButton(self)
self.pushButton2.setGeometry(135, 280, 230, 30)
self.pushButton.setObjectName("pushButton")
self.pushButton.setText("登录")
self.lable = QLabel(self)
self.lable.setGeometry(75, 100, 350, 50)
self.png = QPixmap("..\\Image\\1.png")
self.png2 = QPixmap("..\\Image\\2.png")
def callback(self, status):
if status is True:
self.lable.setPixmap(self.png)
elif status is False:
self.lable.setPixmap(self.png2)
else:
self.lable.setPixmap(QPixmap(""))
# ==========================================================================
# * Founction Name : ModuleInit
# * Parameter : None
# * Return : None
# * Description : 为当前模块设置Log头,方便识别,以及其他初始化设置
# ==========================================================================
"""
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = UI_LoginBar()
ui.show()
sys.exit(app.exec_())
import re
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QMainWindow, QApplication, QLineEdit
from PyQt5.QtCore import Qt, pyqtSlot
from PassWordEdit import PwdLineEdit
import ctypes
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("myappid")
class LogIn_UI(QMainWindow):
def __init__(self, parent=None):
super(LogIn_UI, self).__init__(parent)
self.SetupUi()
self.pushButton.clicked.connect(self.test)
def test(self):
self.hide()
def SetupUi(self):
self.setObjectName("MainWindow")
self.resize(500, 300) # 设置大小
self.setWindowTitle("XX登录界面") # 设置标题
self.setFixedSize(self.width(), self.height()) # 禁止最大化
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("..\\Image\\10.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.setWindowIcon(icon) # 设置窗口的图标
self.checkBox = QtWidgets.QCheckBox(self)
self.checkBox.setGeometry(QtCore.QRect(130, 180, 91, 16))
self.checkBox.setObjectName("checkBox")
self.checkBox.setText("记住账号")
self.checkBox_2 = QtWidgets.QCheckBox(self)
self.checkBox_2.setGeometry(QtCore.QRect(280, 180, 91, 16))
self.checkBox_2.setObjectName("checkBox_2")
self.checkBox_2.setText("自动登录")
self.pushButton = QtWidgets.QPushButton(self)
self.pushButton.setGeometry(QtCore.QRect(135, 230, 230, 30))
self.pushButton.setObjectName("pushButton")
self.pushButton.setText("登录")
self.tableWidget = QtWidgets.QTableWidget(self)
self.tableWidget.setGeometry(QtCore.QRect(120, 70, 260, 90))
self.tableWidget.setObjectName("tableWidget")
self.TableSet()
self.pushButton_3 = QtWidgets.QPushButton(self)
self.pushButton_3.setGeometry(QtCore.QRect(400, 75, 72, 30))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_3.setText("注册账号")
self.pushButton_4 = QtWidgets.QPushButton(self)
self.pushButton_4.setGeometry(QtCore.QRect(400, 120, 72, 30))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_4.setText("修改密码")
self.pushButton_2 = QtWidgets.QPushButton(self)
self.pushButton_2.setGeometry(QtCore.QRect(25, 80, 70, 70))
self.pushButton_2.setText("")
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_2.setFlat(True) # 设置按键边框取消
self.pushButton_2.setIconSize(QtCore.QSize(70, 70))
self.pushButton_2.setIcon(QtGui.QIcon("..\\Image\\16.jpg"))
def TableSet(self):
self.table = self.tableWidget
self.table.setColumnCount(1)
self.table.setRowCount(2)
# 设置表头隐藏显示
self.table.setVerticalHeaderLabels(['账号', '密码'])
self.table.verticalHeader().setVisible(True)
self.table.horizontalHeader().setVisible(False)
# 设置行高列宽
self.table.setColumnWidth(0, 220)
self.table.setRowHeight(0, 44)
self.table.setRowHeight(1, 44)
# 账号密码设置
self.PassWordSet()
self.IdentitySet()
self.table.setCellWidget(0, 0, self.IdentityItem)
self.table.setCellWidget(1, 0, self.PassWordItem)
#@pyqtSlot()
def IdentitySet(self):
self.IdentityItem = QLineEdit()
self.IdentityItem.installEventFilter(self)
self.IdentityItem.setClearButtonEnabled(True)
def PassWordSet(self):
self.PassWordItem = PwdLineEdit()
self.PassWordItem.installEventFilter(self)
# self.PassWordItem.setText("hello")
# 禁止右键菜单
self.PassWordItem.setClearButtonEnabled(True)
self.PassWordItem.setContextMenuPolicy(Qt.NoContextMenu)
self.PassWordItem.setPlaceholderText("密码>8位 可输入数字字母字符")
#self.PassWordItem.setEchoMode(QLineEdit.Password)
@pyqtSlot()
def on_PassWordItem_editingFinished(self):
regex_pwd = "^([A-Z]|[a-z]|[0-9]|[`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“'。,、?]){6,16}$"
self.pwd1 = self.lineEdit_2.GetPassword()
if len(self.pwd1) > 0:
self.lineEdit_6.clear()
rrpwd = re.compile(regex_pwd)
if rrpwd.match(self.pwd1) is None:
self.lineEdit_6.setText('密码不符合要求!')
else:
self.lineEdit_6.setText('密码未填写!')
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = LogIn_UI()
ui.show()
sys.exit(app.exec_())
#!/usr/bin/env python
# encoding: utf-8
"""
+----------------------------------------------------------------------------+
╱◥██◣ ∧_∧ ∧_∧ ∧_∧ ∧_∧ ╱◥██◣
|田︱田田| (^ .^) (^ 、^) (^ 0^) (^ Д^) |田︱田田|
╬╬╬╬╬╬╬╬╬╬-------∪-∪-------∪-∪--------∪-∪-------∪-∪---╬╬╬╬╬╬╬╬╬╬╬
+----------------------------------------------------------------------------+
License (C) Copyright 2017-2017, Corporation Limited.
File Name : PassWordEdit.py
Auther : samenmoer
Software Version : Python3.6
Email Address : gpf192315@163.com
Creat Time : 2018-10-06 19:34:17
优快云 blog : https://blog.youkuaiyun.com/samenmoer
Description :
------------------------------------------------------------------------------
Modification History
Data By Version Change Description
==============================================================================
${time} | | |
==============================================================================
¤╭⌒╮ ╭⌒╮¤╭⌒╮ ╭⌒╮¤╭⌒╮ ╭⌒╮¤╭⌒╮ ╭⌒╮¤╭⌒╮╭⌒╮¤╭⌒╮╭⌒╮¤╭⌒╮╭⌒╮¤╭⌒╮╭⌒╮¤╭⌒╮╭⌒╮¤╭⌒╮
------------------------------------------------------------------------------
"""
from RecordLog import print_log
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtCore import QTimer
# ==========================================================================
# * Founction Name : ModuleInit
# * Parameter : None
# * Return : None
# * Description : 为当前模块设置Log头,方便识别,以及其他初始化设置
# ==========================================================================
def ModuleInit():
global LogHead
LogHead = "[wait to fill]"
print_log("文件初始化完成", LogHead=LogHead)
return
class PwdLineEdit(QLineEdit):
def __init__(self):
super().__init__()
self.m_LineEditText = ""
self.m_LastCharCount = 0
self.Action()
def Action(self):
self.cursorPositionChanged[int,int].connect(self.DisplayPasswordAfterEditSlot)
self.textEdited[str].connect(self.GetRealTextSlot)
self.time = QTimer(self)
self.time.setInterval(500)
self.time.start()
self.show()
def DisplayPasswordAfterEditSlot(self, old, new):
print('new:', new)
print('old:', old)
if old >= 0 and new >= 0:
if new > old:
self.time.timeout.connect(self.DisplayPasswordSlot)
else:
self.setCursorPosition(old)
def DisplayPasswordSlot(self):
self.setText(self.GetMaskString())
def GetRealTextSlot(self, text):
self.m_LastCharCount = len(self.m_LineEditText)
if len(text) > self.m_LastCharCount:
self.m_LineEditText += text[-1]
elif len(text) <= self.m_LastCharCount:
self.m_LineEditText = self.m_LineEditText[:-1]
def GetMaskString(self):
mask = ""
count = len(self.text())
if count > 0:
for i in range(count):
mask += "*"
return mask
def GetPassword(self):
return self.m_LineEditText