|
| 1 | +# Author:Priyadarshan2000 (Priyadarshan Ghosh) |
| 2 | +# Simple Python IDE using Tkinter |
| 3 | +# See the readme.md for how to Run this Project. |
| 4 | + |
| 5 | +# importing the modules |
| 6 | +from tkinter import * |
| 7 | +from tkinter.filedialog import asksaveasfilename, askopenfilename |
| 8 | +import subprocess |
| 9 | + |
| 10 | +compiler = Tk() |
| 11 | +compiler.title('Simple Python IDE') |
| 12 | +file_path = '' |
| 13 | + |
| 14 | + |
| 15 | +def set_file_path(path): |
| 16 | + global file_path |
| 17 | + file_path = path |
| 18 | + |
| 19 | +def open_file(): |
| 20 | + path = askopenfilename(filetypes=[('Python Files', '*.py')]) |
| 21 | + with open(path, 'r') as file: |
| 22 | + code = file.read() |
| 23 | + editor.delete('1.0', END) |
| 24 | + editor.insert('1.0', code) |
| 25 | + set_file_path(path) |
| 26 | + |
| 27 | + |
| 28 | +def save_as(): |
| 29 | + if file_path == '': |
| 30 | + path = asksaveasfilename(filetypes=[('Python Files', '*.py')]) |
| 31 | + else: |
| 32 | + path = file_path |
| 33 | + with open(path, 'w') as file: |
| 34 | + code = editor.get('1.0', END) |
| 35 | + file.write(code) |
| 36 | + set_file_path(path) |
| 37 | + |
| 38 | + |
| 39 | +def run(): |
| 40 | + if file_path == '': |
| 41 | + save_prompt = Toplevel() |
| 42 | + text = Label(save_prompt, text='Please save your code') |
| 43 | + text.pack() |
| 44 | + return |
| 45 | + command = f'python {file_path}' |
| 46 | + process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 47 | + output, error = process.communicate() |
| 48 | + code_output.insert('1.0', output) |
| 49 | + code_output.insert('1.0', error) |
| 50 | + |
| 51 | + |
| 52 | +menu_bar = Menu(compiler) |
| 53 | + |
| 54 | +file_menu = Menu(menu_bar, tearoff=0) |
| 55 | +file_menu.add_command(label='Open', command=open_file) |
| 56 | +file_menu.add_command(label='Save', command=save_as) |
| 57 | +file_menu.add_command(label='Save As', command=save_as) |
| 58 | +file_menu.add_command(label='Exit', command=exit) |
| 59 | +menu_bar.add_cascade(label='File', menu=file_menu) |
| 60 | + |
| 61 | +run_bar = Menu(menu_bar, tearoff=0) |
| 62 | +run_bar.add_command(label='Run', command=run) |
| 63 | +menu_bar.add_cascade(label='Run', menu=run_bar) |
| 64 | + |
| 65 | +compiler.config(menu=menu_bar) |
| 66 | + |
| 67 | +editor = Text() |
| 68 | +editor.pack() |
| 69 | + |
| 70 | +code_output = Text(height=10) |
| 71 | +code_output.pack() |
| 72 | + |
| 73 | +# Starting the GUI |
| 74 | +compiler.mainloop() |
0 commit comments