首页 > 编程语言 >【Python新手参考】带界面的英文单词计数器

【Python新手参考】带界面的英文单词计数器

时间:2023-09-11 18:48:03浏览次数:48  
标签:count Python win 英文单词 tk words text 新手 pack

事情经过

昨天晚上用电脑写作文,由于不放心Word的计词器,一时又找不到合适的工具,于是索性自己写了一个。那么为什么要带界面呢?原因是我曾经尝试过input(),但是它不能处理文本中的换行,所以只能将tkinter.Text作为输入框。

写完之后我发现这个东西似乎还有点参考价值,故post出来。

包含功能

  • 单词、字符、句子、段落计数器
  • 自动更新统计信息
  • 界面和多行输入
  • 快速清空、粘贴

源码

作为一个送给新手的参考代码,这东西必须开源,我打算让它遵循DWTFYWWI开原许可,即“Do Whatever The Fxxk You Want With It”。

GitHub仓库:https://github.com/TotoWang-hhh/en_word_count/

什么?你不想去神奇的GitHub?

 1 import tkinter as tk
 2 import tkinter.ttk as ttk
 3 import threading
 4 import pyclip
 5 
 6 def _count(word):
 7     lines=word.split('\n')
 8     while '' in lines:
 9         lines.remove('')
10     words=[]
11     for line in lines:
12         lineword=line.split(' ')
13         for lw in lineword:
14             words.append(lw)
15     while '' in words:
16         words.remove('')
17     sentencecount=0
18     charindex=0
19     notriperiod=word.replace('...','') #Ignore ellipsis
20     for char in notriperiod:
21         if char=='.':
22             sentencecount+=1
23         charindex+=1
24     #words.remove(' ')
25     #words.remove('\n')
26     return len(words),sentencecount,len(lines)
27 
28 def count():
29     wordcount,sentencecount,paracount=_count(txtinput.get(1.0,tk.END))
30     countlabel['text']=str(wordcount)+' Words'+' | '+str(len(txtinput.get(1.0,tk.END))-1)+' Chars'+\
31                        ' | '+str(sentencecount)+' Sentences (Periods)'+' | '+str(paracount)+' Paras (Lines)'
32 
33 def updatecount():
34     end=False
35     while win.winfo_exists() and not end:
36         try:
37             count()
38         except:
39             print('EXIT')
40             end=True
41             break
42     print('EXIT')
43     exit()
44 
45 def paste():
46     txtinput.insert(tk.INSERT,pyclip.paste(encoding='utf-8'))
47 
48 win=tk.Tk()
49 win.title('English Word Count')
50 win.minsize(640,360)
51 win.geometry('640x360')
52 
53 txtinput=tk.Text(win,bd=0)
54 
55 tk.Label(win,text='2023 By rgzz666').pack(side=tk.BOTTOM,fill=tk.X)
56 
57 countrow=tk.Frame(win)
58 
59 ttk.Button(countrow,text='REFRESH',command=count).pack(side=tk.RIGHT)
60 ttk.Button(countrow,text='↑ PASTE',command=paste).pack(side=tk.RIGHT)
61 ttk.Button(countrow,text='X CLEAR',command=lambda:txtinput.delete(1.0,tk.END)).pack(side=tk.RIGHT)
62 
63 countlabel=tk.Label(countrow,text='Please press REFRESH first',anchor='w')
64 countlabel.pack(fill=tk.X)
65 
66 countrow.pack(side=tk.BOTTOM,fill=tk.X)
67 
68 txtinput.pack(fill=tk.BOTH,expand=True)
69 
70 update_t=threading.Thread(target=updatecount)
71 update_t.start()
72 
73 win.mainloop()

瞎几把讲解

_count()

传入文本,返回单词、句子、段落计数

count()

获取输入框内容,调用_count()计数,然后修改统计信息

updatecount()

循环调用count(),自动更新统计信息

paste()

通过pyclip粘贴剪贴板内容到输入框

界面部分

想学的自己先去看tkinter教程,懒得讲

什么?你怎么知道我又在这里藏了东西

标签:count,Python,win,英文单词,tk,words,text,新手,pack
From: https://www.cnblogs.com/TotoWang/p/en_word_count.html

相关文章

  • python pandas学习
    importpandasaspdm_list=[('join',25,'male'),('1isa',30,'female'),('david','18','male')]df=pd.DataFrame(m_list,columns=['Name','age','gend......
  • 海康工业相机使用Python成像,web实时预览(Linux)
    Python实现海康机器人工业相机的实时显示视频流及拍照功能(Linux)代码是在ubuntu系统的orinnano板子上跑的程序,有需要的大佬自行研究更改支持网口相机和usb口相机并且理论上window和Linux通用但是我没有试windows平台代码如下:importsysfromctypesimport*importo......
  • Python - 桌面自动化(PyAutoGUI)
    一、安装windows:pipinstallpyautogui-ihttps://pypi.tuna.tsinghua.edu.cn/simplemac:pipinstallpyobjc-corepipinstallpyobjcpipinstallpyautoguilinux:sudoapt-getinstallscrotpython3-tkpython3-devpipinstallpython3-xlibpipinstallpyautog......
  • Python的requests.post函数上传文件和其他数据
    当使用Python的requests.post函数时,可以在其中添加异常处理来捕获可能的网络错误或HTTP错误。以下是一个示例代码,演示如何使用try-except语句来处理requests.post可能抛出的异常:importrequestsurl='http://cbim.com/upload'files={'file1':('file1.txt',open('file1.t......
  • python一键过杀软
    python过杀软新利用python加载shellcode过360、火绒等杀软先上代码将以下代码保存到mt.pyimportbase64importosimportshutilbuf=b"这里替换shellcode"b64buf=base64.b64encode(buf)lzsds="""importctypesimportbase64shellcode=base64.b64decode(\"......
  • 用python爬取天气
    之前做过这么个小网站,能够爬取天气,然后感觉没什么用,有上网站的时间用手机都看完了,然后就寻思能不能发到自己微信或者qq或者邮箱里先写下怎么把数据提出来 importrequestsimportjsonurl=你自己的网址result=requests.get(url)#print(result.text)data=json.loads......
  • python读写xlsx文件
    importosimporttracebackfromopenpyxlimport*fromlogs.loginimportlogginfromopenpyxl.reader.excelimportload_workbookclassread_data_calss:file_name=r'../../Data/data.xlsx'#在当前路劲执行video_list=[]try:'&......
  • python读取yml文件
    classRead_data_class:defread_yml_def(self,dir_path):withopen(dir_path,'r',encoding='utf-8')asf:yaml_list=yaml.safe_load(f)returnyaml_listifname=='main':passclassmain():path=os.pa......
  • Python——15days
    双层语法糖三层语法糖(多层)装饰器的修复技术(了解)有参装饰器*装饰器的写法:自上而下           执行:自下而上双层:@login_auth@outer执行顺先先执行outer——被装饰名字作为参数传入outer里,get_time=outer(index)。通过调用返回值(前提是如果有双层及以上装饰器......
  • Python工具箱系列(四十二)
    RAR文件操作​RAR是广受好评,使用广泛的压缩格式,开发者为尤金·罗谢尔(俄语:ЕвгенийЛазаревичРошал,拉丁转写:YevgenyLazarevichRoshal),RAR的全名是“RoshalARchive”,即“罗谢尔的归档”之意。尤其是winrar一度成为windows上的必备软件。 它的特点如下:​......