python gui tkinter菜鸟编程 练习 ch17_22 在编写文字程序时,如果想要让程序更完整可以设计拼写检查功能,其实在本节并没有介绍Text控件的新功能,这算是一个应用的专题程序。
程序实例ch17_22.py:设计一个小字典myDict.txt,然后将Text控件的每个单词与字典的单词做比较,如果有不符的单词则用红色显示此单词。
这个程序另外两个功能按钮,“拼写检查”按钮可以执行拼写检查,“清除”按钮可以将红色显示的字改为正常显示。

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @FileName  :ch17_22_拼写检查.py
# @time      :2022/11/17 19:35
# Author     :jiangjianming

# TODO

# TODO
"""
在编写文字程序时,如果想要让程序更完整可以设计拼写检查功能,其实在本节并没有介绍Text控件的新功能,这算是一个应用的专题程序。
程序实例ch17_22.py:设计一个小字典myDict.txt,然后将Text控件的每个单词与字典的单词做比较,如果有不符的单词则用红色显示此单词。
这个程序另外两个功能按钮,“拼写检查”按钮可以执行拼写检查,“清除”按钮可以将红色显示的字改为正常显示。
"""
from tkinter import *
from tkinter.font import Font
from tkinter.ttk import *
def spllingCheck():
    text.tag_remove("spellErr","1.0",END)
    textwords = text.get("1.0",END).split()
    print("字典的内容",textwords)

    startChar = ("(")
    endChar = (".",",",";",":","?","!",")")

    start = "1.0"
    for word in textwords:
        if word[0] in startChar:
            word = word[1:]
        if word[-1] in endChar:
            word = word[:-1]
        if(word not in dicts and word.lower() not in dicts):
            print("error:",word)
            pos = text.search(word,start,END)
            text.tag_add("spellErr",pos,"%s+%dc"%(pos,len(word)))
            # start = "%s+%dc"%(pos,len(word))  #更新搜索寻起始位置
            pos = "%s+%dc"%(pos,len(word))

def clrText():
    text.tag_remove("spellErr","1.0",END)
root= Tk()
root.title("ch17_22")
root.geometry("300x180")

#建立工具栏
toolbar = Frame(root,relief=RAISED,borderwidth=1)
toolbar.pack(side=TOP,fill=X,padx=2,pady=1)

chkbtn = Button(toolbar,text="拼写检查",command=spllingCheck)
chkbtn.pack(side=LEFT,padx=5,pady=5)

clrbtn = Button(toolbar,text="清除",command=clrText)
clrbtn.pack(side=LEFT,padx=5,pady=5)

#建立text
text = Text(root,undo=True)
text.pack(fill=BOTH,expand=True)
text.insert(END,"I find it difficult to make my point fully understood by my students.\n")
text.insert(END,"I find it difficult to drive my students home.\n")
text.insert(END," I want to point out that it is difficult to give my students lessons on driving.\n")
text.insert(END," My point is driving is especially difficult for my students.\n")
# text.insert(END,"\n")

text.tag_configure("spellErr",foreground="red")
with open("mydict.txt",'r')as f00:
    dicts = f00.read().split("\n")
root.mainloop()

python字母图案编程 python编写字母标红_拼写检查


原代码存在一个问题,只能检测第一次错误的单词,第二次出现错误的单词则不能检测

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @FileName  :ch17_22_拼写检查.py
# @time      :2022/11/17 19:35
# Author     :jiangjianming

# TODO

# TODO
"""
在编写文字程序时,如果想要让程序更完整可以设计拼写检查功能,其实在本节并没有介绍Text控件的新功能,这算是一个应用的专题程序。
程序实例ch17_22.py:设计一个小字典myDict.txt,然后将Text控件的每个单词与字典的单词做比较,如果有不符的单词则用红色显示此单词。
这个程序另外两个功能按钮,“拼写检查”按钮可以执行拼写检查,“清除”按钮可以将红色显示的字改为正常显示。
"""
from tkinter import *
from tkinter.font import Font
from tkinter.ttk import *
def spllingCheck():
    text.tag_remove("spellErr","1.0",END)
    textwords = text.get("1.0",END).split()
    print("字典的内容",textwords)

    startChar = ("(")
    endChar = (".",",",";",":","?","!",")")

    start = "1.0"
    for word in textwords:
        if word[0] in startChar:
            word = word[1:]
        if word[-1] in endChar:
            word = word[:-1]
        if(word not in dicts and word.lower() not in dicts):
            print("error:",word)
            pos = text.search(word,start,END)
            text.tag_add("spellErr",pos,"%s+%dc"%(pos,len(word)))
            start = "%s+%dc"%(pos,len(word))  #更新搜索寻起始位置
            # pos = "%s+%dc"%(pos,len(word))

def clrText():
    text.tag_remove("spellErr","1.0",END)
root= Tk()
root.title("ch17_22")
root.geometry("300x180")

#建立工具栏
toolbar = Frame(root,relief=RAISED,borderwidth=1)
toolbar.pack(side=TOP,fill=X,padx=2,pady=1)

chkbtn = Button(toolbar,text="拼写检查",command=spllingCheck)
chkbtn.pack(side=LEFT,padx=5,pady=5)

clrbtn = Button(toolbar,text="清除",command=clrText)
clrbtn.pack(side=LEFT,padx=5,pady=5)

#建立text
text = Text(root,undo=True)
text.pack(fill=BOTH,expand=True)
text.insert(END,"I find it difficult to make my point fully understood by my students.\n")
text.insert(END,"I find it difficult to drive my students home.\n")
text.insert(END," I want to point out that it is difficult to give my students lessons on driving.\n")
text.insert(END," My point is driving is especially difficult for my students.\n")
# text.insert(END,"\n")

text.tag_configure("spellErr",foreground="red")
with open("mydict.txt",'r')as f00:
    dicts = f00.read().split("\n")
root.mainloop()

python字母图案编程 python编写字母标红_拼写检查_02


迭代后的程序,可以将所有不匹配的单词标红。

if(word not in dicts and word.lower() not in dicts):
            print("error:",word)
            pos = text.search(word,start,END)
            text.tag_add("spellErr",pos,"%s+%dc"%(pos,len(word)))
            start = "%s+%dc"%(pos,len(word))  #更新搜索寻起始位置
            # pos = "%s+%dc"%(pos,len(word))

主要修改点