目不识丁的我使用Python编写汉字注音小工具
一万点暴击伤害
人懒起来太可怕了,放了个十一充分激发了我的惰性。然后公众号就这么停了半个月,好惭愧…
新学期儿子的幼儿园上线了APP,每天作业通过app布置后,家长需要陪着孩子学习,并上传视频才算完成作业。来看看今天的课后作业吧:

看到最后的千字文,我就瞬间崩溃了,别说教孩子,我自己都一堆字不认识。老婆就是因为很多字需要手机查嫌麻烦,才把辅导孩子的任务甩给了我。平时我们中英文翻译的时候,经常使用百度翻译,那么今天我们使用Python来做一个自动注音的GUI工具吧!
Python的拼音模块
Python的模块库API,每次进去习惯第一动作,就是右键翻译为中文。可Python的拼音模块不需要这么做,因为涉及拼音等模块肯定和中文有关系,文档自然是中文的喽。
那么Python的拼音模块是什么? pypinyin
- 特性
根据词组智能匹配最正确的拼音。
支持多音字。
简单的繁体支持, 注音支持。
支持多种不同拼音/注音风格。
- 安装
pip install pypinyin
- 使用示例
>>> from pypinyin import pinyin, lazy_pinyin, Style
>>> pinyin('中心')
[['zhōng'], ['xīn']]
>>> pinyin('中心', heteronym=True) # 启用多音字模式
[['zhōng', 'zhòng'], ['xīn']]
>>> pinyin('中心', style=Style.FIRST_LETTER) # 设置拼音风格
[['z'], ['x']]
>>> pinyin('中心', style=Style.TONE2, heteronym=True)
[['zho1ng', 'zho4ng'], ['xi1n']]
>>> pinyin('中心', style=Style.BOPOMOFO) # 注音风格
[['ㄓㄨㄥ'], ['ㄒㄧㄣ']]
>>> pinyin('中心', style=Style.CYRILLIC) # 俄语字母风格
[['чжун1'], ['синь1']]
>>> lazy_pinyin('中心') # 不考虑多音字的情况
['zhong', 'xin']
# Python 3(Python 2 下把 '中心' 替换为 u'中心' 即可):
tkinter的宽与高
from tkinter import *
def center_window(width, height):
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(size)
root = Tk()
center_window(700, 700)
root.mainloop()
上面是一个tkinter设置程序居中的简单代码,其中700 700为宽高的px值,在这里没什么问题,但宽高一直都是px值么?
答案是否定的!
Text(frame, width=80, height=20, borderwidth=2, font=('黑体', '11'))
当我们使用Text文本标签时,width和height代表的是容纳字符的长度与高度。这里width代表设置80个字符的宽度,height为20个字符的高度。
程序实现
让我们先来看看实现效果吧:
界面设计
GUI的界面比较简单,只需要有一个用户文本输入,翻译按钮,结果输出即可。
可以看到说明、待注音汉字、执行结果都通过**LabelFrame**进行包裹,主要是为了美观。
整体代码
# -*- coding: utf-8 -*-
# @Author : 王翔
# @WeChat : King_Uranus
# @公众号 : 清风Python
# @GitHub : https://github.com/BreezePython
# @Date : 2019/10/10 23:19
# @Software : PyCharm
# @version :Python 3.7.3
# @File : WordsToPinyin.py
from tkinter import *
from pypinyin import pinyin
class WordsToPinyin:
def __init__(self, master=None):
self.root = master
self.user_input = None
self.translation = None
def create_frame(self, text_info):
frame = LabelFrame(self.root, text=text_info, font=('黑体', '11'), fg='red')
frame.grid(padx=10, pady=10, sticky=NSEW)
return frame
def notice(self):
frame = self.create_frame('说明')
info = "欢迎使用【清风Python】汉语注音工具\n请将待注音的汉字或句子,填写在下方的文本框内"
note = Label(frame, text=info, justify=LEFT, font=('黑体', '11'))
note.grid(sticky=EW)
def user_words(self):
frame = self.create_frame('待注音汉字')
self.user_input = Text(frame, width=80, height=10, borderwidth=2, font=('黑体', '11'))
self.user_input.grid(padx=10, pady=5)
@staticmethod
def split_words(words):
word_list = ""
tmp = ""
for string in words:
if len(bytes(string, 'utf-8')) == 3 and len(string) == 1:
if tmp != '':
word_list += tmp.ljust(6)
tmp = ""
word_list += string.ljust(5)
else:
tmp += string
return word_list
def translate(self):
self.translation.delete(0.0, END)
total_info = ''
info = self.user_input.get(1.0, END).split('\n')
for line in info:
if not line:
continue
a = self.split_words(line)
total_info += ''.join(map(lambda x: x[0].ljust(6), pinyin(line))) + '\n'
total_info += a + '\n'
self.translation.insert(1.0, total_info)
def start_translate(self):
b = Button(self.root, text='开始注音', width=15, command=self.translate)
b.grid()
def result_info(self):
frame = self.create_frame('执行结果')
self.translation = Text(frame, width=80, height=20, borderwidth=2, font=('黑体', '11'))
self.translation.grid(padx=10, pady=5)
if __name__ == '__main__':
def center_window(width, height):
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(size)
root = Tk()
center_window(700, 700)
root.resizable(width=False, height=False)
root.title('清风Python--汉字注音工具')
Main = WordsToPinyin(root)
Main.notice()
Main.user_words()
Main.start_translate()
Main.result_info()
root.mainloop()
程序打包
为了之后使用方便,我们可以通过pyinstaller将小程序打包成exe工具,这样就可以在电脑上直接使用了!
The End
OK,今天的内容就到这里,如果觉得内容对你有所帮助,欢迎点击文章右下角的“在看”。
公众号回复拼音即可获取整体代码及打包好的exe工具。
当然如果你是Pythoner欢迎访问我的github下载:https://github.com/BreezePython
期待你关注我的公众号 清风Python,如果觉得不错,希望能动动手指转发给你身边的朋友们。
作者:清风Python
目不识丁的我使用Python编写汉字注音小工具的更多相关文章
- 用Python编写博客导出工具
用Python编写博客导出工具 罗朝辉 (http://kesalin.github.io/) CC 许可,转载请注明出处 写在前面的话 我在 github 上用 octopress 搭建了个人博 ...
- 使用Python编写打字训练小程序【华为云技术分享】
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/devcloud/article/detail ...
- 使用Python编写打字训练小程序
你眼中的程序猿 别人眼中的程序猿,是什么样子?打字如飞,各种炫酷的页面切换,一个个好似黑客般的网站破解.可现实呢? 二指禅的敲键盘,写一行代码,查半天百度-那么如何能让我们从外表上变得更像一个程序猿呢 ...
- python开发目录合并小工具 PathMerge
前言 这个程序陆陆续续开发了几天,正好我在学Python,就一边做一边学,倒是学到不少东西. 不得不说python是快速开发的好工具. 程序做了一些改进,这两天又忙着毕设,现在才想起来发到博客上.想想 ...
- python 3.6 MJ小工具
2017.07.14 update 做了个界面,不需要使用cmd命令行+文件路径的方式来使用了: 链接如下: http://www.cnblogs.com/chenyuebai/p/7150382.h ...
- sharedb结合elementUi编写的实时小工具
我是使用sharedb 作为后端 ,然后前端使用的elementUI样式,编写的一个值班小工具.接下来,让我们先来了解一下sharedb是什么吧? sharedb工具 github地址:https:/ ...
- Python编写的记事本小程序
用Python中的Tkinter模块写的一个简单的记事本程序,Python2.x和Python3.x的许多内置函数有所改变,所以以下分为Python2.x和Python3.x版本. 一.效果展示: 二 ...
- Python编写的ARP扫描工具
源码如下: rom scapy.all import * import threading import argparse import logging import re logging.getLo ...
- python转exe的小工具
其实只是在cxfreeze的基础上加了个壳,做成窗口软件了 使用了pyqt做的界面,软件发布在了开源中国上,可以直接去下面的地址查看 http://git.oschina.net/robocky/py ...
随机推荐
- NOIP模拟 11
差点迟到没赶上开题 开题后看了T1,好像一道原题,没分析复杂度直接敲了个NC线段树,敲了个暴力,敲了个对拍,就1h了.. 对拍还对出错了,发现标记下传有点问题,改了以后对拍通过,就把T1扔掉看T2 觉 ...
- 转载: ubuntu13.04下载android4.0.1源码过程
转自:http://blog.csdn.net/zhanglongit/article/details/9263009,中间有些不行的地方进行了些小修改. 最初我参考的是老罗的博客http://blo ...
- Xshell6配置ssh免密码登录虚拟机
首先先说明一下有密码的,涉及到root登陆权限的问题: 1.用超级管理员身份登录,修改 vi /etc/ssh/sshd_config, 找到 把其中的permitRootLogin 修改成: # ...
- python_day2(列表,元组,字典,字符串)
1.bytes数据类型 msg = '我爱北京天安门' print(msg.encode(encoding="utf-8")) print(msg.encode(encoding= ...
- vue使用一些外部插件及样式的配置
一.配置全局css及js样式 1.首先将事先写好的css文件及js文件放在src文件目录下的assets文件下 2.在main.js文件输上图右边两个红色框的代码 二.配置全局jQuery及boots ...
- (Codeforce)Correct Solution?
One cold winter evening Alice and her older brother Bob was sitting at home near the fireplace and g ...
- CentOS7下安装带用户认证的squid服务器(无防火墙)
1 安装squid服务: yum install squid 安装htpasswd : yum install httpd-tools 2 配置squid配置文件 #该定义需在 ...
- nyoj 163 Phone List(动态字典树<trie>) poj Phone List (静态字典树<trie>)
Phone List 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Given a list of phone numbers, determine if it i ...
- 领扣(LeetCode)Fizz Buzz 个人题解
写一个程序,输出从 1 到 n 数字的字符串表示. 1. 如果 n 是3的倍数,输出“Fizz”: 2. 如果 n 是5的倍数,输出“Buzz”: 3.如果 n 同时是3和5的倍数,输出 “FizzB ...
- supervisor服务
描述: 遇到各种各样的各种坑, 可以通过python2 的pip安装, 可以通过apt安装, 不支持python3: 如若用apt安装可能会自动启动并且加入开机自启(不保证成功),pip安装一定不会需 ...