diff --git a/Wordcloud_GUI/main.py b/Wordcloud_GUI/main.py index c1d49de..7ca563c 100644 --- a/Wordcloud_GUI/main.py +++ b/Wordcloud_GUI/main.py @@ -1,7 +1,7 @@ from PyQt5 import QtGui, uic from PyQt5.QtWidgets import * import tkinter as tk -from tkinter import filedialog +from tkinter import filedialog, messagebox import WcModule import sys import chardet @@ -11,14 +11,19 @@ root.withdraw() WcModule.size = 210 WcModule.maxword = 75 WcModule.font = "msyh.ttc" -WcModule.file = "../resources/111.txt" -WcModule.mask = "../resources/mask.png" -WcModule.stopwords = {'王勃', '一'} +WcModule.stopwords = {'1', '2'} +# noinspection PyGlobalUndefined class mainthread: def __init__(self): + global boot1 + global boot2 + global boot3 + boot1 = 0 + boot2 = 0 + boot3 = 0 self.mainWindow = uic.loadUi('main.ui') self.mainWindow.FileChooseButton.clicked.connect(self.Choose_File) self.mainWindow.MaskChooseButton.clicked.connect(self.Choose_Mask) @@ -36,34 +41,40 @@ class mainthread: file_path = filedialog.askopenfilename() WcModule.file = file_path cursor = self.mainWindow.LogBrowser.textCursor() + global boot1 if file_path == "": + boot1 = 0 self.mainWindow.FilePathPreview.setText(file_path) cursor.movePosition(QtGui.QTextCursor.End) cursor.insertText(f"[Sys]:File path unselected\n") self.mainWindow.LogBrowser.setTextCursor(cursor) self.mainWindow.LogBrowser.ensureCursorVisible() + self.alert_box("输入路径为空") else: self.mainWindow.FilePathPreview.setText(file_path) data = open(file=file_path, mode='rb') result = chardet.detect(data.read()) result = result["encoding"] cursor.movePosition(QtGui.QTextCursor.End) - cursor.insertText(f"[Sys]:File path selected:{file_path}\n") + cursor.insertText(f"[Sys]:File path selected: {file_path}\n") self.mainWindow.LogBrowser.setTextCursor(cursor) self.mainWindow.LogBrowser.ensureCursorVisible() if "GB" in result: + boot1 = 1 WcModule.codec = "gbk" cursor.movePosition(QtGui.QTextCursor.End) cursor.insertText(f"[Sys]:File codec detected: GB2312(GBK)\n") self.mainWindow.LogBrowser.setTextCursor(cursor) self.mainWindow.LogBrowser.ensureCursorVisible() elif "utf" in result: + boot1 = 1 WcModule.codec = "utf-8" cursor.movePosition(QtGui.QTextCursor.End) cursor.insertText(f"[Sys]:File codec detected: UTF-8\n") self.mainWindow.LogBrowser.setTextCursor(cursor) self.mainWindow.LogBrowser.ensureCursorVisible() else: + boot1 = 0 cursor.movePosition(QtGui.QTextCursor.End) cursor.insertText(f"[Sys]:File codec detection Failed\n") self.mainWindow.LogBrowser.setTextCursor(cursor) @@ -74,16 +85,21 @@ class mainthread: mask_path = filedialog.askopenfilename() WcModule.mask = mask_path cursor = self.mainWindow.LogBrowser.textCursor() + global boot2 + boot2 = 0 if mask_path == "": - self.mainWindow.FilePathPreview.setText(mask_path) + self.mainWindow.MaskPathPreview.setText(mask_path) cursor.movePosition(QtGui.QTextCursor.End) cursor.insertText(f"[Sys]:Mask path unselected\n") self.mainWindow.LogBrowser.setTextCursor(cursor) self.mainWindow.LogBrowser.ensureCursorVisible() + self.alert_box("输入路径为空") + boot2 = 0 else: + boot2 = 1 self.mainWindow.MaskPathPreview.setText(mask_path) cursor.movePosition(QtGui.QTextCursor.End) - cursor.insertText(f"[Sys]:Mask path selected:{mask_path}\n") + cursor.insertText(f"[Sys]:Mask path selected: {mask_path}\n") self.mainWindow.LogBrowser.setTextCursor(cursor) self.mainWindow.LogBrowser.ensureCursorVisible() @@ -95,16 +111,29 @@ class mainthread: max_word = self.mainWindow.MaxWordCountBox.value() WcModule.maxword = max_word - @staticmethod - def Confirm_input(): - inp = str(input_words) - inp = inp.replace(',', ',') - inp = inp.split(',') - WcModule.stopwords = set(inp) + def Confirm_input(self): + cursor = self.mainWindow.LogBrowser.textCursor() + if boot3 == 0: + cursor.movePosition(QtGui.QTextCursor.End) + cursor.insertText(f"[Sys]:Stop words undefined\n") + self.mainWindow.LogBrowser.setTextCursor(cursor) + self.mainWindow.LogBrowser.ensureCursorVisible() + self.alert_box("无输入") + else: + inp = str(input_words) + inp = inp.replace(',', ',') + inp = inp.split(',') + WcModule.stopwords = set(inp) + cursor.movePosition(QtGui.QTextCursor.End) + cursor.insertText(f"[Sys]:Stop words: {set(inp)}\n") + self.mainWindow.LogBrowser.setTextCursor(cursor) + self.mainWindow.LogBrowser.ensureCursorVisible() @staticmethod def textChanged(text): global input_words + global boot3 + boot3 = 1 input_words = None input_words = text @@ -112,14 +141,27 @@ class mainthread: def selectionChange(i): WcModule.font = fontlist[i] + @staticmethod + def alert_box(str1): + a = messagebox.showerror('Error', str1) + print(a) + def Generate_Action(self): cursor = self.mainWindow.LogBrowser.textCursor() - cursor.movePosition(QtGui.QTextCursor.End) - cursor.insertText(f"[Sys]:Parameters:{WcModule.size, WcModule.maxword, WcModule.font, WcModule.file, WcModule.mask, WcModule.stopwords}\n") - cursor.insertText(f"[Sys]:Generating.....\n") - self.mainWindow.LogBrowser.setTextCursor(cursor) - self.mainWindow.LogBrowser.ensureCursorVisible() - WcModule.generate(WcModule.size, WcModule.maxword, WcModule.font, WcModule.file, WcModule.mask, WcModule.stopwords, WcModule.codec) + if boot1 + boot2 == 2: + cursor.movePosition(QtGui.QTextCursor.End) + cursor.insertText( + f"[Sys]:Parameters: {WcModule.size, WcModule.maxword, WcModule.font, WcModule.file, WcModule.mask, WcModule.stopwords, WcModule.codec}\n") + cursor.insertText(f"[Sys]:Generating.....\n") + self.mainWindow.LogBrowser.setTextCursor(cursor) + self.mainWindow.LogBrowser.ensureCursorVisible() + WcModule.generate(WcModule.size, WcModule.maxword, WcModule.font, WcModule.file, WcModule.mask, WcModule.stopwords, WcModule.codec) + else: + cursor.movePosition(QtGui.QTextCursor.End) + cursor.insertText(f"[Sys]:Parameter missing\n") + self.mainWindow.LogBrowser.setTextCursor(cursor) + self.mainWindow.LogBrowser.ensureCursorVisible() + self.alert_box("缺少必要参数") fontlist = ("msyh.ttc", "STXINGKA.TTF", "simkai.ttf", "AdobeHeitiStd-Regular.otf", "simsun.ttc", "SIMYOU.TTF")