更新示例代码

This commit is contained in:
965 2023-05-25 22:27:40 +08:00
parent f426eae271
commit 18ad86d3fa
7 changed files with 268 additions and 184 deletions

View File

@ -2,9 +2,17 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/Modules" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.11 (PythonLearning)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyNamespacePackagesService">
<option name="namespacePackageFolders">
<list>
<option value="$MODULE_DIR$/Modules" />
</list>
</option>
</component>
</module>

29
Modules/WcModule.py Normal file
View File

@ -0,0 +1,29 @@
import jieba
import wordcloud
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
size = None # 210
maxword = None # 75
font = None # "AdobeHeitiStd-Regular.otf"
file = None # "../resources/111.txt"
mask = None # "../resources/mask.png"
def generate(size, maxword, font, file, mask):
raw_data = open(file, encoding="utf-8").read()
ls = jieba.lcut(raw_data)
text = ' '.join(ls)
open(file, encoding="utf-8").close()
mask = np.array(Image.open(mask))
wc = wordcloud.WordCloud(font_path=font,
mask=mask,
background_color='white',
max_font_size=size,
max_words=maxword,
stopwords={'王勃', ''})
wc.generate(text)
plt.imshow(wc)
plt.axis("off")
plt.show()

View File

@ -5,18 +5,18 @@ import numpy as np
import matplotlib.pyplot as plt
raw_data = open("./resources/111.txt", encoding="gbk").read()
raw_data = open("./resources/111.txt", encoding="utf-8").read()
ls = jieba.lcut(raw_data)
text = ' '.join(ls)
open("./resources/111.txt", encoding="gbk").close()
open("./resources/111.txt", encoding="utf-8").close()
mask = np.array(Image.open("./resources/mask.png"))
wc = wordcloud.WordCloud(font_path="msyh.ttc",
wc = wordcloud.WordCloud(font_path="simsun.ttc",
mask=mask,
background_color='white',
max_font_size=240,
stopwords={'王勃', ''})
wc.generate(text)
wc.to_file("./results/111.png")
#wc.to_file("./results/111.png")
plt.imshow(wc)
plt.axis("off")
plt.show()

View File

@ -1,15 +1,17 @@
from PyQt5 import QtCore, QtGui, uic
from PyQt5 import QtGui, uic
from PyQt5.QtWidgets import *
import tkinter as tk
from tkinter import filedialog
import jieba
import wordcloud
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import WcModule
import sys
root = tk.Tk()
root.withdraw()
WcModule.size = 210
WcModule.maxword = 75
WcModule.font = "AdobeHeitiStd-Regular.otf"
WcModule.file = "../resources/111.txt"
WcModule.mask = "../resources/mask.png"
class mainthread:
@ -18,13 +20,17 @@ class mainthread:
self.mainWindow = uic.loadUi('main.ui')
self.mainWindow.FileChooseButton.clicked.connect(self.Choose_File)
self.mainWindow.MaskChooseButton.clicked.connect(self.Choose_Mask)
#self.mainWindow.GenerateButton.actions(self.Generate_Action())
self.mainWindow.FontSelectionBox.addItems(['Adobe 黑体 Std R', '华文行楷 R', '楷体 R', '微软雅黑 R', '幼圆 R'])
self.mainWindow.FontSelectionBox.currentIndexChanged.connect(self.selectionChange)
self.mainWindow.FontSizeSelectBox.valueChanged.connect(self.valueChange1)
self.mainWindow.MaxWordCountBox.valueChanged.connect(self.valueChange2)
self.mainWindow.GenerateButton.toggle()
self.mainWindow.GenerateButton.clicked.connect(self.Generate_Action())
# 文本选择
def Choose_File(self):
global file_path, cursor
file_path = "./resources/111.txt"
file_path = filedialog.askopenfilename()
WcModule.file = file_path
self.mainWindow.FilePathPreview.setText(file_path)
cursor = self.mainWindow.LogBrowser.textCursor()
cursor.movePosition(QtGui.QTextCursor.End)
@ -34,44 +40,36 @@ class mainthread:
# 蒙版选择
def Choose_Mask(self):
global mask_path
mask_path = "./resources/mask.png"
mask_path = filedialog.askopenfilename()
WcModule.mask = mask_path
cursor = self.mainWindow.LogBrowser.textCursor()
self.mainWindow.MaskPathPreview.setText(mask_path)
cursor.movePosition(QtGui.QTextCursor.End)
cursor.insertText(f"[Sys]:Mask path selected:{mask_path}\n")
self.mainWindow.LogBrowser.setTextCursor(cursor)
self.mainWindow.LogBrowser.ensureCursorVisible()
def valueChange1(self):
font_size = 10 * self.mainWindow.FontSizeSelectBox.value()
WcModule.size = font_size
def valueChange2(self):
max_word = self.mainWindow.MaxWordCountBox.value()
WcModule.maxword = max_word
@staticmethod
def selectionChange(i):
WcModule.font = fontlist[i]
@staticmethod
def Generate_Action():
raw_data = open(file_path).read()
ls = jieba.lcut(raw_data)
text = ' '.join(ls)
open(file_path).close()
mask = np.array(Image.open(mask_path))
wc = wordcloud.WordCloud(font_path="msyh.ttc",
mask=mask,
background_color='white',
max_font_size=240,
stopwords={'王勃', ''})
wc.generate(text)
wc.to_file("./results/111.png")
plt.imshow(wc)
plt.axis("off")
plt.show()
global file_path
global mask_path
WcModule.generate(WcModule.size, WcModule.maxword, WcModule.font, WcModule.file, WcModule.mask)
fontlist = ("AdobeHeitiStd-Regular.otf", "STXINGKA.TTF", "simkai.ttf", "msyh.ttc", "simsun.ttc", "SIMYOU.TTF")
if __name__ == '__main__':
app = QApplication([])
app = QApplication(sys.argv)
# 显示UI
m_main = mainthread()
m_main.mainWindow.show()
app.exec_()

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>501</width>
<height>522</height>
<height>501</height>
</rect>
</property>
<property name="font">
@ -25,7 +25,7 @@
<property name="geometry">
<rect>
<x>380</x>
<y>480</y>
<y>460</y>
<width>101</width>
<height>32</height>
</rect>
@ -43,141 +43,11 @@
<bool>true</bool>
</property>
</widget>
<widget class="QWidget" name="Customizewidget" native="true">
<property name="geometry">
<rect>
<x>20</x>
<y>10</y>
<width>461</width>
<height>91</height>
</rect>
</property>
<property name="cursor">
<cursorShape>OpenHandCursor</cursorShape>
</property>
<widget class="QDoubleSpinBox" name="FontSizeSelectBox">
<property name="geometry">
<rect>
<x>360</x>
<y>40</y>
<width>71</width>
<height>41</height>
</rect>
</property>
<property name="value">
<double>13.500000000000000</double>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>360</x>
<y>10</y>
<width>71</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>Adobe 黑体 Std R</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>文字大小</string>
</property>
<property name="textInteractionFlags">
<set>Qt::NoTextInteraction</set>
</property>
</widget>
<widget class="QSpinBox" name="MaxWordCountBox">
<property name="geometry">
<rect>
<x>30</x>
<y>40</y>
<width>71</width>
<height>41</height>
</rect>
</property>
<property name="maximum">
<number>999</number>
</property>
<property name="value">
<number>30</number>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>30</x>
<y>10</y>
<width>71</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>Adobe 黑体 Std R</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>最大词数</string>
</property>
<property name="textInteractionFlags">
<set>Qt::NoTextInteraction</set>
</property>
</widget>
<widget class="QComboBox" name="FontSelectionBox">
<property name="geometry">
<rect>
<x>140</x>
<y>40</y>
<width>181</width>
<height>41</height>
</rect>
</property>
<property name="cursor">
<cursorShape>ArrowCursor</cursorShape>
</property>
<property name="mouseTracking">
<bool>true</bool>
</property>
<property name="acceptDrops">
<bool>true</bool>
</property>
</widget>
<widget class="QLabel" name="label_5">
<property name="geometry">
<rect>
<x>190</x>
<y>10</y>
<width>81</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>Adobe 黑体 Std R</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>字体选择</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="textInteractionFlags">
<set>Qt::NoTextInteraction</set>
</property>
</widget>
</widget>
<widget class="QTextBrowser" name="SizePreview">
<property name="geometry">
<rect>
<x>20</x>
<y>130</y>
<y>110</y>
<width>461</width>
<height>91</height>
</rect>
@ -187,7 +57,7 @@
<property name="geometry">
<rect>
<x>20</x>
<y>110</y>
<y>90</y>
<width>81</width>
<height>21</height>
</rect>
@ -209,7 +79,7 @@
<property name="geometry">
<rect>
<x>400</x>
<y>320</y>
<y>300</y>
<width>81</width>
<height>41</height>
</rect>
@ -222,7 +92,7 @@
<property name="geometry">
<rect>
<x>20</x>
<y>320</y>
<y>300</y>
<width>371</width>
<height>41</height>
</rect>
@ -232,7 +102,7 @@
<property name="geometry">
<rect>
<x>20</x>
<y>300</y>
<y>280</y>
<width>81</width>
<height>21</height>
</rect>
@ -254,7 +124,7 @@
<property name="geometry">
<rect>
<x>20</x>
<y>390</y>
<y>370</y>
<width>461</width>
<height>81</height>
</rect>
@ -264,7 +134,7 @@
<property name="geometry">
<rect>
<x>20</x>
<y>370</y>
<y>350</y>
<width>81</width>
<height>21</height>
</rect>
@ -286,7 +156,7 @@
<property name="geometry">
<rect>
<x>20</x>
<y>250</y>
<y>230</y>
<width>371</width>
<height>41</height>
</rect>
@ -296,7 +166,7 @@
<property name="geometry">
<rect>
<x>400</x>
<y>250</y>
<y>230</y>
<width>81</width>
<height>41</height>
</rect>
@ -309,7 +179,7 @@
<property name="geometry">
<rect>
<x>20</x>
<y>230</y>
<y>210</y>
<width>81</width>
<height>21</height>
</rect>
@ -327,11 +197,134 @@
<set>Qt::NoTextInteraction</set>
</property>
</widget>
<widget class="QDoubleSpinBox" name="FontSizeSelectBox">
<property name="geometry">
<rect>
<x>400</x>
<y>40</y>
<width>71</width>
<height>41</height>
</rect>
</property>
<property name="maximum">
<double>999.899999999999977</double>
</property>
<property name="singleStep">
<double>0.500000000000000</double>
</property>
<property name="value">
<double>21.000000000000000</double>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>400</x>
<y>20</y>
<width>71</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>Adobe 黑体 Std R</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>文字大小</string>
</property>
<property name="textInteractionFlags">
<set>Qt::NoTextInteraction</set>
</property>
</widget>
<widget class="QComboBox" name="FontSelectionBox">
<property name="geometry">
<rect>
<x>160</x>
<y>40</y>
<width>181</width>
<height>41</height>
</rect>
</property>
<property name="cursor">
<cursorShape>ArrowCursor</cursorShape>
</property>
<property name="mouseTracking">
<bool>true</bool>
</property>
<property name="acceptDrops">
<bool>true</bool>
</property>
</widget>
<widget class="QLabel" name="label_5">
<property name="geometry">
<rect>
<x>210</x>
<y>20</y>
<width>81</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>Adobe 黑体 Std R</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>字体选择</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="textInteractionFlags">
<set>Qt::NoTextInteraction</set>
</property>
</widget>
<widget class="QSpinBox" name="MaxWordCountBox">
<property name="geometry">
<rect>
<x>30</x>
<y>40</y>
<width>71</width>
<height>41</height>
</rect>
</property>
<property name="maximum">
<number>999</number>
</property>
<property name="value">
<number>75</number>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>30</x>
<y>20</y>
<width>71</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>Adobe 黑体 Std R</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>最大词数</string>
</property>
<property name="textInteractionFlags">
<set>Qt::NoTextInteraction</set>
</property>
</widget>
<widget class="QPushButton" name="GenerateButton">
<property name="geometry">
<rect>
<x>20</x>
<y>480</y>
<y>460</y>
<width>93</width>
<height>28</height>
</rect>
@ -342,7 +335,6 @@
</widget>
</widget>
<tabstops>
<tabstop>GenerateButton</tabstop>
<tabstop>MaskChooseButton</tabstop>
<tabstop>MaskPathPreview</tabstop>
<tabstop>LogBrowser</tabstop>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 291 KiB

After

Width:  |  Height:  |  Size: 209 KiB

57
temp.py
View File

@ -1 +1,58 @@
import sys
# QtCore是Qt的精髓包括五大模块元对象系统属性系统对象模型对象树信号槽
from PyQt5.QtCore import *
# QtGui 显示应用程序图标,工具提示和各种鼠标光标。
from PyQt5.QtGui import *
# Qt Widgets模块提供了一组UI元素来创建经典的桌面风格的用户界面。
from PyQt5.QtWidgets import *
class QDialogDemo(QMainWindow):
def __init__(self):
super(QDialogDemo, self).__init__()
self.initUI()
def initUI(self):
# 设置窗口标题
self.setWindowTitle('QDialog案例')
# 设置窗口尺寸
self.resize(300,200)
# 创建button控件直接把button放在窗口上
self.button = QPushButton(self)
# 设置button控件文本
self.button.setText('弹出对话框')
# 移动button的位置
self.button.move(50,50)
# 将单击信号和槽绑定
self.button.clicked.connect(self.showDialog)
# 槽方法
def showDialog(self):
# 创建对话框
dialog = QDialog()
# 在对话框dialog里面放一个button
button = QPushButton('确定',dialog)
# 点击button按钮关闭 现成的槽
button.clicked.connect(dialog.close)
# 移动button
button.move(50,50)
# 给dialog设置标题
dialog.setWindowTitle('对话框')
# 设置对话框为模式状态,模式状态:即模式状态开启时,对话框窗口里的所有控件不可用
dialog.setWindowModality(Qt.ApplicationModal)
# 显示对话框
dialog.exec()
# 防止别的脚本调用,只有自己单独运行时,才会调用下面的代码
if __name__ == '__main__':
# 创建app实例并传入参数
app = QApplication(sys.argv)
# 创建对象
main = QDialogDemo()
# 创建窗口
main.show()
# 进入程序的主循环并通过exit函数确保主循环安全结束(该释放资源的释放)
sys.exit(app.exec_())