1. 程式人生 > >python實戰專案詞雲生成器(wordcloud+jieba+pyinstaller打包)——詞雲生成軟體

python實戰專案詞雲生成器(wordcloud+jieba+pyinstaller打包)——詞雲生成軟體

最近學習了python的jieba分詞庫和wordcloud詞雲庫,誕生了想寫個小demo,使用python實現文章的詞雲圖的繪製,然後需要具有互動介面,並且能夠在沒有python環境的電腦下執行,方便不懂程式設計的人直接使用。

全部程式碼實現的打包exe檔案:WordCloudTool免安裝版

主要使用的庫和軟體如下:

python3.7.1  實現演算法程式語言 jieba 詞語分割 wordcloud 繪製雲圖 matplotlib.pyplot 顯示/儲存雲圖 注意,預設上述環境和庫以安裝完畢!  

介面結構設計

登入介面如下:

註冊頁面如下:

檔案選擇頁面如下:

完整程式碼如下:

#_*_coding:utf-8_*_
from tkinter import filedialog
import tkinter as tk
import jieba
import tkinter.messagebox
import pickle
import wordcloud
import matplotlib

#建立一個窗體
window = tk.Tk() #建立以個視窗
window.title("詞雲生成器") #命名窗體
window.geometry('450x300') #設定窗體大小
window.resizable(False, False)

# welcome image
canvas = tk.Canvas(window, height=300, width=450)
image_file = tk.PhotoImage(file='bg.gif')
image = canvas.create_image(0,0, anchor='nw', image=image_file)
canvas.pack(side=tk.LEFT)

# user information
#tk.Label(window, text='User name: ').place(x=50, y= 150)
#tk.Label(window, text='Password: ').place(x=50, y= 190)

var_usr_name = tk.StringVar()
var_usr_name.set('
[email protected]
') entry_usr_name = tk.Entry(window, textvariable=var_usr_name) entry_usr_name.place(x=160, y=150) var_usr_pwd = tk.StringVar() entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*') entry_usr_pwd.place(x=160, y=190) def wordclounds_w(): def Open_file(): try: filename = filedialog.askopenfilename( \ initialdir='C:', title="Please select txt file", \ filetypes=(("txt檔案", "*.txt"), ("所有檔案", "*.*"))) except FileNotFoundError: pass f = open(filename, "r", encoding="utf-8") t = f.read() f.close() ls = jieba.lcut(t) txt = " ".join(ls) w = wordcloud.WordCloud(font_path="msyh.ttf", width=1000, height=700, background_color="white", max_words=15) w.generate(txt) #以檔名輸出為.png圖片 lists = list(reversed(filename)) for i in range(len(lists)): if lists[i] == "/": break w.to_file("".join(reversed(lists[4:i]))+".png") window_w = tk.Toplevel(window) window_w.geometry('450x300') window_w.title('select file window') window_w.resizable(False, False) # txt introduction # welcome image canvas = tk.Canvas(window_w, height=300, width=450) image_files = tk.PhotoImage(file='slg.gif') images = canvas.create_image(0, 0, anchor='nw', image=image_files) canvas.pack(side=tk.LEFT) btn_confirm_sign_up = tk.Button(window_w, text='Please open the txt file!', command=Open_file) btn_confirm_sign_up.place(x=130, y=200) window_w.mainloop() #usr_login def usr_login(): usr_name = var_usr_name.get() usr_pwd = var_usr_pwd.get() try: with open('usrs_info.pickle', 'rb') as usr_file: usrs_info = pickle.load(usr_file) except FileNotFoundError: with open('usrs_info.pickle', 'wb') as usr_file: usrs_info = {'admin': 'admin'} pickle.dump(usrs_info, usr_file) if usr_name in usrs_info: if usr_pwd == usrs_info[usr_name]: tk.messagebox.showinfo(title='Welcome', message='Welcome use this tool! ' + usr_name) wordclounds_w() else: tk.messagebox.showerror(message='Error, your password is wrong, try again.') else: is_sign_up = tk.messagebox.askyesno('Welcome', 'You have not sign up yet. Sign up today?') if is_sign_up: usr_sign_up() #usr_sign_up def usr_sign_up(): def sign_to_Mofan_Python(): np = new_pwd.get() npf = new_pwd_confirm.get() nn = new_name.get() with open('usrs_info.pickle', 'rb') as usr_file: exist_usr_info = pickle.load(usr_file) if np != npf: tk.messagebox.showerror('Error', 'Password and confirm password must be the same!') elif nn in exist_usr_info: tk.messagebox.showerror('Error', 'The user has already signed up!') else: exist_usr_info[nn] = np with open('usrs_info.pickle', 'wb') as usr_file: pickle.dump(exist_usr_info, usr_file) tk.messagebox.showinfo('Welcome', 'You have successfully signed up!') window_sign_up.destroy() window_sign_up = tk.Toplevel(window) window_sign_up.geometry('350x200') window_sign_up.title('Sign up window') window_sign_up.resizable(False, False) new_name = tk.StringVar() new_name.set('
[email protected]
') tk.Label(window_sign_up, text='User name: ').place(x=10, y=10) entry_new_name = tk.Entry(window_sign_up, textvariable=new_name) entry_new_name.place(x=150, y=10) new_pwd = tk.StringVar() tk.Label(window_sign_up, text='Password: ').place(x=10, y=50) entry_usr_pwd = tk.Entry(window_sign_up, textvariable=new_pwd, show='*') entry_usr_pwd.place(x=150, y=50) new_pwd_confirm = tk.StringVar() tk.Label(window_sign_up, text='Confirm password: ').place(x=10, y=90) entry_usr_pwd_confirm = tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*') entry_usr_pwd_confirm.place(x=150, y=90) btn_confirm_sign_up = tk.Button(window_sign_up, text='Sign up', command=sign_to_Mofan_Python) btn_confirm_sign_up.place(x=150, y=130) # login and sign up button btn_login = tk.Button(window, text='Login', command=usr_login) btn_login.place(x=170, y=230) btn_sign_up = tk.Button(window, text='Sign up', command=usr_sign_up) btn_sign_up.place(x=270, y=230) window.mainloop()