汉语拼音学习机(Flask,详细代码,帮助学习)

汉语拼音学习机

这个汉语拼音学习机的界面:
在这里插入图片描述

开发软件

Pycharm

代码如下:

import tkinter
import tkinter.scrolledtext
import tkinter.messagebox
from pypinyin.phrases_dict import phrases_dict
from pypinyin.pinyin_dict import pinyin_dict


root = tkinter.Tk()

root.geometry("700x400+200+200")
root.title("汉语拼音词组学习机")
root.resizable(True,True)

#在窗口上创建一个标签
(tkinter.Label(root,text='请输入要查询的内容:',
               anchor="e",font=("microsoft yahei",16))
.place(x=10,y=10,width=200,height=40))

#用来输入查询内容的单行文本框
entrySearch = tkinter.Entry(root,
                            font=("microsoft yahei",16))
entrySearch.place(x=220,y=10,width=180,height=40)

#0表示查单字节拼音,1表示查词组
search_type = tkinter.IntVar(root,value=0)
radioPinyin = tkinter.Radiobutton(root,text='查单字拼音',
                                  variable = search_type,
                                  value=0)
radioPinyin.place(x=410,y=20,width=80,height=20)
radioPhrase = tkinter.Radiobutton(root,text='查词组',
                                  variable = search_type,
                                  value=1)
radioPhrase.place(x=500,y=20,width=60,height=20)

#True表示包含词组拼音,False表示不包含词组拼音
include_pinyin = tkinter.BooleanVar(root,value=False)
checkbuttonInclude = tkinter.Checkbutton(root,variable=include_pinyin,
                                         text='包含词组拼音',
                                         onvalue=True,
                                         offvalue = False)
checkbuttonInclude.place(x=580,y=20,width=100,height=20)

#创建按钮和按钮单击事件处理函数
def search():
    #删除多行文本框中原来的内容
    textContent.delete('0.0',tkinter.END)

    #获取并检查用户输入的带查询的内容
    user_input = entrySearch.get().strip()
    if not user_input:
        tkinter.messagebox.showinfo('提示','查询内容不能为空')
        return
    #查单个字的所有读音
    if search_type.get() ==0:
        if len(user_input)>1:
            tkinter.messagebox.showinfo('提示','只能查单个字的读音')
            return
        #查询拼音字典,如果有的话只会有一个包含所有读音的查询结果
        for num, pinyin in pinyin_dict.items():
            if chr(num) == user_input:
                textContent.insert(tkinter.INSERT,
                                   f'"{user_input}"的拼音有:\n{pinyin}')
                return
        else:
            tkinter.messagebox.showinfo('抱歉','你太厉害了,我都不认识这个字')
    #查词组
    elif search_type.get()==1:
        #是否有查询结果
        flag = False
        for phrase,pinyin in phrases_dict.items():
            #用户输入的字符串不作为整体对待,每个字单独处理
            #查询同时包含每个字的词组
            if all(map(phrase.count,user_input)):
                #是否输出词组的拼音
                if include_pinyin.get():
                    msg = f'{phrase}:{pinyin}\n'
                else:
                    msg = f'{phrase}\n'
                textContent.insert(tkinter.INSERT,msg)
                flag = True
            if not flag:
                tkinter.messagebox.showinfo('抱歉','我的小脑瓜里找不到这样的词组')
                return
(tkinter.Button(root,text='查询',command=search,
                font=('microsoft yahei',16))
.place(x=10,y=60,width=60,height=40))

#显示查询结果的多行文本框
textContent = tkinter.scrolledtext.ScrolledText(root,
                                                font=('microsoft yahei',16))
textContent.place(x=10,y=110,width=680,height=280)

root.mainloop()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值