《Python GUI设计 tkinter菜鸟编程》配套代码 第17章 文字区域 Text

ch17_1.py

# ch17_1.py
from tkinter import *

root = Tk()
root.title("ch17_1")

text = Text(root,height=2,width=30)
text.pack()

root.mainloop()

ch17_2.py

# ch17_2.py
from tkinter import *

root = Tk()
root.title("ch17_2")

text = Text(root,height=3,width=30)
text.pack()
text.insert(END,"Python王者归来\nJava王者归来\n")
text.insert(INSERT,"深石数字公司")

root.mainloop()

ch17_3.py

# ch17_3.py 
from tkinter import * 

root = Tk()
root.title("ch17_3")

text = Text(root,height=3,width=30) 
text.pack() 
myString = \
"""Python是一种跨平台的计算机程序设计语言。
是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。
最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,
越多被用于独立的、大型项目的开发。"""
text.insert(END,myString)

root.mainloop()

ch17_4_1.py

# ch17_4_1.py 
from tkinter import * 

root = Tk()
root.title("ch17_4_1")

# yscrollbar = Scrollbar(root)
text = Text(root,height=5,width=30,wrap="none")
# yscrollbar.pack(side=RIGHT,fill=Y)
text.pack() 
# yscrollbar.config(command=text.yview)
# text.config(yscrollcommand=yscrollbar.set)

################################################
xscrollbar = Scrollbar(root,orient=HORIZONTAL)
xscrollbar.pack(side=BOTTOM,fill=X)
xscrollbar.config(command=text.xview)
text.config(xscrollcommand=xscrollbar.set)
################################################

myString = \
"""Python是一种跨平台的计算机程序设计语言。
是一个高层次的结合了解释性、编译性、
互动性和面向对象的脚本语言。
最初被设计用于编写自动化脚本(shell),
随着版本的不断更新和语言新功能的添加,
越多被用于独立的、大型项目的开发。"""
text.insert(END,myString)

root.mainloop()

ch17_4.py

# ch17_4.py 
from tkinter import * 

root = Tk()
root.title("ch17_4")

yscrollbar = Scrollbar(root)
text = Text(root,height=5,width=30)
yscrollbar.pack(side=RIGHT,fill=Y)
text.pack() 
yscrollbar.config(command=text.yview)
text.config(yscrollcommand=yscrollbar.set)

myString = \
"""Python是一种跨平台的计算机程序设计语言。
是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。
最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,
越多被用于独立的、大型项目的开发。"""
text.insert(END,myString)

root.mainloop()

ch17_5.py

# ch17_5.py 
from tkinter import * 

root = Tk()
root.title("ch17_5")

xscrollbar = Scrollbar(root,orient=HORIZONTAL)
yscrollbar = Scrollbar(root)
text = Text(root,height=5,width=30,wrap="none")
xscrollbar.pack(side=BOTTOM,fill=X)
yscrollbar.pack(side=RIGHT,fill=Y)
text.pack() 
xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)
text.config(xscrollcommand=xscrollbar.set)
text.config(yscrollcommand=yscrollbar.set)

myString = \
"""Python是一种跨平台的计算机程序设计语言。
是一个高层次的结合了解释性、编译性、互动性
和面向对象的脚本语言。
最初被设计用于编写自动化脚本(shell),
随着版本的不断更新和语言新功能的添加,
越多被用于独立的、大型项目的开发。"""
text.insert(END,myString)
# text.insert(INSERT,myString)
root.mainloop()

ch17_6.py

# ch17_6.py 
from tkinter import * 

root = Tk()
root.title("ch17_6")

xscrollbar = Scrollbar(root,orient=HORIZONTAL)
yscrollbar = Scrollbar(root)
text = Text(root,height=5,width=30,wrap="none",bg="lightyellow")
xscrollbar.pack(side=BOTTOM,fill=X)
yscrollbar.pack(side=RIGHT,fill=Y)
text.pack(fill=BOTH,expand=True) # text.pack(fill=X,expand=True) 
xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)
text.config(xscrollcommand=xscrollbar.set)
text.config(yscrollcommand=yscrollbar.set)

