tkinter蓝光记事本.py

如本文章标有价格,需议价或其它事情商议请加微信pythonxia

李兴球python可视化tkinter记事本开发
"""
   蓝光记事本.py
   一个简单的记事本,还没有添加右键菜单。
"""
import tkinter as tk
from tkinter import messagebox
from tkinter.filedialog import askopenfilename, asksaveasfilename

def open_file():
    """打开文件进行编辑"""
    filepath = askopenfilename( filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
    if not filepath: return                    # 如果此文件不存在,直接返回
    txt_editor.delete(1.0, tk.END)             # 删除文本框所有内容
    with open(filepath, "r") as input_file:
        text = input_file.read()
        txt_editor.insert(tk.END, text)
    window.title(f"{filepath} 蓝光记事本")

def save_file():
    """保存文件"""
    filepath = asksaveasfilename(defaultextension="txt",
        filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")], )
    if not filepath: return
    with open(filepath, "w") as output_file:
        text = txt_editor.get(1.0, tk.END)
        output_file.write(text)
    window.title(f"{filepath} 蓝光记事本")

window = tk.Tk()
window.title("蓝光记事本 www.lixingqiu.com")
window.rowconfigure(0, minsize=800, weight=1)
window.columnconfigure(1, minsize=800, weight=1)

txt_editor = tk.Text(window,bg='#433fff',fg='white')                # 多行文本框
button_frame = tk.Frame(window, relief=tk.RAISED, bd=2)             # 按钮所在的框架 
button_open = tk.Button(button_frame, text="打 开", command=open_file)
button_save = tk.Button(button_frame, text="保 存...", command=save_file)
button_about = tk.Button(button_frame, text="关 于...",
                         command=lambda:messagebox.showinfo('版权所有','程序设计:李兴球'))

button_open.grid(row=0, column=0, sticky="ew", padx=5, pady=5)
button_save.grid(row=1, column=0, sticky="ew", padx=5,pady=5)
button_about.grid(row=2, column=0, sticky="ew", padx=5,pady=5)

button_frame.grid(row=0, column=0, sticky="ns")
txt_editor.grid(row=0, column=1, sticky="nsew")

window.mainloop()

"""
边框样式
relief=tk.SUNKEN(默认)
relief=tk.RAISED
relief=tk.GROOVE
relief=tk.RIDGE
relief=tk.FLAT
"""

本站所有作品,教程等皆为原创,版权所有。只供个人及单位内部研究使用,对外展示或传播必需经本站同意,且注明来自本站。培训机构等用本站资源培训学生,需经本站授权。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。入住QQ群:225792826 和爱好者共同交流,并且能下载免费提供的Python资源(需提供真实姓名才可入群)
李兴球的博客_Python创意编程技术前沿_pygame » tkinter蓝光记事本.py
scratch

学本领,探索更大的世界!

李兴球博客 风火轮编程主页