mirror of
https://github.com/WMK965/965-Python-Learning-Repo.git
synced 2025-04-27 14:03:22 +00:00
扩展Wordcloud_GUI项目功能
This commit is contained in:
parent
bceeae2167
commit
a37cc1f376
@ -1,7 +1,7 @@
|
|||||||
from PyQt5 import QtGui, uic
|
from PyQt5 import QtGui, uic
|
||||||
from PyQt5.QtWidgets import *
|
from PyQt5.QtWidgets import *
|
||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
from tkinter import filedialog
|
from tkinter import filedialog, messagebox
|
||||||
import WcModule
|
import WcModule
|
||||||
import sys
|
import sys
|
||||||
import chardet
|
import chardet
|
||||||
@ -11,14 +11,19 @@ root.withdraw()
|
|||||||
WcModule.size = 210
|
WcModule.size = 210
|
||||||
WcModule.maxword = 75
|
WcModule.maxword = 75
|
||||||
WcModule.font = "msyh.ttc"
|
WcModule.font = "msyh.ttc"
|
||||||
WcModule.file = "../resources/111.txt"
|
WcModule.stopwords = {'1', '2'}
|
||||||
WcModule.mask = "../resources/mask.png"
|
|
||||||
WcModule.stopwords = {'王勃', '一'}
|
|
||||||
|
|
||||||
|
|
||||||
|
# noinspection PyGlobalUndefined
|
||||||
class mainthread:
|
class mainthread:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
global boot1
|
||||||
|
global boot2
|
||||||
|
global boot3
|
||||||
|
boot1 = 0
|
||||||
|
boot2 = 0
|
||||||
|
boot3 = 0
|
||||||
self.mainWindow = uic.loadUi('main.ui')
|
self.mainWindow = uic.loadUi('main.ui')
|
||||||
self.mainWindow.FileChooseButton.clicked.connect(self.Choose_File)
|
self.mainWindow.FileChooseButton.clicked.connect(self.Choose_File)
|
||||||
self.mainWindow.MaskChooseButton.clicked.connect(self.Choose_Mask)
|
self.mainWindow.MaskChooseButton.clicked.connect(self.Choose_Mask)
|
||||||
@ -36,34 +41,40 @@ class mainthread:
|
|||||||
file_path = filedialog.askopenfilename()
|
file_path = filedialog.askopenfilename()
|
||||||
WcModule.file = file_path
|
WcModule.file = file_path
|
||||||
cursor = self.mainWindow.LogBrowser.textCursor()
|
cursor = self.mainWindow.LogBrowser.textCursor()
|
||||||
|
global boot1
|
||||||
if file_path == "":
|
if file_path == "":
|
||||||
|
boot1 = 0
|
||||||
self.mainWindow.FilePathPreview.setText(file_path)
|
self.mainWindow.FilePathPreview.setText(file_path)
|
||||||
cursor.movePosition(QtGui.QTextCursor.End)
|
cursor.movePosition(QtGui.QTextCursor.End)
|
||||||
cursor.insertText(f"[Sys]:File path unselected\n")
|
cursor.insertText(f"[Sys]:File path unselected\n")
|
||||||
self.mainWindow.LogBrowser.setTextCursor(cursor)
|
self.mainWindow.LogBrowser.setTextCursor(cursor)
|
||||||
self.mainWindow.LogBrowser.ensureCursorVisible()
|
self.mainWindow.LogBrowser.ensureCursorVisible()
|
||||||
|
self.alert_box("输入路径为空")
|
||||||
else:
|
else:
|
||||||
self.mainWindow.FilePathPreview.setText(file_path)
|
self.mainWindow.FilePathPreview.setText(file_path)
|
||||||
data = open(file=file_path, mode='rb')
|
data = open(file=file_path, mode='rb')
|
||||||
result = chardet.detect(data.read())
|
result = chardet.detect(data.read())
|
||||||
result = result["encoding"]
|
result = result["encoding"]
|
||||||
cursor.movePosition(QtGui.QTextCursor.End)
|
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.setTextCursor(cursor)
|
||||||
self.mainWindow.LogBrowser.ensureCursorVisible()
|
self.mainWindow.LogBrowser.ensureCursorVisible()
|
||||||
if "GB" in result:
|
if "GB" in result:
|
||||||
|
boot1 = 1
|
||||||
WcModule.codec = "gbk"
|
WcModule.codec = "gbk"
|
||||||
cursor.movePosition(QtGui.QTextCursor.End)
|
cursor.movePosition(QtGui.QTextCursor.End)
|
||||||
cursor.insertText(f"[Sys]:File codec detected: GB2312(GBK)\n")
|
cursor.insertText(f"[Sys]:File codec detected: GB2312(GBK)\n")
|
||||||
self.mainWindow.LogBrowser.setTextCursor(cursor)
|
self.mainWindow.LogBrowser.setTextCursor(cursor)
|
||||||
self.mainWindow.LogBrowser.ensureCursorVisible()
|
self.mainWindow.LogBrowser.ensureCursorVisible()
|
||||||
elif "utf" in result:
|
elif "utf" in result:
|
||||||
|
boot1 = 1
|
||||||
WcModule.codec = "utf-8"
|
WcModule.codec = "utf-8"
|
||||||
cursor.movePosition(QtGui.QTextCursor.End)
|
cursor.movePosition(QtGui.QTextCursor.End)
|
||||||
cursor.insertText(f"[Sys]:File codec detected: UTF-8\n")
|
cursor.insertText(f"[Sys]:File codec detected: UTF-8\n")
|
||||||
self.mainWindow.LogBrowser.setTextCursor(cursor)
|
self.mainWindow.LogBrowser.setTextCursor(cursor)
|
||||||
self.mainWindow.LogBrowser.ensureCursorVisible()
|
self.mainWindow.LogBrowser.ensureCursorVisible()
|
||||||
else:
|
else:
|
||||||
|
boot1 = 0
|
||||||
cursor.movePosition(QtGui.QTextCursor.End)
|
cursor.movePosition(QtGui.QTextCursor.End)
|
||||||
cursor.insertText(f"[Sys]:File codec detection Failed\n")
|
cursor.insertText(f"[Sys]:File codec detection Failed\n")
|
||||||
self.mainWindow.LogBrowser.setTextCursor(cursor)
|
self.mainWindow.LogBrowser.setTextCursor(cursor)
|
||||||
@ -74,16 +85,21 @@ class mainthread:
|
|||||||
mask_path = filedialog.askopenfilename()
|
mask_path = filedialog.askopenfilename()
|
||||||
WcModule.mask = mask_path
|
WcModule.mask = mask_path
|
||||||
cursor = self.mainWindow.LogBrowser.textCursor()
|
cursor = self.mainWindow.LogBrowser.textCursor()
|
||||||
|
global boot2
|
||||||
|
boot2 = 0
|
||||||
if mask_path == "":
|
if mask_path == "":
|
||||||
self.mainWindow.FilePathPreview.setText(mask_path)
|
self.mainWindow.MaskPathPreview.setText(mask_path)
|
||||||
cursor.movePosition(QtGui.QTextCursor.End)
|
cursor.movePosition(QtGui.QTextCursor.End)
|
||||||
cursor.insertText(f"[Sys]:Mask path unselected\n")
|
cursor.insertText(f"[Sys]:Mask path unselected\n")
|
||||||
self.mainWindow.LogBrowser.setTextCursor(cursor)
|
self.mainWindow.LogBrowser.setTextCursor(cursor)
|
||||||
self.mainWindow.LogBrowser.ensureCursorVisible()
|
self.mainWindow.LogBrowser.ensureCursorVisible()
|
||||||
|
self.alert_box("输入路径为空")
|
||||||
|
boot2 = 0
|
||||||
else:
|
else:
|
||||||
|
boot2 = 1
|
||||||
self.mainWindow.MaskPathPreview.setText(mask_path)
|
self.mainWindow.MaskPathPreview.setText(mask_path)
|
||||||
cursor.movePosition(QtGui.QTextCursor.End)
|
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.setTextCursor(cursor)
|
||||||
self.mainWindow.LogBrowser.ensureCursorVisible()
|
self.mainWindow.LogBrowser.ensureCursorVisible()
|
||||||
|
|
||||||
@ -95,16 +111,29 @@ class mainthread:
|
|||||||
max_word = self.mainWindow.MaxWordCountBox.value()
|
max_word = self.mainWindow.MaxWordCountBox.value()
|
||||||
WcModule.maxword = max_word
|
WcModule.maxword = max_word
|
||||||
|
|
||||||
@staticmethod
|
def Confirm_input(self):
|
||||||
def Confirm_input():
|
cursor = self.mainWindow.LogBrowser.textCursor()
|
||||||
inp = str(input_words)
|
if boot3 == 0:
|
||||||
inp = inp.replace(',', ',')
|
cursor.movePosition(QtGui.QTextCursor.End)
|
||||||
inp = inp.split(',')
|
cursor.insertText(f"[Sys]:Stop words undefined\n")
|
||||||
WcModule.stopwords = set(inp)
|
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
|
@staticmethod
|
||||||
def textChanged(text):
|
def textChanged(text):
|
||||||
global input_words
|
global input_words
|
||||||
|
global boot3
|
||||||
|
boot3 = 1
|
||||||
input_words = None
|
input_words = None
|
||||||
input_words = text
|
input_words = text
|
||||||
|
|
||||||
@ -112,14 +141,27 @@ class mainthread:
|
|||||||
def selectionChange(i):
|
def selectionChange(i):
|
||||||
WcModule.font = fontlist[i]
|
WcModule.font = fontlist[i]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def alert_box(str1):
|
||||||
|
a = messagebox.showerror('Error', str1)
|
||||||
|
print(a)
|
||||||
|
|
||||||
def Generate_Action(self):
|
def Generate_Action(self):
|
||||||
cursor = self.mainWindow.LogBrowser.textCursor()
|
cursor = self.mainWindow.LogBrowser.textCursor()
|
||||||
cursor.movePosition(QtGui.QTextCursor.End)
|
if boot1 + boot2 == 2:
|
||||||
cursor.insertText(f"[Sys]:Parameters:{WcModule.size, WcModule.maxword, WcModule.font, WcModule.file, WcModule.mask, WcModule.stopwords}\n")
|
cursor.movePosition(QtGui.QTextCursor.End)
|
||||||
cursor.insertText(f"[Sys]:Generating.....\n")
|
cursor.insertText(
|
||||||
self.mainWindow.LogBrowser.setTextCursor(cursor)
|
f"[Sys]:Parameters: {WcModule.size, WcModule.maxword, WcModule.font, WcModule.file, WcModule.mask, WcModule.stopwords, WcModule.codec}\n")
|
||||||
self.mainWindow.LogBrowser.ensureCursorVisible()
|
cursor.insertText(f"[Sys]:Generating.....\n")
|
||||||
WcModule.generate(WcModule.size, WcModule.maxword, WcModule.font, WcModule.file, WcModule.mask, WcModule.stopwords, WcModule.codec)
|
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")
|
fontlist = ("msyh.ttc", "STXINGKA.TTF", "simkai.ttf", "AdobeHeitiStd-Regular.otf", "simsun.ttc", "SIMYOU.TTF")
|
||||||
|
Loading…
Reference in New Issue
Block a user