myString = \
"""Python是一种跨平台的计算机程序设计语言。
是一个高层次的结合了解释性、编译性、互动性
和面向对象的脚本语言。
最初被设计用于编写自动化脚本(shell),
随着版本的不断更新和语言新功能的添加,
越多被用于独立的、大型项目的开发。"""
text.insert(END,myString)
# text.insert(INSERT,myString)
root.mainloop()

ch17_7_1.py

# ch17_7_1.py 
from tkinter import * 
from tkinter.ttk import * 
from tkinter.font import Font

def familyChanged(event):             # font family更新
    f=Font(family=familyVar.get())    # 取得新的fong family
    text.configure(font=f)            # 更新text的font family

root = Tk()
root.title("ch17_7_1")
root.geometry("300x180")

# 建立font family OptionMenu
familyVar = StringVar()
familyFamily = ("Arial","Times","Courier")
familyVar.set(familyFamily[0])
family = OptionMenu(root,familyVar,*familyFamily,command=familyChanged)
family.pack(pady=2)

# 建立Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.focus_set()

root.mainloop()

ch17_7.py

# ch17_7.py 
from tkinter import * 
from tkinter.font import Font

def familyChanged(event):             # font family更新
    f=Font(family=familyVar.get())    # 取得新的fong family
    text.configure(font=f)            # 更新text的font family

root = Tk()
root.title("ch17_7")
root.geometry("300x180")

# 建立font family OptionMenu
familyVar = StringVar()
familyFamily = ("Arial","Times","Courier")
familyVar.set(familyFamily[0])
family = OptionMenu(root,familyVar,*familyFamily,command=familyChanged)
family.pack(pady=2)

# 建立Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.focus_set()

root.mainloop()

ch17_8_1.py

# ch17_8_1.py 
from tkinter import * 
from tkinter.font import Font
from tkinter.ttk import * 

def familyChanged(event):             # font family更新
    f=Font(family=familyVar.get())    # 取得新的fong family
    text.configure(font=f)            # 更新text的font family
def weightChanged(event):             # weight family更新
    f=Font(weight=weightVar.get())    # 取得新的fong weight
    text.configure(font=f)            # 更新text的font weight

root = Tk()
root.title("ch17_8_1")
root.geometry("300x180")

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

# 建立font family OptionMenu
familyVar = StringVar()
familyFamily = ("Arial","Times","Courier")
familyVar.set(familyFamily[0])
family = OptionMenu(toolbar,familyVar,*familyFamily,command=familyChanged)
family.pack(side=LEFT,pady=2)

# 建立font weight OptionMenu
weightVar = StringVar()
weightFamily = ("normal","bold")
weightVar.set(weightFamily[0])
weight = OptionMenu(toolbar,weightVar,*weightFamily,command=weightChanged)
weight.pack(pady=3,side=LEFT)

# 建立Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.focus_set()

root.mainloop()

ch17_8.py

# ch17_8.py 
from tkinter import * 
from tkinter.font import Font

def familyChanged(event):             # font family更新
    f=Font(family=familyVar.get())    # 取得新的fong family
    text.configure(font=f)            # 更新text的font family
def weightChanged(event):             # weight family更新
    f=Font(weight=weightVar.get())    # 取得新的fong weight
    text.configure(font=f)            # 更新text的font weight

root = Tk()
root.title("ch17_8")
root.geometry("300x180")

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

# 建立font family OptionMenu
familyVar = StringVar()
familyFamily = ("Arial","Times","Courier")
familyVar.set(familyFamily[0])
family = OptionMenu(toolbar,familyVar,*familyFamily,command=familyChanged)
family.pack(side=LEFT,pady=2)

# 建立font weight OptionMenu
weightVar = StringVar()
weightFamily = ("normal","bold")
weightVar.set(weightFamily[0])
weight = OptionMenu(toolbar,weightVar,*weightFamily,command=weightChanged)
weight.pack(pady=3,side=LEFT)

# 建立Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.focus_set()

root.mainloop()

ch17_9.py

# ch17_9.py 
from tkinter import * 
from tkinter.font import Font
from tkinter.ttk import * 
def familyChanged(event):             # font family更新
    f=Font(family=familyVar.get())    # 取得新的fong family
    text.configure(font=f)            # 更新text的font family
def weightChanged(event):             # weight family更新
    f=Font(weight=weightVar.get())    # 取得新的fong weight
    text.configure(font=f)            # 更新text的font weight
def sizeSelected(event):              # size family更新
    f=Font(size=sizeVar.get())        # 取得新fong size
    text.configure(font=f)            # 取得text的font size

root = Tk()
root.title("ch17_9")
root.geometry("300x180")

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

# 建立font family OptionMenu
familyVar = StringVar()
familyFamily = ("Arial","Times","Courier")
familyVar.set(familyFamily[0])
family = OptionMenu(toolbar,familyVar,*familyFamily,command=familyChanged)
family.pack(side=LEFT,pady=2)

# 建立font weight OptionMenu
weightVar = StringVar()
weightFamily = ("normal","bold")
weightVar.set(weightFamily[0])
weight = OptionMenu(toolbar,weightVar,*weightFamily,command=weightChanged)
weight.pack(pady=3,side=LEFT)

# 建立font size Combobox
sizeVar = IntVar()
size = Combobox(toolbar,textvariable=sizeVar)
sizeFamily = [x for x in range(8,31)]
size["value"] = sizeFamily
size.current(4)
size.bind("<<ComboboxSelected>>",sizeSelected)
size.pack(side=LEFT)

# 建立Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.focus_set()

root.mainloop()

ch17_10.py

# ch17_10.py 
from tkinter import * 

def selectedText():
    try:
        selText = text.get(SEL_FIRST,SEL_LAST)
        print("选取文字:",selText)
        # print("SEL_FIRST:",type(SEL_FIRST),"  SEL_LAST:",SEL_LAST)
    except TclError:
        print("没有选取文字")

root = Tk()
root.title("ch17_10")
root.geometry("300x180")

# 建立Button
btn = Button(root,text="Print selection",command=selectedText)
btn.pack(pady=3)

# 建立Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.insert(END,"Love You Like A Love Song") # 插入文字

root.mainloop()

ch17_11.py

# ch17_11.py 
from tkinter import * 

def selectedText():
    try:
        selText = text.get(SEL_FIRST,SEL_LAST)
        print("选取文字:",selText)
        # print("SEL_FIRST:",type(SEL_FIRST),"  SEL_LAST:",SEL_LAST)
        print("selectionStart:",text.index(SEL_FIRST))
        print("selectionEnd:",text.index(SEL_LAST))
    except TclError:
        print("没有选取文字")

root = Tk()
root.title("ch17_11")
root.geometry("300x180")

# 建立Button
btn = Button(root,text="Print selection",command=selectedText)
btn.pack(pady=3)

# 建立Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.insert(END,"Love You Like A Love Song") # 插入文字

root.mainloop()

ch17_12.py

# ch17_12.py 
from tkinter import * 

def printIndex():
    print("INSERT :",text.index(INSERT))
    print("CURRENT:",text.index(CURRENT))
    print("END    :",text.index(END))
    print("******************************")

root = Tk()
root.title("ch17_12")
root.geometry("300x180")

# 建立Button
btn = Button(root,text="Print index",command=printIndex)
btn.pack(pady=3)

# 建立Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.insert(END,"Love You Like A Love Song\n") # 插入文字
text.insert(END,"梦醒时分") # 插入文字

root.mainloop()

ch17_13.py

# ch17_13.py 
from tkinter import * 

root = Tk()
root.title("ch17_13")
root.geometry("300x180")

# 建立Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.insert(END,"Love You Like A Love Song\n") # 插入文字
text.insert(1.14,"梦醒时分") # 插入文字

root.mainloop()

ch17_14.py

# ch17_14.py 
from tkinter import * 

root = Tk() 
root.title("ch17_14") 
root.geometry("300x180") 

# 建立Text 
text = Text(root) 

for i in range(1,10): 
    text.insert(END,str(i) + ' Python GUI设计王者归来 \n') 

# 设置书签 
text.mark_set("mark1","5.0") 
text.mark_set("mark2","8.0") 

print(text.get("mark1","mark2")) 
text.pack(fill=BOTH,expand=True) 

root.mainloop() 

