-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
131 lines (111 loc) · 2.99 KB
/
main.py
File metadata and controls
131 lines (111 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Yusuf Hafidz
# 120140234
# RB
import tkinter as tk
from tkinter import filedialog
# TK ROOT
root = tk.Tk()
# ===== #
# App
# ===== #
# Head
CONST_app_name = "Text Editor"
root.title(CONST_app_name)
# Root Window Size
root.geometry("900x800")
root.minsize(0,800) # Tinggi minimal window 800px (sesuai spesifikasi tugas)
# Root Grid Configuration
root.rowconfigure(0, weight=1,minsize=100)
root.columnconfigure(0, weight=0, minsize=100)
root.columnconfigure(1, weight=1, minsize=100)
# Root Frames
frm_side_menu = tk.Frame(root,
background="#4f4f4f"
)
frm_side_menu.grid(row=0, column=0, sticky="ns")
frm_editor= tk.Frame(root,
background="#3f3f3f"
)
frm_editor.grid(row=0,column=1, sticky="nswe")
# ===== #
# Side Menu
# ===== #
# Buttons
btn_open = tk.Button(frm_side_menu,
text="Open",
height=1,
width=10
)
btn_open.grid(row=0, column=0, padx=10,pady=(10,5))
btn_save = tk.Button(frm_side_menu,
text="Save",
height=1,
width=10
)
btn_save.grid(row=1, column=0, padx=10,pady=5)
# ===== #
# Main Editor
# ===== #
# Textbox
txt_main_editor = tk.Text(frm_editor,
background="#3f3f3f",
# background="red", # For debugging purpose
foreground="white",
borderwidth=0,
insertbackground="white"
)
txt_main_editor.grid(column=0, row=0, padx=10, pady=10, sticky="nswe")
# Frame Editor Grid Config
frm_editor.rowconfigure(0, weight=1)
frm_editor.columnconfigure(0, weight=1, minsize=800,)
# ===== #
# Logics/Commands
# ===== #
# Global Var
f_opened = False # File open status
f_name = None # Opened file name
# Callback Function
# Open File
def open_file():
global f_opened
global f_name
# Open file dialog
fd = filedialog.askopenfilename(
filetypes=[("Text Files", "*.txt"), ("All Files","*.*")],
title="Open Text File..."
)
if fd:
# If a file is selected
with open(fd, "r") as f:
# Open file, put file data in editor, rename window title
txt_main_editor.delete("1.0", tk.END)
txt_main_editor.insert("1.0", f.read())
root.title(f"{CONST_app_name} - {f.name}")
f_name = f.name
f_opened = True
# Save file
def save_file() -> int:
global f_opened
global f_name
if f_opened:
# Save opened file
with open(f_name, "w") as fw:
fw.write(txt_main_editor.get("1.0", tk.END)[:-1])
else:
# If no file is opened, do save as..
fd = filedialog.asksaveasfile(
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")],
title="Save as...",
defaultextension="*.*"
)
if fd:
with open(fd.name, "w") as fw:
fw.write(txt_main_editor.get("1.0", tk.END)[:-1]) # [:-1] avoid 'newline' from tk.END
root.title(f"{CONST_app_name} - {fw.name}")
f_opened = True
f_name = fw.name
# Buttons Command
btn_open["command"] = open_file
btn_save["command"] = save_file
# MAINLOOP
root.mainloop()