diff options
| author | Bobby <[email protected]> | 2022-11-06 01:15:27 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-11-06 01:15:27 -0400 |
| commit | ffd0ec06c9176c38962b1841ef2d1c3d5a806922 (patch) | |
| tree | 2001caaa2b2ba9741c1ea0e3f6dba92ea436ae35 /src | |
| parent | 8f8d5b2fa7264e9c614a048fd55dbd9925c1aeed (diff) | |
| download | texty-ffd0ec06c9176c38962b1841ef2d1c3d5a806922.tar.xz texty-ffd0ec06c9176c38962b1841ef2d1c3d5a806922.zip | |
feat: basic text editor and preferences
Diffstat (limited to 'src')
| -rw-r--r-- | src/texty.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/texty.py b/src/texty.py index e9be7d1..4f7f460 100644 --- a/src/texty.py +++ b/src/texty.py @@ -2,4 +2,67 @@ # text editor focused on simplicity and ease of use while being powerful and # extensible. It is a work in progress and is not yet ready for use. +import json +import os import tkinter as tk + +PREFERENCES_FILE = "texty.prefs" +DEFAULT_PREFS = { + "width": 800, + "height": 600, + "x_pos": 0, + "y_pos": 0, +} + + +class Texty(tk.Tk): + def __init__(self): + super().__init__() + self.title("Texty") + self.load_prefs() + self.geometry("{}x{}".format(self.prefs["width"], self.prefs["height"])) + self.minsize(400, 300) + + # set position + if self.prefs["x_pos"] > 0 and self.prefs["y_pos"] > 0: + self.geometry("+{}+{}".format(self.prefs["x_pos"], self.prefs["y_pos"])) + else: + self.geometry( + "+{}+{}".format( + self.winfo_screenwidth() // 2 - self.prefs["width"] // 2, + self.winfo_screenheight() // 2 - self.prefs["height"] // 2, + ) + ) + + self.draw_window() + + def load_prefs(self): + self.prefs = DEFAULT_PREFS + if os.path.exists(PREFERENCES_FILE): + with open(PREFERENCES_FILE) as f: + self.prefs = json.load(f) + else: + with open(PREFERENCES_FILE, "w") as f: + json.dump(DEFAULT_PREFS, f) + + def save_prefs(self): + with open(PREFERENCES_FILE, "w") as f: + json.dump(self.prefs, f) + + def draw_window(self): + self.text = tk.Text(self) + self.text.pack(fill="both", expand=True) + + def on_close(self): + self.prefs["width"] = self.winfo_width() + self.prefs["height"] = self.winfo_height() + self.prefs["x_pos"] = self.winfo_x() + self.prefs["y_pos"] = self.winfo_y() + self.save_prefs() + self.destroy() + + +if __name__ == "__main__": + app = Texty() + app.protocol("WM_DELETE_WINDOW", app.on_close) + app.mainloop() |