ch17_15.py

# ch17_15.py 
from tkinter import * 

root = Tk() 
root.title("ch17_15") 
root.geometry("300x180") 
# 建立Text 
text = Text(root) 

for i in range(1,10): 
    text.insert(END,str(i) + ' Python GUI设计王者归来 \n') 

# 设置书签 
text.mark_set("mark1","5.0") # 指定标签mark1
text.mark_set("mark2","8.0") # 指定标签mark2

# 设置书签
text.tag_add("tag1","mark1","mark2") # 将mark1和mark2之间的文字命名为tag1标签
text.tag_config("tag1",foreground="blue",background="lightyellow") # 为标签执行特定的编辑或绑定
text.pack(fill=BOTH,expand=True) 

root.mainloop() 

ch17_16.py

# ch17_16.py 
from tkinter import * 
from tkinter.font import Font
from tkinter.ttk import *

def sizeSelected(event):
    f=Font(size=sizeVar.get())    # 取得新的font size 
    text.tag_config(SEL,font=f)

root = Tk() 
root.title("ch17_16") 
root.geometry("300x180") 

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

# 建立font size Combobox 
sizeVar = IntVar()
size = Combobox(toolbar,textvariable=sizeVar)
sizeFamily = [x for x in range(8,30)]
size["value"] = sizeFamily
size.current(4)
size.bind("<<ComboboxSelected>>",sizeSelected)
size.pack()

# 建立Text 
text = Text(root) 
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.insert(END,"Five Hundred Miles\n")
text.insert(END,"If you miss the rain I'm on,\n")
text.insert(END,"You will know that I am gone.\n")
text.insert(END,"You can hear the whistle blow\n")
text.insert(END,"A hundred miles,\n")
text.focus_set()

root.mainloop() 

ch17_17.py

# ch17_17.py 
from tkinter import * 
from tkinter.font import Font
from tkinter.ttk import *

def sizeSelected(event):
    f=Font(size=sizeVar.get())    # 取得新的font size 
    text.tag_config(SEL,font=f)

root = Tk() 
root.title("ch17_17") 
root.geometry("300x180") 

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

# 建立font size Combobox 
sizeVar = IntVar()
size = Combobox(toolbar,textvariable=sizeVar)
sizeFamily = [x for x in range(8,30)]
size["value"] = sizeFamily
size.current(4)
size.bind("<<ComboboxSelected>>",sizeSelected)
size.pack()

# 建立Text 
text = Text(root) 
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.insert(END,"Five Hundred Miles\n","a")           # 插入时同时设置Tag
text.insert(END,"If you miss the rain I'm on,\n")
text.insert(END,"You will know that I am gone.\n")
text.insert(END,"You can hear the whistle blow\n")
text.insert(END,"A hundred miles,\n")
text.focus_set()
# 将Tag a 设置为居中,蓝色,含下划线
text.tag_config("a",foreground="blue",justify=CENTER,underline=True)

root.mainloop() 

ch17_18.py

# ch17_18.py 
from tkinter import * 
from tkinter import messagebox
def cutJob():                            # Cut方法
    print("剪切操作执行中...")
    copyJob()                            # 复制选取文字
    text.delete(SEL_FIRST,SEL_LAST)      # 删除选取文字
def copyJob():
    print("复制操作执行中...")
    try:
        text.clipboard_clear()
        copyText = text.get(SEL_FIRST,SEL_LAST)
        text.clipboard_append(copyText)
    except TclError:
        print("没有选取...")
def pasteJob():
    print("粘贴操作执行中...")
    try:
        copyText = text.selection_get(selection="CLIPBOARD")
        text.insert(INSERT,copyText)
    except TclError:
        print("剪切板没有数据")
def showPopupMenu(event):
    print("显示弹出菜单...")
    popupmenu.post(event.x_root,event.y_root)

root = Tk() 
root.title("ch17_18") 
root.geometry("300x180") 

popupmenu = Menu(root,tearoff=False)
# 在弹出菜单内建立三个命令列表
popupmenu.add_command(label="Cut",command=cutJob)
popupmenu.add_command(label="Copy",command=copyJob)
popupmenu.add_command(label="Paste",command=pasteJob)
# 单击鼠标右键绑定显示弹出菜单
root.bind("<Button-3>",showPopupMenu)

# 建立Text 
text = Text(root) 
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.insert(END,"Five Hundred Miles\n")           # 插入时同时设置Tag
text.insert(END,"If you miss the rain I'm on,\n")
text.insert(END,"You will know that I am gone.\n")
text.insert(END,"You can hear the whistle blow\n")
text.insert(END,"A hundred miles,\n")

root.mainloop() 

ch17_19.py

# ch17_19.py 
from tkinter import * 
from tkinter import messagebox
def cutJob():                            # Cut方法
    print("剪切操作执行中...")
    text.event_generate("<<Cut>>")
def copyJob():
    print("复制操作执行中...")
    text.event_generate("<<Copy>>")
def pasteJob():
    print("粘贴操作执行中...")
    text.event_generate("<<Paste>>")
def showPopupMenu(event):
    print("显示弹出菜单...")
    popupmenu.post(event.x_root,event.y_root)

root = Tk() 
root.title("ch17_19") 
root.geometry("300x180") 

popupmenu = Menu(root,tearoff=False)
# 在弹出菜单内建立三个命令列表
popupmenu.add_command(label="Cut",command=cutJob)
popupmenu.add_command(label="Copy",command=copyJob)
popupmenu.add_command(label="Paste",command=pasteJob)
# 单击鼠标右键绑定显示弹出菜单
root.bind("<Button-3>",showPopupMenu)

# 建立Text 
text = Text(root) 
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.insert(END,"Five Hundred Miles\n")           # 插入时同时设置Tag
text.insert(END,"If you miss the rain I'm on,\n")
text.insert(END,"You will know that I am gone.\n")
text.insert(END,"You can hear the whistle blow\n")
text.insert(END,"A hundred miles,\n")

root.mainloop() 

ch17_20.py

# ch17_20.py 
from tkinter import * 
from tkinter import messagebox
def cutJob():                            # Cut方法
    print("剪切操作执行中...")
    text.event_generate("<<Cut>>")
def copyJob():
    print("复制操作执行中...")
    text.event_generate("<<Copy>>")
def pasteJob():
    print("粘贴操作执行中...")
    text.event_generate("<<Paste>>")
def showPopupMenu(event):
    print("显示弹出菜单...")
    popupmenu.post(event.x_root,event.y_root)
def undoJob():
    try:
        text.edit_undo()
    except:
        print("先前没有动作")
def redoJob():
    try:
        text.edit_redo()
    except:
        print("先前没有动作")

root = Tk() 
root.title("ch17_20") 
root.geometry("300x180") 

popupmenu = Menu(root,tearoff=False)
# 在弹出菜单内建立三个命令列表
popupmenu.add_command(label="Cut",command=cutJob)
popupmenu.add_command(label="Copy",command=copyJob)
popupmenu.add_command(label="Paste",command=pasteJob)
# 单击鼠标右键绑定显示弹出菜单
root.bind("<Button-3>",showPopupMenu)

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

# 建立Button
undoBtn = Button(toolbar,text="Undo",command=undoJob)
undoBtn.pack(side=LEFT,pady=2)
redoBtn = Button(toolbar,text="Redo",command=redoJob)
redoBtn.pack(side=LEFT,pady=2)

# 建立Text 
text = Text(root,undo=True) 
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.insert(END,"Five Hundred Miles\n")           # 插入时同时设置Tag
text.insert(END,"If you miss the rain I'm on,\n")
text.insert(END,"You will know that I am gone.\n")
text.insert(END,"You can hear the whistle blow\n")
text.insert(END,"A hundred miles,\n")

root.mainloop() 

ch17_21.py

# ch17_21.py 
from tkinter import * 

def mySearch():
    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)
        # print("pos= ",pos) # pos=  3.0  pos=  4.0  pos=     
        if (pos == ""):
            break
        text.tag_add("found",pos,"%s+%dc" %(pos,len(key)))
        start = "%s+%dc" % (pos,len(key))
        # print("start= ",start) # start=  3.0+3c  start=  4.0+3c

root = Tk() 
root.title("ch17_21") 
root.geometry("300x180") 

root.rowconfigure(1,weight=1) 
root.columnconfigure(0,weight=1) 

entry = Entry() 
entry.grid(row=0,column=0,padx=5,sticky=W+E) 

btn = Button(root,text="查找",command=mySearch)
btn.grid(row=0,column=1,padx=5,pady=5)

# 建立Text 
text = Text(root,undo=True) 
text.grid(row=1,column=0,columnspan=2,padx=3,
                pady=5,sticky=N+S+W+E)
text.insert(END,"Five Hundred Miles\n")           # 插入时同时设置Tag
text.insert(END,"If you miss the rain I'm on,\n")
text.insert(END,"You will know that I am gone.\n")
text.insert(END,"You can hear the whistle blow\n")
text.insert(END,"A hundred miles,\n")

text.tag_configure("found", background="yellow")

root.mainloop() 

ch17_22.py

# ch17_22.py 
from tkinter import * 

def spellingCheck():
    text.tag_remove("spellErr","1.0",END)
    textwords = text.get("1.0",END).split()
    print("字典内容\n",textwords)
    # print("#############dicts##############\n",dicts)
    # print("#############dicts##############")
    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)))
            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=spellingCheck)
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,"Five Hundred Miles\n")           # 插入时同时设置Tag
text.insert(END,"If you miss the rain I am on,\n")
text.insert(END,"You will knw that I am gone.\n")
text.insert(END,"You can hear the whistle blw\n")
text.insert(END,"A hunded miles,\n")

text.tag_configure("spellErr", foreground="red")
with open("myDict.txt","r") as dictObj:
    dicts = dictObj.read().split('\n')

root.mainloop() 

ch17_23.py

# ch17_23.py 
from tkinter import * 

def saveFile():
    textContent = text.get("1.0",END)
    filename = "ch17_23.txt"
    with open(filename,"w") as output:
        output.write(textContent)
        root.title(filename)

root = Tk() 
root.title("Untitled") 
root.geometry("300x180") 

menubar = Menu(root)          # 建立最上层菜单
# 建立菜单类别对象,并将此菜单命名为File
filemenu = Menu(menubar,tearoff=False)
menubar.add_cascade(label="File",menu=filemenu)
# 在File菜单内建立菜单列表
filemenu.add_command(label="Save",command=saveFile)
filemenu.add_command(label="Exit",command=root.destroy)
root.config(menu=menubar)

# 建立Text 
text = Text(root,undo=True) 
text.pack(fill=BOTH,expand=True)
text.insert(END,"Five Hundred Miles\n")           # 插入时同时设置Tag
text.insert(END,"If you miss the rain I am on,\n")
text.insert(END,"You will knw that I am gone.\n")
text.insert(END,"You can hear the whistle blw\n")
text.insert(END,"A hunded miles,\n")

root.mainloop() 

ch17_24.py

# ch17_24.py 
from tkinter import * 
from tkinter.filedialog import asksaveasfilename

def saveAsFile():
    global filename
    textContent = text.get("1.0",END)
    
    filename = asksaveasfilename()
    print("传递回来的文件路径是:   ",filename)
    if filename == "":
        return
    with open(filename,"w") as output:
        output.write(textContent)
        root.title(filename)
filename = "Untitled"
root = Tk() 
root.title(filename) 
root.geometry("300x180") 

menubar = Menu(root)          # 建立最上层菜单
# 建立菜单类别对象,并将此菜单命名为File
filemenu = Menu(menubar,tearoff=False)
menubar.add_cascade(label="File",menu=filemenu)
# 在File菜单内建立菜单列表
filemenu.add_command(label="Save As",command=saveAsFile)
filemenu.add_separator()
filemenu.add_command(label="Exit",command=root.destroy)
root.config(menu=menubar)

# 建立Text 
text = Text(root,undo=True) 
text.pack(fill=BOTH,expand=True)
text.insert(END,"Five Hundred Miles\n")           # 插入时同时设置Tag
text.insert(END,"If you miss the rain I am on,\n")
text.insert(END,"You will knw that I am gone.\n")
text.insert(END,"You can hear the whistle blw\n")
text.insert(END,"A hunded miles,\n")

root.mainloop() 

ch17_25.py

# ch17_25.py 
from tkinter import * 
from tkinter.filedialog import asksaveasfilename

def saveAsFile():
    global filename
    textContent = text.get("1.0",END)
    
    filename = asksaveasfilename(defaultextension=".txt")
    print("传递回来的文件路径是:   ",filename)
    if filename == "":
        return
    with open(filename,"w") as output:
        output.write(textContent)
        root.title(filename)
filename = "Untitled"
root = Tk() 
root.title(filename) 
root.geometry("300x180") 

menubar = Menu(root)          # 建立最上层菜单
# 建立菜单类别对象,并将此菜单命名为File
filemenu = Menu(menubar,tearoff=False)
menubar.add_cascade(label="File",menu=filemenu)
# 在File菜单内建立菜单列表
filemenu.add_command(label="Save As",command=saveAsFile)
filemenu.add_separator()
filemenu.add_command(label="Exit",command=root.destroy)
root.config(menu=menubar)

# 建立Text 
text = Text(root,undo=True) 
text.pack(fill=BOTH,expand=True)
text.insert(END,"Five Hundred Miles\n")           # 插入时同时设置Tag
text.insert(END,"If you miss the rain I am on,\n")
text.insert(END,"You will knw that I am gone.\n")
text.insert(END,"You can hear the whistle blw\n")
text.insert(END,"A hunded miles,\n")

root.mainloop() 

ch17_26.py

# ch17_26.py 
from tkinter import * 
from tkinter.filedialog import asksaveasfilename

def newFile():
    text.delete("1.0",END)
    root.title("Untitled")

def saveAsFile():
    global filename
    textContent = text.get("1.0",END)
    
    filename = asksaveasfilename(defaultextension=".txt")
    print("传递回来的文件路径是:   ",filename)
    if filename == "":
        return
    with open(filename,"w") as output:
        output.write(textContent)
        root.title(filename)
filename = "Untitled"
root = Tk() 
root.title(filename) 
root.geometry("300x180") 

menubar = Menu(root)          # 建立最上层菜单
# 建立菜单类别对象,并将此菜单命名为File
filemenu = Menu(menubar,tearoff=False)
menubar.add_cascade(label="File",menu=filemenu)
# 在File菜单内建立菜单列表
filemenu.add_command(label="New File",command=newFile)
filemenu.add_command(label="Save As",command=saveAsFile)
filemenu.add_separator()
filemenu.add_command(label="Exit",command=root.destroy)
root.config(menu=menubar)

# 建立Text 
text = Text(root,undo=True) 
text.pack(fill=BOTH,expand=True)
text.insert(END,"Five Hundred Miles\n")           # 插入时同时设置Tag
text.insert(END,"If you miss the rain I am on,\n")
text.insert(END,"You will knw that I am gone.\n")
text.insert(END,"You can hear the whistle blw\n")
text.insert(END,"A hunded miles,\n")

root.mainloop() 

ch17_27.py

# ch17_27.py 
from tkinter import * 
from tkinter.filedialog import asksaveasfilename
from tkinter.filedialog import askopenfilename

def newFile():
    text.delete("1.0",END)
    root.title("Untitled")
def openFile():
    global filename
    filename = askopenfilename()
    if filename == "":
        return
    with open(filename,"r") as fileObj:
        content = fileObj.read()
    text.delete("1.0",END)
    text.insert(END,content)
    root.title(filename)

def saveAsFile():
    global filename
    textContent = text.get("1.0",END)
    
    filename = asksaveasfilename(defaultextension=".txt")
    print("传递回来的文件路径是:   ",filename)
    if filename == "":
        return
    with open(filename,"w") as output:
        output.write(textContent)
        root.title(filename)
filename = "Untitled"
root = Tk() 
root.title(filename) 
root.geometry("300x180") 

menubar = Menu(root)          # 建立最上层菜单
# 建立菜单类别对象,并将此菜单命名为File
filemenu = Menu(menubar,tearoff=False)
menubar.add_cascade(label="File",menu=filemenu)
# 在File菜单内建立菜单列表
filemenu.add_command(label="New File",command=newFile)
filemenu.add_command(label="Open File ...",command=openFile)
filemenu.add_command(label="Save As ...",command=saveAsFile)
filemenu.add_separator()
filemenu.add_command(label="Exit",command=root.destroy)
root.config(menu=menubar)

