Advertisement
rudy1234567890

ide.py

Jul 16th, 2023 (edited)
1,480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.77 KB | Source Code | 0 0
  1. import tkinter as tk
  2. from tkinter.filedialog import (
  3.     asksaveasfilename,
  4.     askopenfilename
  5. )
  6. from tkinter.font import Font
  7. from tkinter.messagebox import askyesno
  8. import subprocess
  9.  
  10. buildin_keywords = (
  11.     "import", "print", "list", "dict", "for", "in",
  12.     "or", "and", "not", "tuple", "set"
  13. )
  14. tab_size = 0
  15. global_filepath = ''
  16. shortcuts_map = {
  17.     '<Control-s>': lambda event: save_as(),
  18.     '<Control-o>': lambda event: open_file(),
  19.     '<Control-n>': lambda event: create_newfile(),
  20.     '<Control-q>': lambda event: compiler.destroy(),
  21.     '<Control-b>': lambda event: run(),
  22.     '<Control-a>': lambda event: select_all()
  23. }
  24.  
  25. def open_outputscr():
  26.     output_scr = tk.Toplevel(compiler)
  27.     output_scr.title("OUTPUT")
  28.     output_scr.minsize(800, 500)
  29.  
  30. def run():
  31.     global global_filepath
  32.     output_window = tk.Toplevel(compiler)
  33.     vsbar1 = tk.Scrollbar(output_window, orient="vertical")
  34.     code_output = tk.Text(output_window, height=10, yscrollcommand=vsbar1.set, fg="#000000", bg="gray")
  35.     vsbar1.pack(side=tk.RIGHT, fill='y')
  36.     vsbar1.config(command=code_output.yview)
  37.     code_output.configure(state=tk.DISABLED)
  38.     code_output.pack()  
  39.     code_output.configure(state=tk.NORMAL)
  40.     code_output.delete('1.0', tk.END)
  41.     if global_filepath == '':
  42.         ans = askyesno(title="File Not Saved", message="Do you want to save file?")
  43.         if ans:
  44.             save_as()
  45.         else:
  46.             return
  47.     save_as()
  48.     command = f'python {global_filepath}'      
  49.     process = subprocess.Popen(
  50.         command,
  51.         stdout=subprocess.PIPE,
  52.         stderr=subprocess.PIPE,
  53.         shell=True
  54.     )
  55.     output, error = process.communicate()
  56.     code_output.insert('1.0', output)
  57.     code_output.insert('1.0', error)
  58.     code_output.configure(state=tk.DISABLED)
  59.     output_window.bind('<Control-q>', lambda event: output_window.destroy())
  60.  
  61. def save_as():
  62.     global global_filepath
  63.     if global_filepath == '':
  64.         file_path = asksaveasfilename(
  65.         filetypes= [('Python Files', '*.py')],
  66.         initialdir='/storage/emulated/0' ,
  67.     )
  68.     else:
  69.         file_path = global_filepath
  70.     if file_path:
  71.         with open(file_path, 'w') as file:
  72.             file.write(editor.get('1.0', tk.END))
  73.     global_filepath = file_path
  74.            
  75.                        
  76. def open_file():
  77.     global global_filepath
  78.     file_path = askopenfilename(
  79.         filetypes= [('Python Files', '*.py')],
  80.         initialdir='/storage/emulated/0' ,
  81.     )
  82.    
  83.     if file_path:
  84.         with open(file_path, 'r') as file:
  85.             editor.delete('1.0', tk.END)
  86.             editor.insert('1.0', file.read())
  87.     global_filepath = file_path
  88.            
  89. def create_newfile():
  90.     compiler.withdraw()
  91.     compiler.deiconify()
  92.     editor.delete('1.0', tk.END)
  93.  
  94. def select_all():
  95.     editor.tag_add(tk.SEL, "1.0", tk.END)
  96.     editor.mark_set(tk.INSERT, "1.0")
  97.     editor.see(tk.INSERT)
  98.     return 'break'
  99.  
  100. def get_cursorpos():
  101.     return editor.index(tk.INSERT).split('.')
  102.  
  103. #autocompletes
  104. def symbol_autocomplete(e):
  105.     if str(e.widget) != '.!text':
  106.         return
  107.     brackets = {'(':')', '{':'}', '[':']', '\'':'\'', '\"':'\"'}    
  108.     if e.char in brackets:
  109.         editor.insert(editor.index(tk.INSERT), brackets[e.char])
  110.         linecol_sep = editor.index(tk.INSERT)
  111.         sep_idx = linecol_sep.find('.')
  112.         editor.mark_set(
  113.             tk.INSERT,
  114.             f"{linecol_sep[0:int(sep_idx)]}.{int(linecol_sep[int(sep_idx)+1:])-1}")
  115.  
  116.  
  117. compiler = tk.Tk()
  118. compiler.title('RUDY COMPILER')                        
  119. compiler.bind('<Key>', symbol_autocomplete)
  120.  
  121. #shortcuts binding
  122. for key, action in shortcuts_map.items():
  123.     compiler.bind(key, action)
  124.  
  125. #scrollbar in editor
  126. vsbar = tk.Scrollbar(compiler, orient="vertical")
  127. #editor
  128. editor = tk.Text(compiler, yscrollcommand=vsbar.set, height=15, bg="#000000", fg="lightgreen")
  129. vsbar.pack(side=tk.RIGHT, fill='y')
  130. vsbar.config(command=editor.yview)
  131. editor.focus_set()
  132. editor.config(insertbackground="#FFF", tabs=Font().measure('    '))
  133. editor.pack(expand=True, fill="both")
  134.  
  135. #main menu
  136. menu_bar = tk.Menu(compiler, background="#1B1212", foreground="#fff")
  137. compiler.config(menu=menu_bar, bg="#000")
  138.  
  139. #file menu
  140. file_menu = tk.Menu(menu_bar)
  141. file_menu.add_command(label = 'Open', command = open_file)
  142. file_menu.add_command(label="New", command=create_newfile)
  143. file_menu.add_command(label='Save', command=save_as)
  144. file_menu.add_command(label='Save as', command=save_as)
  145. file_menu.add_command(label='Exit',command=lambda: compiler.destroy())
  146.  
  147. #run menu
  148. run_menu = tk.Menu(menu_bar)
  149. run_menu.add_command(label="Run", command=run)
  150.  
  151. #cascade
  152. menu_bar.add_cascade(label = 'File', menu=file_menu)
  153. menu_bar.add_cascade(label="Run", menu=run_menu)
  154.  
  155. compiler.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement