wordcloud+wxpython

接上次单独的词云,想加个简单的图形界面打包成exe程序的,结果pyinstaller出来就是运行不了!好难受,难道我少了什么东西,
网上搜索了大神的wxpthon,有cdsn的王大炮,还有其他网站的某个大神,但是我找不着了又。。。感谢!果然大神都是用class的,下面是框架:
在这里插入图片描述
接下来是整段python,self用的我好懵:

import wx
from wordcloud import WordCloud
import jieba
import matplotlib.pyplot as plt
#import numpy as np
#from PIL import Image
import os

wildcard = “All files (.)|.

class Frame(wx.Frame):
def init(self, parent=None, id=-1, pos=wx.DefaultPosition,title=‘自己写’):
wx.Frame.init(self, parent, id, title,pos=(1000,200), size=(500, 400))
self.panel = wx.Panel(self)
self.path_text = wx.TextCtrl(self.panel)
self.openBtn = wx.Button(self.panel,label=‘打开’)
self.openBtn.Bind(wx.EVT_BUTTON, self.OnOpen)
self.saveAsBtn = wx.Button(self.panel,label=‘另存为’)
self.saveAsBtn.Bind(wx.EVT_BUTTON, self.OnSaveAs)
self.content_text= wx.TextCtrl(self.panel,style = wx.TE_MULTILINE|wx.HSCROLL)
#下面是窗口拉大拉下自适应
self.box = wx.BoxSizer()

    self.box.Add(self.path_text,proportion = 5,flag = wx.EXPAND|wx.ALL,border = 3)
    self.box.Add(self.openBtn,proportion = 2,flag = wx.EXPAND|wx.ALL,border = 3)
    self.box.Add(self.saveAsBtn,proportion = 2,flag = wx.EXPAND|wx.ALL,border = 3)
    
    self.v_box = wx.BoxSizer(wx.VERTICAL)
    
    self.v_box.Add(self.box,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
    self.v_box.Add(self.content_text,proportion = 5,flag = wx.EXPAND|wx.ALL,border = 3)
    
    self.panel.SetSizer(self.v_box)
    
def OnOpen(self, event):
    dlg = wx.FileDialog(self, message='打开',defaultDir='',defaultFile='',
                        
                        wildcard=wildcard)
    if dlg.ShowModal() == wx.ID_OK:
        file1 = dlg.GetPath()
        self.path_text.SetValue(file1)
        with open(file1,'r',encoding='utf-8') as f:
            self.string_data = f.read()
            self.content_text.SetValue(self.string_data)   
        dlg.Destroy() 
        
def OnSaveAs(self, event):
    dlg = wx.FileDialog(self, '另存为', os.getcwd(),defaultFile='rrcut.xlsx',
                        
                        wildcard=wildcard)
    if dlg.ShowModal() == wx.ID_OK:
        file2 = dlg.GetPath()
        text = " ".join(jieba.cut(self.string_data,cut_all=True))
        llist = text.split()
        
        remove_words = [u'\n',u' ',u',',u',']
        
        for word in llist:
            for i in range(len(remove_words)):
                if word == remove_words[i]:
                    llist.remove(word)
                    
        sett = set(llist)
        dict = {}
        for item in sett:
            dict.update({item:llist.count(item)})
            dic={}
            for k,v in dict.items():
                dic[k] = v
                
        with open(file2,'w',encoding='utf-8') as f:
            for d,v in dic.items():
                f.write(d + "," + str(v) + '\n')
                
        llist = " ".join(llist)
        
        cloud = WordCloud(font_path=".\\fonts\\simhei.ttf",
                          collocations=False,
                          background_color='white',
                          width=400,
                          height=300,
                          max_words=100,
                          max_font_size=50,
                          scale=2)
        wcloud = cloud.generate(llist)
        wcloud.to_file(file2.split(".")[0] +".jpg")
        plt.imshow(wcloud,interpolation='bilinear')
        plt.axis("off")
        plt.show()
        self.path_text.SetValue("完成")
        dlg.Destroy() 

class App(wx.App):
def OnInit(self):
self.frame = Frame()
self.frame.Show(True)
self.SetTopWindow(self.frame) # 设置顶层框架
return True

def main():
app = App()
app.MainLoop()

if name == ‘main’:
main()

好像对我的工作没啥用了。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值