# 建立Text 
text = Text(root,undo=True) 
text.pack(fill=BOTH,expand=True)

root.mainloop() 

ch17_28_1.py

 # ch17_28_1.py 
from tkinter import * 
from tkinter.filedialog import asksaveasfilename
from tkinter.filedialog import askopenfilename
from tkinter.scrolledtext import ScrolledText as Text

def newFile():
    text.delete("1.0",END)
    root.title("Untitled")
def openFile():
    global filename
    filename = askopenfilename()
    if filename == "":
        return
    with open(filename,"r") as fileObj:
        content = fileObj.read()
    text.delete("1.0",END)
    text.insert(END,content)
    root.title(filename)

def saveAsFile():
    global filename
    textContent = text.get("1.0",END)
    
    filename = asksaveasfilename(defaultextension=".txt")
    print("传递回来的文件路径是:   ",filename)
    if filename == "":
        return
    with open(filename,"w") as output:
        output.write(textContent)
        root.title(filename)
filename = "Untitled"
root = Tk() 
root.title(filename) 
root.geometry("300x180") 

menubar = Menu(root)          # 建立最上层菜单
# 建立菜单类别对象,并将此菜单命名为File
filemenu = Menu(menubar,tearoff=False)
menubar.add_cascade(label="File",menu=filemenu)
# 在File菜单内建立菜单列表
filemenu.add_command(label="New File",command=newFile)
filemenu.add_command(label="Open File ...",command=openFile)
filemenu.add_command(label="Save As ...",command=saveAsFile)
filemenu.add_separator()
filemenu.add_command(label="Exit",command=root.destroy)
root.config(menu=menubar)

# 建立Text 
text = Text(root,undo=True) 
text.pack(fill=BOTH,expand=True)

root.mainloop() 

ch17_28.py

# ch17_28.py 
from tkinter import * 
from tkinter.filedialog import asksaveasfilename
from tkinter.filedialog import askopenfilename
from tkinter.scrolledtext import ScrolledText #as Text

def newFile():
    text.delete("1.0",END)
    root.title("Untitled")
def openFile():
    global filename
    filename = askopenfilename()
    if filename == "":
        return
    with open(filename,"r") as fileObj:
        content = fileObj.read()
    text.delete("1.0",END)
    text.insert(END,content)
    root.title(filename)

def saveAsFile():
    global filename
    textContent = text.get("1.0",END)
    
    filename = asksaveasfilename(defaultextension=".txt")
    print("传递回来的文件路径是:   ",filename)
    if filename == "":
        return
    with open(filename,"w") as output:
        output.write(textContent)
        root.title(filename)
filename = "Untitled"
root = Tk() 
root.title(filename) 
root.geometry("300x180") 

menubar = Menu(root)          # 建立最上层菜单
# 建立菜单类别对象,并将此菜单命名为File
filemenu = Menu(menubar,tearoff=False)
menubar.add_cascade(label="File",menu=filemenu)
# 在File菜单内建立菜单列表
filemenu.add_command(label="New File",command=newFile)
filemenu.add_command(label="Open File ...",command=openFile)
filemenu.add_command(label="Save As ...",command=saveAsFile)
filemenu.add_separator()
filemenu.add_command(label="Exit",command=root.destroy)
root.config(menu=menubar)

# 建立Text 
text = ScrolledText(root,undo=True) 
text.pack(fill=BOTH,expand=True)

root.mainloop() 

ch17_29.py

# ch17_29.py 
from tkinter import * 
from PIL import Image, ImageTk

root = Tk() 
root.title("ch17_29") 
root.geometry("1000x750")
img = Image.open("bryant.jpg")
myPhoto = ImageTk.PhotoImage(img)

text = Text() 
text.image_create(END,image=myPhoto)
text.insert(END,"\n\n")
text.insert(END,"篮球巨星科比·布莱恩特")
text.pack(fill=BOTH,expand=True)

root.mainloop() 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值