首先,这是一个针对特殊用处的界面设计,内部有部分功能块可供参考。
主要工作:登录界面和操作界面。其中登陆界面的用户数据库可根据自己定义,操作界面主要是用来操作excel相关数据,本次将数据存入csv文件中,方便在linux服务器下面进行操作。
直接附上整体代码,在代码中各个模块中进行解释
# 所有用到的数据库
# This Python file uses the following encoding: utf-8
import sqlite3
import traceback
from PyQt5.QtGui import QMouseEvent, QCursor, QIcon
from LoginUi import *
from InterUI import *
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAbstractItemView, QMessageBox, QHeaderView, QTableWidgetItem, \
QAction, QMenu, QComboBox
from PyQt5.QtCore import Qt, QPoint
import pandas as pd
from pandas.core.frame import DataFrame
# 登录界面直接讲过了就不说了
class LoginWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_LoginWindow()
self.ui.setupUi(self)
# 设置为无边框的界面
# self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.ui.pushButton_L_ok.clicked.connect(self.login_in)
self.ui.pushButton_L_ok.setShortcut('Return') # 设置快捷键
self.show()
def login_in(self):
# 获取登录用户和密码
username = self.ui.lineEdit_L_username.text()
password = self.ui.lineEdit_L_password.text()
# # sql语句,判断数据库中是否拥有这账号和密码
# sql = 'select user_id, password, authority from user where user_id=? and password=? and authority=?'
# cursor.execute(sql, (username, password, self.authority))
# # 获取一致的部分,存在表示输入正确,不存在提示错误
# data = cursor.fetchall()
# # 账号和密码不为空时候,才进入
# if username == "123456" and password =="123456":
if username == "123456" and password == "123456":
self.win = MainWindow()
self.close()
else:
print("wrong!")
# 让登录界面可移动
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.m_flag = True
self.m_Position = event.globalPos() - self.pos() # 获取鼠标相对窗口的位置
event.accept()
self.setCursor(QCursor(Qt.OpenHandCursor)) # 更改鼠标图标
def mouseMoveEvent(self, QMouseEvent):
if Qt.LeftButton and self.m_flag:
self.move(QMouseEvent.globalPos() - self.m_Position) # 更改窗口位置
QMouseEvent.accept()
def mouseReleaseEvent(self, QMouseEvent):
self.m_flag = False
self.setCursor(QCursor(Qt.ArrowCursor))
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton_Submmit.clicked.connect(self.add_new_info)
self.ui.pushButton_Save.clicked.connect(self.changeinfos)
# # 禁止双击编辑单元格
# self.ui.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
# # 选择一行
# self.ui.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
# 添加右击菜单
self.ui.tableWidget.setContextMenuPolicy(Qt.CustomContextMenu)
self.ui.tableWidget.customContextMenuRequested.connect(self.create_rightmenu)
self.show_info()
# self.ui.tableWidget.itemClicked.connect(self.chooseInfo)
# 设置为无边框的界面
self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
# self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.show()
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.m_flag = True
self.m_Position = event.globalPos() - self.pos() # 获取鼠标相对窗口的位置
event.accept()
self.setCursor(QCursor(Qt.OpenHandCursor)) # 更改鼠标图标
def mouseMoveEvent(self, QMouseEvent):
if Qt.LeftButton and self.m_flag:
self.move(QMouseEvent.globalPos() - self.m_Position) # 更改窗口位置
QMouseEvent.accept()
def mouseReleaseEvent(self, QMouseEvent):
self.m_flag = False
self.setCursor(QCursor(Qt.ArrowCursor))
# 此函数是将Excel表格信息或者csv文件中的信息显示在界面上
def show_info(self):
# excel_path = '/XXXX/XXX.xlsx'
# data = pd.read_excel(excel_path, sheet_name='XX')
path = '/XXX/xxx.csv'
data = pd.read_csv(path, dtype = {'Num':str})
self.ui.tableWidget.setColumnCount(data.shape[1])
self.ui.tableWidget.setRowCount(data.shape[0])
for i in range(data.shape[0]):
for j in range(data.shape[1]):
self.ui.tableWidget.setItem(i, j, QTableWidgetItem(str(data.iloc[i, j])))
if j == 2:
self.combo_box = QComboBox(self)
self.combo_box.addItems(["男1", "男2", "女1"])
self.ui.tableWidget.setCellWidget(i, j, self.combo_box)
## 上面if是添加表格下拉的信息选择的操作
# 展示列表行索引
self.ui.tableWidget.setHorizontalHeaderLabels(data.columns)
# 右键菜单 在表格信息上右击能有两个选择功能
def create_rightmenu(self):
# 菜单对象
self.groupBox_menu = QMenu(self)
self.actionA = QAction(QIcon(), u'添加数据', self)
self.actionA.setShortcut('Ctrl+S') # 设置快捷键
self.groupBox_menu.addAction(self.actionA)
self.actionB = QAction(QIcon(), u'删除数据', self)
self.groupBox_menu.addAction(self.actionB)
self.actionA.triggered.connect(self.button) # 将动作A触发时连接到槽函数 button
self.actionB.triggered.connect(self.button_2)
self.groupBox_menu.popup(QCursor.pos()) # 声明当鼠标在groupBox控件上右击时,在鼠标位置显示右键菜单
def button(self):
# 按钮1槽函数,进行数据保存
# print('添加数据')
列1 = ' '
列2 = ' '
列3 = ' '
列4 = ' '
infos = pd.DataFrame({'列1': [列1], '列2': [列2],'列3': [列3], '列4': [列4]})
# print(infos)
if "" in infos:
print('Wrong')
QMessageBox.information(self, '提示', '输入信息不能为空')
else:
infos = DataFrame(infos)
path = '/XXX/xxx.csv'
data = pd.read_csv(path, dtype={'Num': str})
data1 = pd.concat([data, infos], ignore_index=True)
# print(data1)
data1.to_csv('/XXX/xxx.csv', encoding='utf-8', index=False)
self.reload_all_infos()
QMessageBox.information(self, '提示', 'Successfully')
def button_2(self):
# 按钮2槽函数,删除数据
file_path = '/XXX/xxx.csv'
data1 = pd.read_csv(file_path, dtype = {'Num':str})
# print('删除数据')
info_index = self.ui.tableWidget.currentIndex().row()
# print(data1.values[info_index])
infos_delete = data1.drop(data1.index[info_index])
# print(infos_delete)
infos_delete.to_csv('/XXX/xxx.csv', encoding='utf-8', index = False)
self.reload_all_infos()
def reload_all_infos(self):
self.show_info()
if __name__ == '__main__':
app = QApplication(sys.argv)
main = LoginWindow()
sys.exit(app.exec_())