-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
executable file
·139 lines (111 loc) · 3.93 KB
/
gui.py
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
132
133
134
135
136
137
138
139
#!/usr/bin/env python
# ==============================================
#
# gui.py
#
# Graphical User Interface GUI
#
# ==============================================
import customtkinter
from customtkinter import filedialog
from lexical import lex_data, lex_file
from semantic import yacc_data, yacc_file
from os import getcwd
customtkinter.set_appearance_mode("System")
customtkinter.set_default_color_theme("dark-blue")
root = customtkinter.CTk()
root.geometry("1280x720")
global current_file, current_text
current_file, current_text = "", ""
def open_new_file():
global current_file, current_text
with open(f"{getcwd()}/new_file.txt", mode="w+", encoding="utf8") as new_file:
root.title("new_file.txt")
current_file = new_file
current_text = code.delete("0.0", "end")
def open_file():
global current_file, current_text
text_file = filedialog.askopenfilename(
initialdir=f"{getcwd()}/",
title="Open Text File",
filetypes=(("Text Files", "*.txt"),),)
code.delete("0.0", "end") # Delete previous text
with open(text_file, mode="r+", encoding="utf8") as text_file:
current_file = text_file
root.title(f"{current_file.name}")
current_text = current_file.read()
code.insert("0.0", current_text)
def save_file():
global current_file, current_text
if current_file:
with open(current_file.name, mode="w+", encoding="utf8") as saved_file:
current_file = saved_file
current_text = code.get("0.0", "end")
current_file.write(current_text)
else:
save_as_file()
def save_as_file():
global current_file, current_text
text_file = filedialog.askopenfilename(
initialdir=f"{getcwd()}/",
title="Open Text File",
filetypes=(("Text Files", "*.txt"),),
)
with open(text_file, mode="w", encoding="utf8") as saved_file:
current_file = saved_file
root.title(f"{current_file.name}")
current_text = code.get("0.0", "end")
current_file.write(current_text)
def lexical_analysis():
global current_file, current_text
if current_file:
save_file()
result.configure(state="normal")
result.delete("0.0", "end")
result.insert(
"0.0", f"{lex_file(current_file.name)}"
)
result.update()
result.configure(state="disable")
else:
save_as_file()
def semantic_analysis():
global current_file, current_text
if current_file:
save_file()
result.configure(state="normal")
result.delete("0.0", "end")
result.insert(
"0.0", f"{yacc_file(current_file.name)}"
)
result.update()
result.configure(state="disable")
else:
save_as_file()
frame = customtkinter.CTkFrame(master=root)
frame.pack(pady=10, padx=10, fill="both", expand=True)
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1)
frame.grid_rowconfigure(1, weight=1)
open_button = customtkinter.CTkButton(
master=frame, corner_radius=9, command=open_file, text="Open File"
)
open_button.grid(row=2, column=0, padx=10, pady=10)
save_button = customtkinter.CTkButton(
master=frame, corner_radius=9, command=save_file, text="Save File"
)
save_button.grid(row=2, column=1, padx=10, pady=10)
lex_button = customtkinter.CTkButton(
master=frame, corner_radius=9, command=lexical_analysis, text="Lexical Analysis"
)
lex_button.grid(row=0, column=0, padx=10, pady=10)
sem_button = customtkinter.CTkButton(
master=frame, corner_radius=9, command=semantic_analysis, text="Semantic Analysis"
)
sem_button.grid(row=0, column=1, padx=10, pady=10)
code = customtkinter.CTkTextbox(master=frame, font=("monoscape", 16))
code.grid(row=1, column=0, padx=10, pady=10, sticky="nsew")
result = customtkinter.CTkTextbox(master=frame, wrap="word", font=("monoscape", 16))
result.grid(row=1, column=1, padx=10, pady=10, sticky="nsew")
open_new_file()
root.mainloop()