微信小游戏开发的市场前景与创新策略探讨
1085
2022-10-27
用Python制作编辑器
import tkinterimport tkinter as tkfrom tkinter import *import tkinter.filedialogimport tkinter.messageboximport tkinter.scrolledtextimport tkinter.colorchooserimport tkinter.simpledialog# ----------------------------------------一、创建窗口-------------------------------------------------filename = "" # 定义空文件名win = tk.Tk()win["height"] = 400win["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("
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~