Advertisement
here2share

My_IDE.py

Jun 13th, 2021
1,033
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. # My_IDE.py
  2.  
  3. from tkinter import *
  4.  
  5. from tkinter.filedialog import asksaveasfilename, askopenfilename
  6.  
  7. import subprocess
  8.  
  9. compiler = Tk()
  10.  
  11. compiler.title('My IDE')
  12.  
  13. file_path = ''
  14.  
  15. def set_file_path(path):
  16.  
  17.     global file_path
  18.  
  19.     file_path = path
  20.  
  21. def open_file():
  22.  
  23.     path = askopenfilename(filetypes=[('Python Files', '*.py')])
  24.  
  25.     with open(path, 'r') as file:
  26.  
  27.         code = file.read()
  28.  
  29.         editor.delete('1.0', END)
  30.  
  31.         editor.insert('1.0', code)
  32.  
  33.         set_file_path(path)
  34.  
  35. def save_as():
  36.  
  37.     if file_path == '':
  38.  
  39.         path = asksaveasfilename(filetypes=[('Python Files', '*.py')])
  40.  
  41.     else:
  42.  
  43.         path = file_path
  44.  
  45.     with open(path, 'w') as file:
  46.  
  47.         code = editor.get('1.0', END)
  48.  
  49.         file.write(code)
  50.  
  51.         set_file_path(path)
  52.  
  53. def run():
  54.  
  55.     if file_path == '':
  56.  
  57.         save_prompt = Toplevel()
  58.  
  59.         text = Label(save_prompt, text='Please save your code')
  60.  
  61.         text.pack()
  62.  
  63.         return
  64.  
  65.     command = f'python {file_path}'
  66.  
  67.     process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  68.  
  69.     output, error = process.communicate()
  70.  
  71.     code_output.insert('1.0', output)
  72.  
  73.     code_output.insert('1.0', error)
  74.  
  75. menu_bar = Menu(compiler)
  76.  
  77. file_menu = Menu(menu_bar, tearoff=0)
  78.  
  79. file_menu.add_command(label='Open', command=open_file)
  80.  
  81. file_menu.add_command(label='Save', command=save_as)
  82.  
  83. file_menu.add_command(label='Save As', command=save_as)
  84.  
  85. file_menu.add_command(label='Exit', command=exit)
  86.  
  87. menu_bar.add_cascade(label='File', menu=file_menu)
  88.  
  89. run_bar = Menu(menu_bar, tearoff=0)
  90.  
  91. run_bar.add_command(label='Run', command=run)
  92.  
  93. menu_bar.add_cascade(label='Run', menu=run_bar)
  94.  
  95. compiler.config(menu=menu_bar)
  96.  
  97. scr_width = 160
  98.  
  99. editor = Text(width=scr_width)
  100.  
  101. editor.pack()
  102.  
  103. code_output = Text(height=16,width=scr_width)
  104.  
  105. code_output.pack()
  106.  
  107. compiler.mainloop()
  108.  
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement