码迷,mamicode.com
首页 > 编程语言 > 详细

用Python制作编辑器

时间:2020-11-01 10:31:40      阅读:18      评论:0      收藏:0      [点我收藏+]

标签:返回   计数   fir   粘贴   glob   cad   cut   缓冲   解码   

import tkinter
import tkinter as tk
from tkinter import *
import tkinter.filedialog
import tkinter.messagebox
import tkinter.scrolledtext
import tkinter.colorchooser
import tkinter.simpledialog


# ----------------------------------------一、创建窗口-------------------------------------------------
filename = ""  # 定义空文件名
win = tk.Tk()
win["height"] = 400
win["width"] = 600
# -----------------------------------------------------------------------------------------------------


# ---------------------------------------二、功能定义区---------------------------------------------

# -----------------------------------------1.菜单栏 File-----------------------------------------------------------

textChanged = tk.IntVar(value=0)  # 字符串计数器

# 1.打开


def Open():
    global filename
    # 1.获取字符,是否保存原来的文档
    if textChanged.get():
        yesno = tk.messagebox.askyesno(
            title="Save or not", message="Do you want to Save?")  # 询问框

        if yesno == tkinter.YES:
            Save()
        filename = tkinter.filedialog.askopenfilename(
            title="Open file", filetypes=[("Text files", "*.txt")])  # 打开路径框

    # 2.打开新的文档
    if filename:
        txtContent.delete(0.0, tk.END)  # 删除原来的文本
        fp = open(filename, "rb")  # 读取文本
        txtContent.insert(tkinter.INSERT, "".join(
            fp.read().decode(‘GBK‘)))  # 解码插入到文本编辑器
        fp.close()  # 关闭指针
        textChanged.set(0)  # 字符串置0

# 2.保存


def Save():
    global filename
    if not filename:
        SaveAs()

    elif textChanged.get():
        fp = open(filename, "w")  # 创建文本
        fp.write(txtContent.get(0.0, tkinter.END))  # 将文本框的内容导入文件
        fp.close()  # 关闭
        textChanged.set(0)  # 计数器置0

# 3.保存到


def SaveAs():
    global filename
    # 创建对话框,默认对应的路径和文件名
    newfilename = tkinter.filedialog.asksaveasfilename(
        title="Save As", initialdir=r"c:\\", initialfile="new.txt")

    # 写入文件内容
    if newfilename:
        fp = open(newfilename, "w")
        fp.write(txtContent.get(0.0, tkinter.END))
        fp.close()
        filename = newfilename

        textChanged.set(0)

# 4.关闭


def Close():
    global filename
    Save()  # 保存
    txtContent.delete(0.0, tkinter.END)  # 区域置空
    filename = ""  # 文件名置空


# -------------------------------------------2.菜单栏 Edit-----------------------------------------------------------
# 1.回退
def Undo():
    txtContent["undo"] = True
    try:
        txtContent.edit_undo()
    except Exception as e:
        pass

# 2.前进


def Redo():
    txtContent["undo"] = True
    try:
        txtContent.edit_redo()
    except Exception as e:
        pass

# 3.复制


def Copy():
    txtContent.clipboard_clear()  # 清空缓冲区
    txtContent.clipboard_append(txtContent.selection_get())  # 保存到缓冲区
    pass

# 4.剪切


def Cut():
    Copy()  # 复制文本
    txtContent.delete(tkinter.SEL_FIRST, tkinter.SEL_LAST)  # 删除文本编译器的选中文本

# 5.粘贴


def Paste():
    try:
        txtContent.insert(tkinter.SEL_FIRST,
                          txtContent.clipboard_get())  # 获取缓冲区文本
        txtContent.delete(tkinter.SEL_FIRST, tkinter.SEL_LAST)  # 删除选中文本
        return
    except Exception as e:
        pass

# 6.查找


def Search():
    textToSearch = tkinter.simpledialog.askstring(
        title="Search", prompt="What to search?")
    start = txtContent.search(textToSearch, 0.0, tkinter.END)
    if start:  # 查找到时,返回yes
        tkinter.messagebox.showinfo(title="Found", message="ok")


# --------------------------------------------3.菜单栏 Help-----------------------------------------------------------
# 1,关于
def About():
    tkinter.messagebox.showinfo(
        title="About", message="Author:Dong Fuguo")  # 弹出信息框
    pass


# -------------------------------------------------------------------------------------------------------------------

# ------------------------------------三、菜单栏框架----------------------------------
menu1 = tk.Menu(win)

# 菜单1 File
# ---------------------------------------------------------------------
submenu = tk.Menu(menu1, tearoff=0)

submenu.add_command(label="Open", command=Open)
submenu.add_separator()
submenu.add_command(label="Save", command=Save)
submenu.add_separator()
submenu.add_command(label="SaveAs", command=SaveAs)
submenu.add_separator()
submenu.add_command(label="Close", command=Close)

menu1.add_cascade(label="File", menu=submenu)
# -----------------------------------------------------------------------


# 菜单2  Edit
# -----------------------------------------------------------------------
submenu2 = tk.Menu(menu1, tearoff=0)

submenu2.add_command(label="Undo", command=Undo)
submenu2.add_separator()
submenu2.add_command(label="Redo", command=Redo)
submenu2.add_separator()
submenu2.add_command(label="Copy", command=Copy)
submenu2.add_separator()
submenu2.add_command(label="Cut", command=Cut)
submenu2.add_separator()
submenu2.add_command(label="Paste", command=Paste)
submenu2.add_separator()
submenu2.add_command(label="Search", command=Search)

menu1.add_cascade(label="Edit", menu=submenu2)
# ------------------------------------------------------------------------


# 菜单3 Help
# ------------------------------------------------------------------------
submenu3 = tk.Menu(menu1, tearoff=0)

submenu3.add_command(label="About", command=About)

menu1.add_cascade(label="Help", menu=submenu3)
# ------------------------------------------------------------------------

# ----------------------------------------------------------------------------------------------------------


# ------------------------------------------四、总体结构---------------------------------
# 1.菜单栏
win.config(menu=menu1)

# 2.滑动文本框


def keyPress(event):
    textChanged.set(1)


txtContent = tk.scrolledtext.ScrolledText(win, wrap=tk.WORD)
txtContent.pack(fill=tk.BOTH, expand=tkinter.YES)  # 创建

txtContent.bind("<Key>", keyPress)  # 绑定按键识别器

win.mainloop()

用Python制作编辑器

标签:返回   计数   fir   粘贴   glob   cad   cut   缓冲   解码   

原文地址:https://www.cnblogs.com/ezhar/p/13907473.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有
迷上了代码!