本帖最后由 happy_day 于 2024-4-11 17:34 编辑
最近工作原因接触python比较多,开始重拾编程语言了,
今天工作之余顺便练手写了一个简易记事本,
真的非常非常非常简陋,只能自适应拖拽,打开显示文本,保存编辑文本,大佬们轻喷,
不过作为初学者,熟悉python函数的用法和tk库的用法,培养一下编程逻辑,还是不错的,虽然自己也不算初学者
项目没打包成exe,有兴趣的可以自己再完善完善,打包
附截图:
附代码:
[Python] 纯文本查看 复制代码 import tkinter as tk
from tkinter import filedialog
def open_file():
file = filedialog.askopenfilename(defaultextension=".txt",
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if file:
text.delete(1.0, tk.END)
with open(file, "r") as file:
text.insert(tk.INSERT, file.read())
def save_open():
file = filedialog.asksaveasfilename(defaultextension=".txt",
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
print(file)
if file:
with open(file, "w") as file:
file.write(text.get(1.0, tk.END))
root = tk.Tk()
root.title("简易记事本")
text = tk.Text(root)
text.grid(row=0, column=0, sticky="nsew")
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
menu = tk.Menu(root)
root.config(menu=menu)
menu.add_cascade(label="打开",command=open_file)
menu.add_cascade(label="保存",command=save_open)
# open_file()
# save_open
root.mainloop()
|