Day25 用python写UI-聊聊Text(二)

Text会讲三天,因为发现东西有点多,怕放在一篇会爆炸。

♠♣今天的文章大纲♥♦

  • 复原与重复
  • 搜寻
  • 拼字check

复原与重复

import tkinter as tk
from tkinter import messagebox

root = tk.Tk()
root.title('cuteluluWindow')
root.configure(bg="#7AFEC6")
root.iconbitmap('heart_green.ico')
root.geometry('300x100')

def cut():
    text.event_generate("<<Cut>>")

def copy():
    text.event_generate("<<Copy>>")

def paste():
    text.event_generate("<<Paste>>")

def showPopupMenu(event):
    popupmenu.post(event.x_root,event.y_root)

def undo():
    try:
        text.edit_undo()
    except:
        print("No action")

def redo():
    try:
        text.edit_redo()
    except:
        print("No action")

popupmenu=tk.Menu(root,tearoff=False)
popupmenu.add_command(label="cut",command=cut)
popupmenu.add_command(label="copy",command=copy)
popupmenu.add_command(label="paste",command=paste)

root.bind("<Button-3>",showPopupMenu)

toolbar=tk.Frame(root,relief="raised",borderwidth=1)
toolbar.pack(side="top",fill="x",padx=2,pady=1)

undoBtn = tk.Button(toolbar, text="Undo", command=undo)
undoBtn.pack(side="left", pady=2)
redoBtn = tk.Button (toolbar, text="Redo", command=redo)
redoBtn.pack(side="left", pady=2)

text = tk.Text (root, undo=True)
text.pack(fill="both", expand=True, padx=3, pady=2)
text.insert ('end', "我没有说谎 我何必说谎\n")
text.insert ('end', "你懂我的 我对你从来就不会假装 \n")
text.insert ('end', "我哪有说谎\n")
text.insert ('end', "请别以为你有多难忘 笑是真的不是我逞强\n")

root.mainloop()

执行结果⬇⬇⬇
https://ithelp.ithome.com.tw/upload/images/20211010/20140047Ma8hiyHO7h.png

按undo就会还原空白,在按redo就会回复原来的样子。


搜寻

这个排版跟前面的不一样喔~不知道大家有没有发现呢?
这个范例适用grid做排版,我们在Day3Day4Day5有讲到grid跟pack不能同时出现在同一个程序里面,一定要记得喔~不然会出错~

import tkinter as tk
from tkinter import messagebox

root = tk.Tk()
root.title('cuteluluWindow')
root.configure(bg="#7AFEC6")
root.iconbitmap('heart_green.ico')
root.geometry('300x100')

def cut():
    text.event_generate("<<Cut>>")

def copy():
    text.event_generate("<<Copy>>")

def paste():
    text.event_generate("<<Paste>>")

def showPopupMenu(event):
    popupmenu.post(event.x_root,event.y_root)

def undo():
    try:
        text.edit_undo()
    except:
        print("No action")

def redo():
    try:
        text.edit_redo()
    except:
        print("No action")
def search(): #搜寻开始
    text.tag_remove("found","1.0","end")
    start="1.0"
    key=entry.get()

    if(len(key.strip()) == 0):
        return
    while True:
        pos=text.search(key,start,"end")
        if (pos==""):
            break
        text.tag_add("found",pos,"%s+%dc" % (pos,len(key)))
        start="%s+%dc" % (pos,len(key))

popupmenu=tk.Menu(root,tearoff=False)
popupmenu.add_command(label="cut",command=cut)
popupmenu.add_command(label="copy",command=copy)
popupmenu.add_command(label="paste",command=paste)

root.bind("<Button-3>",showPopupMenu)
root.rowconfigure(1,weight=1)
root.columnconfigure(0,weight=1)

entry=tk.Entry()
entry.grid(row=0,column=0, padx=5)


toolbar=tk.Frame(root,relief="raised",borderwidth=1)
toolbar.grid(row=0,column=4,padx=2,pady=1)

undoBtn = tk.Button(toolbar, text="Undo", command=undo)
undoBtn.grid(row=0,column=3, pady=5)
redoBtn = tk.Button (toolbar, text="Redo", command=redo)
redoBtn.grid(row=0,column=2, pady=5)
seabtn=tk.Button(root,text="Search",command=search)
seabtn.grid(row=0,column=1, padx=5,pady=5)


