基于PyQt5的图形化界面开发——自制Redis图形化客户端
- 前言
- 1. Python第三方库安装及Redis学习
- 1.1 Python第三方库安装
- 1.2 Redis数据库安装和基础命令
- 2. 登录界面
- 2.1 login.py
- 2.2 image_login.py
- 3. 主界面
- 3.1 main_ui.py
- 3.2 image_main.py
- 4.界面切换
- 5. 写在最后(附下载链接)
前言
之前我们学习了Redis数据库的基本操作,现在我们来一边学习PyQt5图形化界面开发,一边来加深对Redis的操作。
对于计算机基础薄弱的同学而言命令操作数据库似乎太过抽象,用图形化界面的方式操作有利于加深理解。
这次我们先看使用效果,效果如下:
操作系统:Windows10 专业版
开发环境:Pycahrm Comunity 2022.3
Python解释器版本:Python3.8
第三方库: Redis,PyQt5
1. Python第三方库安装及Redis学习
1.1 Python第三方库安装
本次需要用到的库为 Redis 和 PyQt5
PyQt5 用于编写图形化界面
Redis 用于操作数据库
如果你不会安装第三方库,你可以参考我的以下文章进行学习:
使用vscode、pycharm安装Python第三方库
1.2 Redis数据库安装和基础命令
如果你不会安装数据库、启动数据库,你可以参考以下文章,并且还可以学习Redis基础语句:
Redis数据库的安装(Windows10)
Redis数据库常用语句
2. 登录界面
Redis数据库是需要登录的,可能你会在多个服务器(主机)上部署多个数据库,又或许你的数据库端口号并非默认6379端口,亦或许你的密码由于不下心泄露或者其他原因需要更改,这都将导致我们频繁的对代码进行修改。
所以我们采用登录界面的方式,为这些可能变换的信息提供更改的可能:
预期效果是以上这样,在正确输入以后会完成登录,跳转至操作界面,由于这次我们添加了图片,所以添加了一份资源代码,代码如下:
2.1 login.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '.\login.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(372, 353)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(110, 130, 191, 21))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(60, 130, 31, 20))
self.label.setObjectName("label")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(110, 190, 191, 21))
self.lineEdit_3.setEchoMode(QtWidgets.QLineEdit.Password)
self.lineEdit_3.setObjectName("lineEdit_3")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(50, 190, 51, 20))
self.label_3.setObjectName("label_3")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(110, 160, 191, 21))
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(60, 160, 31, 20))
self.label_2.setObjectName("label_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(140, 270, 93, 28))
self.pushButton.setObjectName("pushButton")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(90, 20, 221, 91))
self.label_4.setStyleSheet("border-image:url(:/image/redis.png)")
self.label_4.setText("")
self.label_4.setObjectName("label_4")
self.checkBox = QtWidgets.QCheckBox(self.centralwidget)
self.checkBox.setGeometry(QtCore.QRect(140, 240, 91, 19))
self.checkBox.setObjectName("checkBox")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(110, 220, 191, 20))
self.label_5.setText("")
self.label_5.setObjectName("label_5")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 372, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Redis-登录"))
self.label.setText(_translate("MainWindow", "Host"))
self.label_3.setText(_translate("MainWindow", "Passwd"))
self.label_2.setText(_translate("MainWindow", "Port"))
self.pushButton.setText(_translate("MainWindow", "登录"))
self.checkBox.setText(_translate("MainWindow", "记住密码"))
import image_login
2.2 image_login.py
资源文件,存放图片等资源
太长了,待会将完整工程打包放在文末的链接供下载
3. 主界面
主界面需要有完成各个操作的功能,为了简化界面(偷懒),这里我只放了字符串,列表,集合三种结构,代码如下:
3.1 main_ui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '.\main.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(467, 343)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(290, 220, 121, 31))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(50, 170, 251, 16))
self.label_3.setText("")
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(140, 10, 191, 91))
self.label_4.setStyleSheet("border-image: url(:/image/redis.png);")
self.label_4.setText("")
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(330, 270, 111, 20))
self.label_5.setObjectName("label_5")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(50, 110, 351, 48))
self.widget.setObjectName("widget")
self.gridLayout = QtWidgets.QGridLayout(self.widget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.label = QtWidgets.QLabel(self.widget)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(self.widget)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 0, 1, 1, 1)
self.lineEdit = QtWidgets.QLineEdit(self.widget)
self.lineEdit.setObjectName("lineEdit")
self.gridLayout.addWidget(self.lineEdit, 1, 0, 1, 1)
self.lineEdit_2 = QtWidgets.QLineEdit(self.widget)
self.lineEdit_2.setObjectName("lineEdit_2")
self.gridLayout.addWidget(self.lineEdit_2, 1, 1, 1, 1)
self.widget1 = QtWidgets.QWidget(self.centralwidget)
self.widget1.setGeometry(QtCore.QRect(50, 200, 199, 67))
self.widget1.setObjectName("widget1")
self.formLayout = QtWidgets.QFormLayout(self.widget1)
self.formLayout.setContentsMargins(0, 0, 0, 0)
self.formLayout.setObjectName("formLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton = QtWidgets.QPushButton(self.widget1)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.widget1)
self.pushButton_2.setObjectName("pushButton_2")
self.verticalLayout.addWidget(self.pushButton_2)
self.formLayout.setLayout(0, QtWidgets.QFormLayout.LabelRole, self.verticalLayout)
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.pushButton_3 = QtWidgets.QPushButton(self.widget1)
self.pushButton_3.setObjectName("pushButton_3")
self.verticalLayout_2.addWidget(self.pushButton_3)
self.pushButton_4 = QtWidgets.QPushButton(self.widget1)
self.pushButton_4.setObjectName("pushButton_4")
self.verticalLayout_2.addWidget(self.pushButton_4)
self.formLayout.setLayout(0, QtWidgets.QFormLayout.FieldRole, self.verticalLayout_2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 467, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Redis-主界面"))
self.comboBox.setItemText(0, _translate("MainWindow", "字符串"))
self.comboBox.setItemText(1, _translate("MainWindow", "列表"))
self.comboBox.setItemText(2, _translate("MainWindow", "集合"))
self.label_5.setText(_translate("MainWindow", "by --IoT_h2"))
self.label.setText(_translate("MainWindow", "KEY"))
self.label_2.setText(_translate("MainWindow", "VALUE"))
self.pushButton.setText(_translate("MainWindow", "添加"))
self.pushButton_2.setText(_translate("MainWindow", "删除"))
self.pushButton_3.setText(_translate("MainWindow", "显示当前KEY"))
self.pushButton_4.setText(_translate("MainWindow", "显示所有KEY"))
import image_main
3.2 image_main.py
资源文件,用于存放图片等资源
太长了,待会将完整工程打包放在文末的链接供下载
4.界面切换
这次的界面切换和以往不同,不必那么麻烦了,只是添加了一个登录跳转主界面的操作,还有两个数据显示界面的跳转。
需要 注意 的是,点击登录,登录界面应该隐藏,而显示数据界面弹出的时候,主界面不应该关闭。
5. 写在最后(附下载链接)
如果发生报错,请检查你是否安装第三方库或是否改动了此工程结构:
下载链接如下,注意下载后要解压才能正常打开:
点我下载