-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIDE.py
More file actions
152 lines (135 loc) · 6.45 KB
/
IDE.py
File metadata and controls
152 lines (135 loc) · 6.45 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
import subprocess
import os
class TextEditor:
def __init__(self, root):
self.root = root
self.root.title("simplified python IDE")
self.root.geometry("1000x600+200+150")
self.filename = None
self.title = StringVar()
self.status = StringVar()
self.titlebar = Label(self.root, textvariable=self.title, font=("times new roman", 15, "bold"), bd=2, relief=GROOVE)
self.titlebar.pack(side=TOP,fill=BOTH)
self.settitle()
self.statusbar = Button(self.root, textvariable=self.status, font=("times new roman", 15, "bold"), bd=2, relief=GROOVE)
self.statusbar.pack(side=BOTTOM, fill=BOTH)
self.statusbar.bind("<Button-1>", self.run_code)
self.status.set("Run Code")
self.menubar = Menu(self.root, font=("times new roman", 15, "bold"), activebackground="skyblue")
self.root.config(menu=self.menubar)
self.filemenu = Menu(self.menubar, font=("times new roman", 12), activebackground="skyblue", tearoff=0)
self.filemenu.add_command(label="New", accelerator="Ctrl+N", command=self.newfile)
self.filemenu.add_command(label="Open", accelerator="Ctrl+O", command=self.openfile)
self.filemenu.add_command(label="Save", accelerator="Ctrl+S", command=self.savefile)
self.filemenu.add_command(label="Save As", accelerator="Ctrl+A", command=self.saveasfile)
self.filemenu.add_separator()
self.filemenu.add_command(label="Exit", accelerator="Ctrl+E", command=self.exit)
self.menubar.add_cascade(label="File", menu=self.filemenu)
self.editmenu = Menu(self.menubar, font=("times new roman", 12), activebackground="skyblue", tearoff=0)
self.editmenu.add_command(label="Cut", accelerator="Ctrl+X", command=self.cut)
self.editmenu.add_command(label="Copy", accelerator="Ctrl+C", command=self.copy)
self.editmenu.add_command(label="Paste", accelerator="Ctrl+V", command=self.paste)
self.menubar.add_cascade(label="Edit", menu=self.editmenu)
self.viewmenu = Menu(self.menubar, font=("times new roman", 12), activebackground="skyblue", tearoff=0)
self.viewmenu.add_command(label="Zoom in", accelerator="Ctrl+\'+\'", command=self.zoom_in)
self.viewmenu.add_command(label="Zoom out", accelerator="Ctrl+\'-\'", command=self.zoom_out)
self.menubar.add_cascade(label="View", menu=self.viewmenu)
scrol_y = Scrollbar(self.root, orient=VERTICAL)
self.txtarea = Text(self.root, yscrollcommand=scrol_y.set, font=("times new roman", 11), state="normal", relief=GROOVE)
scrol_y.pack(side=RIGHT, fill=Y)
scrol_y.config(command=self.txtarea.yview)
self.txtarea.pack(fill=BOTH, expand=1)
self.shortcuts()
def settitle(self):
if self.filename:
self.title.set(self.filename)
else:
self.title.set("Untitled")
def newfile(self, *args):
self.txtarea.delete("1.0", END)
self.filename = None
self.settitle()
def openfile(self, *args):
try:
self.filename = filedialog.askopenfilename(title="Select file", filetypes=(("Simplified Python Files", "*.spy"), ("Text Files", "*.txt")))
if self.filename:
infile = open(self.filename, "r")
self.txtarea.delete("1.0", END)
for line in infile:
self.txtarea.insert(END, line)
infile.close()
self.settitle()
except Exception as e:
pass
def savefile(self,*args):
try:
if self.filename:
data = self.txtarea.get("1.0", END)
outfile = open(self.filename, "w")
outfile.write(data)
outfile.close()
self.settitle()
else:
self.saveasfile()
except Exception as e:
pass
def saveasfile(self, *args):
try:
untitledfile = filedialog.asksaveasfilename(title="Save file As", defaultextension=".spy", initialfile="Untitled.spy", filetypes=(("Simplified Python Files", "*.spy"), ("Text Files", "*.txt")))
data = self.txtarea.get("1.0", END)
outfile = open(untitledfile, "w")
outfile.write(data)
outfile.close()
self.filename = untitledfile
self.settitle()
except Exception as e:
pass
def exit(self, *args):
op = messagebox.askyesno("WARNING", "Your Unsaved Data May be Lost! Are you Sure?")
if op > 0:
self.root.destroy()
else:
return
def cut(self, *args):
self.txtarea.event_generate("<Cut>")
def copy(self, *args):
self.txtarea.event_generate("<Copy>")
def paste(self, *args):
self.txtarea.event_generate("<Paste>")
def zoom_in(self, *args):
font = self.txtarea.cget('font').split("} ")
font[0] = font[0][1:]
new_font = (font[0], int(font[1]) + 1)
self.txtarea.configure(font=new_font)
def zoom_out(self, *args):
font = self.txtarea.cget('font').split("} ")
font[0] = font[0][1:]
new_font = (font[0], int(font[1]) - 1)
self.txtarea.configure(font=new_font)
def shortcuts(self):
self.txtarea.bind("<Control-n>", self.newfile)
self.txtarea.bind("<Control-o>", self.openfile)
self.txtarea.bind("<Control-s>", self.savefile)
self.txtarea.bind("<Control-a>", self.saveasfile)
self.txtarea.bind("<Control-e>", self.exit)
self.txtarea.bind("<Control-x>", self.cut)
self.txtarea.bind("<Control-c>", self.copy)
self.txtarea.bind("<Control-v>", self.paste)
self.txtarea.bind("<Control-equal>", self.zoom_in)
self.txtarea.bind("<Control-minus>", self.zoom_out)
def run_code(self, *args):
if self.filename:
code = open(self.filename, 'r').read()
else:
code = self.txtarea.get("1.0",END)
script_file = open("execute.sh", 'w')
script_file.write(f"!#/bin/bash\nracket execute.rkt -a \"{code}\"\nread -p \"Press Enter to continue...\"")
script_file.close()
subprocess.call("execute.sh", shell=True)
os.remove("execute.sh")
root = Tk()
TextEditor(root)
root.mainloop()