Python QT 练习之 词云 进阶版

# Time: 2020/06/01

#Author: Xiaohong

# 运行环境: OS: Windows 10

#  Python: 3.7

# 功能: 读文件,生成词云。可以屏蔽某些词汇,可以只生成 TOP50 高频词的词云

效果如下:

主程序:Tl_003.py

import sys
import time
import os
from PyQt5.QtWidgets import *
from PyQt5 import QtGui, QtCore, QtWidgets
# from PyQt5.QtWidgets import QTableWidget, QProgressBar, QLineEdit, QComboBox, QFrame, QTableWidgetItem
from PyQt5.QtCore import *
from PyQt5.QtGui import *
# 这个test_pyqt是ui文件对应的py文件的文件名
from Ui_qt_003 import Ui_qt003

import wordcloud   #pip install wordcloud
import jieba     #pip install jieba
import imageio    #pip install imageio

#调用 QT 设计的界面对应的类
class W003(QDialog):
    def __init__(self, parent=None):        
        super(W003, self).__init__(parent)
        self.child = Ui_qt003()
        self.child.setupUi(self)
        self.child.btn_select_file.clicked.connect(self.select_file)
        self.child.btn_select_file_probit.clicked.connect(self.select_file_probit)
        self.child.btn_cloud.clicked.connect(self.gen_word_cloud)
        self.child.btn_select_file_2.clicked.connect(self.select_file2)
        self.child.btn_show_pic.clicked.connect(self.show_pic_orig)

    # 设立函数,来取得当前时间
    def get_sysdate(self):
        now = time.strftime("%Y%m%d %H%M%S", time.localtime(time.time()))
        # print(now)
        return now

    def select_file(self):
        fileName, filetype = QFileDialog.getOpenFileName(self, "选择文件", "/", "All Files (*);;TXT Files (*.txt)") # User 选择路径来找到文本文件
        # print(fileName, filetype)  # 打印文件全部路径(包括文件名和后缀名)和文件类型
        self.child.t_file.setText(fileName)    #把文件的路径写在 Text 上

    def select_file_probit(self):
        fileName, filetype = QFileDialog.getOpenFileName(self, "选择文件", "/", "All Files (*);;TXT Files (*.txt)") # User 选择路径来找到文本文件
        # print(fileName, filetype)  # 打印文件全部路径(包括文件名和后缀名)和文件类型
        self.child.t_file_probit.setText(fileName)    #把文件的路径写在 Text 上
    
    def select_file2(self):
        fileName, filetype = QFileDialog.getOpenFileName(self, "选择文件", "/", "All Files (*);;PNG Files (*.png)") # User 选择路径来找到图片
        # print(fileName, filetype)  # 打印文件全部路径(包括文件名和后缀名)和文件类型
        self.child.t_file_2.setText(fileName)    #把文件的路径写在 Text 上

    def show_pic_orig(self):
        filename2=self.child.t_file_2.text()
        if filename2=='':
            pass
        else:
            self.child.lbl_pic_orig.setPixmap(QPixmap(filename2))  # 在label上显示图片
            self.child.lbl_pic_orig.setScaledContents (True) # 让图片自适应label大小

    def gen_word_cloud(self):
        #取得文件名
        filename=self.child.t_file.text()
        #背景图的文件名
        pic_filename=self.child.t_file_2.text()

        #分离出文件路径及文件名
        if filename=='':
            filepath='d://'
            tempfilename=self.get_sysdate()+'.png'
        else:
            (filepath,tempfilename)=os.path.split(filename)
            (tempfilename,extension)=os.path.splitext(tempfilename)
            tempfilename=tempfilename+self.get_sysdate()+'.png'
        file=os.path.join(filepath,tempfilename)
        self.
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值