代码拉取完成,页面将自动刷新
from PyQt5 import QtWidgets, QtCore
from PyQt5.Qt import Qt
from MainUI import Ui_MainWindow
import json
from PyQt5.QtWidgets import QMessageBox, QFileDialog, QTableWidgetItem
import requests
from qt_material import apply_stylesheet
import csv
import easygui
with open("info.json", "r", encoding="utf-8") as f:
INFO = json.load(f)
with open("settings.json", "r", encoding="utf-8") as f:
SETTINGS = json.load(f)
def check_version():
try:
if SETTINGS["server"]["already set"]:
info = requests.get("http://" + SETTINGS["server"]["IP address"] + "/least-version")
if INFO["version code"] < int(info.text):
QMessageBox.warning(None, "发现新版本", "发现新版本!请更新!")
exit(3)
mainwindow.connect = True
mainwindow.actionlogin.setEnabled(True)
mainwindow.label_2.setText("服务器已连接")
mainwindow.pushButton_6.setEnabled(False)
else:
QMessageBox.critical(None, "云服务IP未设置", "请设置云服务器的地址!")
exit(2)
except requests.exceptions.ConnectionError:
QMessageBox.information(None, "云服务连接失败", "请检查云服务器是否开启!")
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.connect = False # 服务器连接状态
self.setupUi(self)
self.pushButton_5.setProperty("class", "danger")
self.pushButton_3.clicked.connect(self.load_table_local)
self.tableWidget.setColumnCount(2)
self.tableWidget.setHorizontalHeaderLabels(["English", "中文"])
self.actionlogin.triggered.connect(self.login)
self.pushButton_6.clicked.connect(self.link_check_veraion)
self.actionlogin.setEnabled(False)
self.pushButton.setEnabled(False)
self.pushButton.clicked.connect(self.load_cloud_data)
self.actionAbout.triggered.connect(self.about)
self.comboBox.currentIndexChanged.connect(self.display_cloud_data)
self.is_stop_display_cloud_data = False # 用来判断是否继续显示数据,避免报错
self.pushButton_4.setEnabled(False)
self.pushButton_5.setEnabled(False)
self.pushButton_2.setEnabled(False)
self.table_type = "" # cloud 表示现在加载的词库来自云端 本地为local
self.pushButton_5.clicked.connect(self.delete_table)
self.table = []
self.pushButton_2.clicked.connect(self.upload_table)
self.pushButton_4.clicked.connect(self.remember)
self.is_login = False
def about(self):
QMessageBox.information(None, "ABOUT", """
作者:zhangsr
版本:{}
""".format(INFO["version name"]))
def link_check_veraion(self):
check_version()
def welcome(self):
pass
def display_data(self, table):
self.tableWidget.setRowCount(len(table))
for i in range(len(table)):
# 填充表格内容
data = QTableWidgetItem(str(table[i][0]))
self.tableWidget.setItem(i, 0, data)
data = QTableWidgetItem(str(table[i][1]))
self.tableWidget.setItem(i, 1, data)
# 调整表格宽度大小
self.tableWidget.resizeColumnsToContents()
self.tableWidget.resizeRowsToContents()
self.tableWidget.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
self.pushButton_4.setEnabled(True)
self.pushButton_5.setEnabled(True)
self.pushButton_2.setEnabled(True)
print(self.table, "in display data")
def load_table_local(self):
def load(method):
with open(path[0], "r", encoding=method) as f: # ansi
reader = csv.reader(f) # 创建一个csv.reader对象,用于读取文件f中的内容
table = [] # 创建一个空列表,用于存储数据
# 读取表格
for i in reader:
cache = []
for x in i:
cache.append(x)
table.append(cache)
self.display_data(table)
self.table = table
self.table_type = "local"
path = QFileDialog.getOpenFileName(self, "选择本地表格文件", "", "CSV files(*.csv)")
if path[0]:
try:
load("utf-8")
except UnicodeDecodeError: # 如果文件使用了ANSI编码,就使用ANSI编码加载
try:
load("ansi")
except UnicodeDecodeError: # 如果文件使用了UTF-16编码,就使用UTF-16编码加载
load("utf-16")
def login(self):
self.name = easygui.enterbox("用户名", "用户名")
password = easygui.passwordbox("密码", "密码")
self.password = password
if self.name is None or password is None:
QMessageBox.critical(None, "登录失败", "用户名或密码不能为空!")
return False
self.data = requests.post("http://" + SETTINGS["server"]["IP address"] + "/login",
data={"username": self.name, "password": password}).json()
print(self.data)
# print(type(data))
if self.data["list"] == "password incorrect":
QMessageBox.critical(None, "登录失败", "密码错误!")
return False
if self.data["list"] == "user not exist":
QMessageBox.critical(None, "登录失败", "用户名错误!")
return False
cache = 0
for i in self.data["list"]:
self.data["list"][cache] = i[:-4] # 去除后缀
cache += 1
# print(data)
del cache # 删除变量
self.pushButton.setEnabled(True)
self.is_login = True
def load_cloud_data(self):
self.is_stop_display_cloud_data = True
self.comboBox.clear()
for i in self.data["list"]:
self.comboBox.addItem(i)
self.is_stop_display_cloud_data = False
def display_cloud_data(self, index):
if not self.is_stop_display_cloud_data:
file = self.comboBox.itemText(index) + ".csv"
try:
data = requests.post("http://" + SETTINGS["server"]["IP address"] + "/data/",
data={"file": file, "username": self.name}).json()
except requests.exceptions.JSONDecodeError:
QMessageBox.critical(None, "加载失败", "服务器返回数据出错,请检查服务器连接或反馈")
return False
print(data)
self.display_data(data["data"])
self.data = data["data"]
self.table_type = "cloud"
def delete_table(self):
if self.table_type == "cloud":
if QMessageBox.warning(None, "确认",
"你确认要将{}用户的{}词表删除吗?".format(self.name, self.comboBox.currentText()),
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No) == QMessageBox.Yes:
password = easygui.passwordbox("", "输入{}的密码".format(self.name))
data = requests.post("http://" + SETTINGS["server"]["IP address"] + "/delete/",
data={"file": self.comboBox.currentText() + ".csv", "password": password,
"user": self.name})
if data.json()["return"] == "password incorrect":
QMessageBox.critical(None, "删除失败", "密码错误!")
return False
elif data.json()["return"] == "user not exist":
QMessageBox.critical(None, "删除失败", "用户不存在!")
return False
elif data.json()["return"] == "file not exist":
QMessageBox.critical(None, "删除失败", "文件不存在!")
return False
elif data.json()["return"] == "complete":
self.data = requests.post("http://" + SETTINGS["server"]["IP address"] + "/login",
data={"username": self.name, "password": password}).json()
cache = 0
for i in self.data["list"]:
self.data["list"][cache] = i[:-4] # 去除后缀
cache += 1
del cache # 删除变量
self.load_cloud_data()
QMessageBox.information(None, "提示", "删除成功!")
self.pushButton_5.setEnabled(False)
else:
QMessageBox.information(None, "提示", "本地词表不支持删除")
return False
def remember(self):
import remember
self.add_window = remember.MainWindow(self.table)
self.add_window.show()
def upload_table(self):
if not self.is_login:
QMessageBox.critical(None, "提示", "请先登录!")
return False
if self.table_type == "local":
name = easygui.enterbox("", "词表名")
print("=" * 30)
print(self.table)
data = requests.post("http://" + SETTINGS["server"]["IP address"] + "/upload/",
data={"name": name, "username": self.name, "data": json.dumps({"data": self.table})})
if data.json()["return"] == "file exist":
QMessageBox.critical(None, "提示", "该词表已存在!")
elif data.json()["return"] == "complete":
QMessageBox.information(None, "提示", "上传成功!")
self.data = requests.post("http://" + SETTINGS["server"]["IP address"] + "/login",
data={"username": self.name, "password": self.password}).json()
cache = 0
for i in self.data["list"]:
self.data["list"][cache] = i[:-4] # 去除后缀
cache += 1
del cache # 删除变量
self.load_cloud_data()
self.pushButton_2.setEnabled(False)
else:
QMessageBox.information(None, "", "这已经是云上此词表啦")
if __name__ == '__main__':
import sys
QtCore.QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling) # 适应windows缩放
app = QtWidgets.QApplication(sys.argv)
# 重新设置标题
mainwindow = MainWindow()
mainwindow.setFixedSize(736, 439) # 设置禁止改变窗口大小
mainwindow.setWindowTitle("星际词表-{}".format(INFO["version name"]))
# mainwindow.setWindowFlags(QtCore.Qt.WindowCloseButtonHint)
check_version()
apply_stylesheet(app, theme='dark_cyan.xml')
mainwindow.show()
sys.exit(app.exec_())
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。