simpletextedit.py
· 1.4 KiB · Python
Raw
import tkinter as tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
def open_file():
"""Open a file for editing."""
filepath = askopenfilename(
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
)
if not filepath:
return
txt_edit.delete(1.0, tk.END)
with open(filepath, "r") as input_file:
text = input_file.read()
txt_edit.insert(tk.END, text)
window.title(f"Zipity - {filepath}")
def save_file():
"""Save the current file as a new file."""
filepath = asksaveasfilename(
defaultextension="txt",
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")],
)
if not filepath:
return
with open(filepath, "w") as output_file:
text = txt_edit.get(1.0, tk.END)
output_file.write(text)
window.title(f"Zipity - {filepath}")
window = tk.Tk()
window.title("Zipity")
window.rowconfigure(0, minsize=800, weight=1)
window.columnconfigure(1, minsize=800, weight=1)
txt_edit = tk.Text(window)
fr_buttons = tk.Frame(window, relief=tk.RAISED, bd=2)
btn_open = tk.Button(fr_buttons, text="Open", command=open_file)
btn_save = tk.Button(fr_buttons, text="Save As...", command=save_file)
btn_open.grid(row=0, column=0, sticky="ew", padx=5, pady=5)
btn_save.grid(row=1, column=0, sticky="ew", padx=5)
fr_buttons.grid(row=0, column=0, sticky="ns")
txt_edit.grid(row=0, column=1, sticky="nsew")
window.mainloop()
1 | import tkinter as tk |
2 | from tkinter.filedialog import askopenfilename, asksaveasfilename |
3 | |
4 | def open_file(): |
5 | """Open a file for editing.""" |
6 | filepath = askopenfilename( |
7 | filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")] |
8 | ) |
9 | if not filepath: |
10 | return |
11 | txt_edit.delete(1.0, tk.END) |
12 | with open(filepath, "r") as input_file: |
13 | text = input_file.read() |
14 | txt_edit.insert(tk.END, text) |
15 | window.title(f"Zipity - {filepath}") |
16 | |
17 | def save_file(): |
18 | """Save the current file as a new file.""" |
19 | filepath = asksaveasfilename( |
20 | defaultextension="txt", |
21 | filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")], |
22 | ) |
23 | if not filepath: |
24 | return |
25 | with open(filepath, "w") as output_file: |
26 | text = txt_edit.get(1.0, tk.END) |
27 | output_file.write(text) |
28 | window.title(f"Zipity - {filepath}") |
29 | |
30 | window = tk.Tk() |
31 | window.title("Zipity") |
32 | window.rowconfigure(0, minsize=800, weight=1) |
33 | window.columnconfigure(1, minsize=800, weight=1) |
34 | |
35 | txt_edit = tk.Text(window) |
36 | fr_buttons = tk.Frame(window, relief=tk.RAISED, bd=2) |
37 | btn_open = tk.Button(fr_buttons, text="Open", command=open_file) |
38 | btn_save = tk.Button(fr_buttons, text="Save As...", command=save_file) |
39 | |
40 | btn_open.grid(row=0, column=0, sticky="ew", padx=5, pady=5) |
41 | btn_save.grid(row=1, column=0, sticky="ew", padx=5) |
42 | |
43 | fr_buttons.grid(row=0, column=0, sticky="ns") |
44 | txt_edit.grid(row=0, column=1, sticky="nsew") |
45 | |
46 | window.mainloop() |
47 |