下载安装Python3.7.8
python官网:https://www.python.org/
PyQt5配置
安装PyQt5
pip install PyQt5
pip install qt5_tools
pytcharm创建项目
配置外部工具
QTDesigner的Arguments语句不用填
QTDesigner的Working directory语句:$ProjectFileDir$
Pyuic的Arguments语句:-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
PyUIC的Working directory语句:$FileDir$
PyRIC的Arguments语句:$FileName$ -o $FileNameWithoutExtension$_rc.py
PyRIC的Working directory语句:$FileDir$
PostgreSQL各平台安装教程:http://t.csdn.cn/w9Ff6
测试是否成功
实战
登录界面设计
登录标题
图标
标签字体设置
输入框
设置提示默认值
按钮
禁止窗口缩放
设置变量名
数据库设计
下面数据库语句是单独在Navicat里面执行的
info表
DROP TABLE IF EXISTS `info`;
CREATE TABLE `info` (
`ID` int(11) NOT NULL,
`sex` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`height` int(11) DEFAULT NULL,
`weight` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `info` VALUES ('1', '男', '18', '180', '70');
INSERT INTO `info` VALUES ('2', '女', '20', '177', '60');
login表
DROP TABLE IF EXISTS `login`;
CREATE TABLE `login` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(20) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `login` VALUES ('1', '小米', 'xiaomi');
INSERT INTO `login` VALUES ('2', '苹果', 'pingguo');
登录代码功能
# -*- coding: UTF-8 -*-
'''
@Project :PyQtTest
@File :main.py
@IDE :PyCharm
@Author :Manba_77
'''
from PySide2.QtWidgets import QApplication, QMessageBox
from PySide2.QtUiTools import QUiLoader
import pymysql
class Win_Login:
def __init__(self):
# 从文件中加载UI定义
# 从UI定义中动态创建一个相应的窗口对象
# 注意:里面的控件对象也成为窗口对象的属性了
# 比如 self.ui.button,self.ui.textEdit
self.ui = QUiLoader().load('login.ui')
self.ui.btn_login.clicked.connect(self.OnSignIn)
self.ui.edt_password.returnPressed.connect(self.OnSignIn)
def exe_sql(self, sql):
conn = pymysql.connect(host='127.0.0.1', user='root', password='123456', database='qtproject')
try:
cursor = conn.cursor()
cursor.execute(sql)
result_sql = cursor.fetchall()
cursor.close()
conn.close()
return result_sql
except:
result_sql = 0
return result_sql
def OnSignIn(self):
username = self.ui.edt_username.text().strip()
password = self.ui.edt_password.text().strip()
sql_1 = 'SELECT * From login;'
result_sql_1 = self.exe_sql(sql_1)
if result_sql_1 == 1:
QMessageBox.Warning(
self.ui,
'用户名或密码错误'
)
else:
for res_1 in result_sql_1:
print(res_1)
if username == res_1[1] and password == res_1[2]:
sql_2 = 'SELECT * From info;'
result_sql_2 = self.exe_sql(sql_2)
for res_2 in result_sql_2:
if res_1[0] == res_2[0]:
QMessageBox.warning(
self.ui,
'结果',
"性别:{}\n年龄:{}\n身高:{}\n体重:{}".format(res_2[1], res_2[2], res_2[3], res_2[4])
)
app = QApplication([])
stat = Win_Login()
stat.ui.show()
app.exec_()
运行效果
完整项目代码
项目代码已打包放在网盘可自取,感谢三连
链接:https://pan.baidu.com/s/1gYAqzGTac62jgFBLPPPgkQ?pwd=skml
提取码:skml