接上次单独的词云,想加个简单的图形界面打包成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()
好像对我的工作没啥用了。。。。