Python_Gooey和pyinstaller打造易用的工具

发布时间 2023-04-28 17:54:46作者: 辰令

Python沟通

Python 搭建 GUI 界面时,首选Gooey ,然后 PyQt5 和 Tkinter, 
Pyinstaller
  : --paths 后接第三方模块的路径,多个路径直接用逗号分隔(英文逗号)
   -F 后接源文件路径
   使用-F,只生成一个大的可执行文件
   --clean 表示清理打包完成后的临时文件(可选,但建议写上)
  打包多个.py文件

    打包的py文件(并记录好文件路径),以及第三方库的路径
    
    我的源文件路径 D:\《Numpy数据处理详解》电子书\打包\pyinstaller学习.py
    
    我的用到的第三方库  C:\Users\huawei\AppData\Roaming\Python\Python39\site-packages\pandas,xlwings
     
    pyinstaller -F pyinstaller学习.py --paths C:\Users\huawei\AppData\Roaming\Python\Python39\site-packages\pandas,xlwings --clean

安装

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple Gooey
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple jieba

需求示例

功能:输入:文件地址,输出:显示词频最高的十个词语
       输入是GUI的形式
	      地址框,执行框
原理: 打包第三方库		

代码示例

#!/usr/bin/python3
# -*- coding: utf-8 -*- 

import jieba
from gooey import Gooey, GooeyParser
import argparse

def get_file_words(input_file):
    words =[]
    with open(input_file,mode="r",encoding="utf8") as fw:
        lines = fw.readlines()
        for line in lines:
            line = line.replace("\r","").replace("\r","").strip().replace(' ','')
            cut_word_list = jieba.cut(line,cut_all=False) 
            stopwords=[':','“','!','”',' ',',','、']
            for sig_word in cut_word_list:
                if sig_word not in stopwords:
                    words.append(sig_word)
    return words

def get_freq(words):
    # 统计每一个汉字的出现次数,使用字典的形式进行统计
    result = {}
    for word in words:
        res = result.get(word, 0)
        if res == 0:
            result[word] = 1
        else:
            result[word] = result[word] + 1

    result = sorted(result.items(),  key=lambda kv:(kv[1], kv[0]), reverse=True)
    return result
  
def regular_main():
    parser =  argparse.ArgumentParser(description="My Cool !")
    parser.add_argument('--filename', help="name of the file to process", default=r'D:\annotation\info.txt') 
    args = parser.parse_args() 
    exp_file_nm = args.filename
    exp_word = get_file_words(exp_file_nm)
    exp_freq = get_freq(exp_word)
    print(exp_freq[:5])

### 待完善
@Gooey(target="stat word freq")
def main():
    parser =  GooeyParser(description="My Cool !")
    parser.add_argument('filename',
                        metavar='Input file',
                        help='The file for which you want to static freq',
                        widget='FileChooser')
    args = parser.parse_args() 
    print(args)
    # exp_file_nm = str(args.filename)
    # print(exp_file_nm)
    # exp_word = get_file_words(exp_file_nm)
    # exp_freq = get_freq(exp_word)
    # print(exp_freq[:5]) 

if __name__ == '__main__':  
    #regular_main()
    main()

参考

Python的tkinter和pyinstaller打造易用的工具  https://www.cnblogs.com/ytwang/p/15111997.html