text = tk.Text (root, undo=True)
text.grid(row=1,column=0, padx=3, pady=2)
text.insert ('end', "我没有说谎 我何必说谎\n")
text.insert ('end', "你懂我的 我对你从来就不会假装 \n")
text.insert ('end', "我哪有说谎\n")
text.insert ('end', "请别以为你有多难忘 笑是真的不是我逞强\n")

root.mainloop()

执行结果⬇⬇⬇
https://ithelp.ithome.com.tw/upload/images/20211010/20140047ITgsY63WTt.png


拼字check

import tkinter as tk
from tkinter import messagebox

root = tk.Tk()
root.title('cuteluluWindow')
root.configure(bg="#7AFEC6")
root.iconbitmap('heart_green.ico')
root.geometry('300x100')

def cut():
    text.event_generate("<<Cut>>")

def copy():
    text.event_generate("<<Copy>>")

def paste():
    text.event_generate("<<Paste>>")

def showPopupMenu(event):
    popupmenu.post(event.x_root,event.y_root)

def undo():
    try:
        text.edit_undo()
    except:
        print("No action")

def redo():
    try:
        text.edit_redo()
    except:
        print("No action")

def spelling():#拼字检查开始
    text.tag_remove("Error","1.0","end")
    textwords=text.get("1.0","end").split()
    print("Dictionary")

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

    start="1.0"
    for word in textwords:
        if word[0] in startChar:
            word=word[1:]
        if word[-1] in startChar:
            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)))
            pos="%s+%dc" % (pos,len(word))

def clr():
    text.tag_remove("spellErr","1.0","end")


popupmenu=tk.Menu(root,tearoff=False)
popupmenu.add_command(label="cut",command=cut)
popupmenu.add_command(label="copy",command=copy)
popupmenu.add_command(label="paste",command=paste)

root.bind("<Button-3>",showPopupMenu)

toolbar=tk.Frame(root,relief="raised",borderwidth=1)
toolbar.pack(side="top",fill="x",padx=2,pady=1)

chkBtn=tk.Button(toolbar,text="check",command=spelling)
chkBtn.pack(side="left", pady=2)
clrBtn=tk.Button(toolbar,text="clear",command=clr)
clrBtn.pack(side="left", pady=2)
undoBtn = tk.Button(toolbar, text="Undo", command=undo)
undoBtn.pack(side="left", pady=2)
redoBtn = tk.Button (toolbar, text="Redo", command=redo)
redoBtn.pack(side="left", pady=2)

text = tk.Text (root, undo=True)
text.pack(fill="both", expand=True, padx=3, pady=2)
text.insert ('end', "我没有说谎 我何必说谎\n")
text.insert ('end', "你懂我的 我对你从来就不会假装 \n")
text.insert ('end', "我哪有说谎\n")
text.insert ('end', "请别以为你有多难忘 笑是真的不是我逞强\n")

text.tag_configure("spellerror",foreground="red")
with open("myDict.txt","r") as dictObj: #自己建立一个记事本在同一个资料夹里当作字典
    dicts=dictObj.read().split("\n")

root.mainloop()

执行结果⬇⬇⬇
https://ithelp.ithome.com.tw/upload/images/20211010/20140047F0R2eba5ez.png


今天的这篇有做不一样的尝试,就是後一个范例会结合前面的一起呈现,所以程序码比较长,但是有标记从哪里开始是那个范例的主题,因为觉得如果只单做一个太无聊了,所以就决定用结合的方式作呈现,也可以知道之後真正用在介面上的感觉。
最後祝台湾生日快乐啦~~~
/images/emoticon/emoticon53.gif


<<:  DAY25 linebot结果展示-1

>>:  Day28 Java 注解

【Day 26】C String - Practice 1

前言 即使我们看完了 Cstring 的影片,却还是对於他蛮模糊的吧!我自己是写了一些题目後才对 C...

还原 SQL Server 资料库常见的问题 - 心得分享

DBABootcamp 在处理资料库还原的时候,常常会出现 (the database is in ...

第 18 集:Bootstrap 客制化 Sass 必备知识(上)

此篇会着重在客制化修改会用到的 sass 基础语法以及观念分为上下两集。 编译 scss 注解有分...

【Day5】从频域到 wave 的转换,浅谈虚数可以拿来 Train Model 吗?

在频域里面遭遇虚数 经过前面 4 篇的介绍我们已经知道如何萃取出声音的特徵了,我们用来训练的资料,都...

D24: 工程师太师了: 第12.5话

工程师太师了: 第12.5话 杂记: <纯靠北工程师>是个Facebook匿名社群粉专,...