Advertisement
Najeebsk

PYTHON-IDE.pyw

Oct 16th, 2024
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter.filedialog import asksaveasfilename, askopenfilename
  3. import subprocess
  4.  
  5. compiler = Tk()
  6. compiler.title('Najeeb Python IDE')
  7. compiler.geometry('1000x600')  # Adjust window size to 1000x700
  8.  
  9. file_path = ''
  10.  
  11. def set_file_path(path):
  12.     global file_path
  13.     file_path = path
  14.  
  15. def open_file():
  16.     path = askopenfilename(filetypes=[('Python Files', '*.py'), ("All Files", "*.*")])
  17.     if path:
  18.         with open(path, 'r') as file:
  19.             code = file.read()
  20.             editor.delete('1.0', END)
  21.             editor.insert('1.0', code)
  22.             set_file_path(path)
  23.  
  24. def save_as():
  25.     if file_path == '':
  26.         path = asksaveasfilename(filetypes=[('Python Files', '*.py'), ("All Files", "*.*")])
  27.     else:
  28.         path = file_path
  29.     if path:
  30.         with open(path, 'w') as file:
  31.             code = editor.get('1.0', END)
  32.             file.write(code)
  33.             set_file_path(path)
  34.  
  35. def run():
  36.     if file_path == '':
  37.         save_prompt = Toplevel()
  38.         text = Label(save_prompt, text='Please save your code')
  39.         text.pack()
  40.         return
  41.     command = f'python {file_path}'
  42.     process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  43.     output, error = process.communicate()
  44.     code_output.delete('1.0', END)  # Clear previous output
  45.     code_output.insert('1.0', output)
  46.     code_output.insert('1.0', error)
  47.  
  48. # Set up menu bar
  49. menu_bar = Menu(compiler)
  50. file_menu = Menu(menu_bar, tearoff=0)
  51. file_menu.add_command(label='Open', command=open_file)
  52. file_menu.add_command(label='Save', command=save_as)
  53. file_menu.add_command(label='Save As', command=save_as)
  54. file_menu.add_command(label='Exit', command=exit)
  55. menu_bar.add_cascade(label='File', menu=file_menu)
  56.  
  57. run_bar = Menu(menu_bar, tearoff=0)
  58. run_bar.add_command(label='Run', command=run)
  59. menu_bar.add_cascade(label='Run', menu=run_bar)
  60.  
  61. compiler.config(menu=menu_bar)
  62.  
  63. # Create editor field with a scrollbar
  64. editor_frame = Frame(compiler)
  65. editor_frame.pack(fill=BOTH, expand=True)
  66. editor_scrollbar = Scrollbar(editor_frame)
  67. editor_scrollbar.pack(side=RIGHT, fill=Y)
  68. editor = Text(editor_frame, font=('Times New Roman', 16), height=20, yscrollcommand=editor_scrollbar.set)
  69. editor.pack(fill=BOTH, expand=True)
  70. editor_scrollbar.config(command=editor.yview)
  71.  
  72. # Create output field with a scrollbar
  73. output_frame = Frame(compiler)
  74. output_frame.pack(fill=BOTH, expand=True)
  75. output_scrollbar = Scrollbar(output_frame)
  76. output_scrollbar.pack(side=RIGHT, fill=Y)
  77. code_output = Text(output_frame, bg="black", fg="white", font=('Times New Roman', 12), height=10, yscrollcommand=output_scrollbar.set)
  78. code_output.pack(fill=BOTH, expand=True)
  79. output_scrollbar.config(command=code_output.yview)
  80.  
  81. compiler.mainloop()
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement