Advertisement
Najeebsk

PYTHON-IDE2.0.pyw

Oct 16th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.43 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import filedialog, messagebox
  3. from tkinter.scrolledtext import ScrolledText
  4. import subprocess
  5. import os
  6. import keyword
  7. import re
  8.  
  9. class PythonIDE:
  10.     def __init__(self, root):
  11.         self.root = root
  12.         self.root.title("Najeeb Python IDE")
  13.         self.root.geometry("1000x600")
  14.         self.filename = None
  15.  
  16.         # Create the Menu Bar
  17.         self.create_menu()
  18.  
  19.         # Create the text editor
  20.         self.editor = ScrolledText(self.root, undo=True, wrap='word', font=("Times New Roman", 14))
  21.         self.editor.pack(fill='both', expand=True)
  22.         self.editor.bind("<KeyRelease>", self.syntax_highlight)
  23.  
  24.         # Output console
  25.         self.console = ScrolledText(self.root, height=10, bg="black", fg="white", font=("Consolas", 12))
  26.         self.console.pack(fill='x')
  27.  
  28.         # Create status bar
  29.         self.status = tk.Label(self.root, text="Welcome to Python IDE", anchor='w')
  30.         self.status.pack(side='bottom', fill='x')
  31.  
  32.         # Define syntax highlight colors
  33.         self.editor.tag_configure("keyword", foreground="blue")
  34.         self.editor.tag_configure("string", foreground="green")
  35.         self.editor.tag_configure("comment", foreground="gray")
  36.         self.editor.tag_configure("number", foreground="purple")
  37.    
  38.     def create_menu(self):
  39.         menubar = tk.Menu(self.root)
  40.         self.root.config(menu=menubar)
  41.  
  42.         # File menu
  43.         file_menu = tk.Menu(menubar, tearoff=0)
  44.         menubar.add_cascade(label="File", menu=file_menu)
  45.         file_menu.add_command(label="New", command=self.new_file)
  46.         file_menu.add_command(label="Open", command=self.open_file)
  47.         file_menu.add_command(label="Save", command=self.save_file)
  48.         file_menu.add_command(label="Save As", command=self.save_as_file)
  49.         file_menu.add_separator()
  50.         file_menu.add_command(label="Exit", command=self.root.quit)
  51.  
  52.         # Run menu
  53.         run_menu = tk.Menu(menubar, tearoff=0)
  54.         menubar.add_cascade(label="Run", menu=run_menu)
  55.         run_menu.add_command(label="Run", command=self.run_code)
  56.  
  57.         # Edit menu
  58.         edit_menu = tk.Menu(menubar, tearoff=0)
  59.         menubar.add_cascade(label="Edit", menu=edit_menu)
  60.         edit_menu.add_command(label="Undo", command=lambda: self.editor.edit_undo())
  61.         edit_menu.add_command(label="Redo", command=lambda: self.editor.edit_redo())
  62.         edit_menu.add_separator()
  63.         edit_menu.add_command(label="Cut", command=lambda: self.editor.event_generate("<<Cut>>"))
  64.         edit_menu.add_command(label="Copy", command=lambda: self.editor.event_generate("<<Copy>>"))
  65.         edit_menu.add_command(label="Paste", command=lambda: self.editor.event_generate("<<Paste>>"))
  66.         edit_menu.add_command(label="Select All", command=lambda: self.editor.event_generate("<<SelectAll>>"))
  67.  
  68.     def new_file(self):
  69.         self.editor.delete(1.0, tk.END)
  70.         self.filename = None
  71.         self.status.config(text="New File")
  72.  
  73.     def open_file(self):
  74.         self.filename = filedialog.askopenfilename(
  75.             defaultextension=".py",
  76.             filetypes=[("Python Files", "*.py"), ("All Files", "*.*")]
  77.         )
  78.         if self.filename:
  79.             with open(self.filename, 'r') as file:
  80.                 self.editor.delete(1.0, tk.END)
  81.                 self.editor.insert(1.0, file.read())
  82.             self.status.config(text=f"Opened {os.path.basename(self.filename)}")
  83.             self.syntax_highlight()
  84.  
  85.     def save_file(self):
  86.         if self.filename:
  87.             with open(self.filename, 'w') as file:
  88.                 file.write(self.editor.get(1.0, tk.END))
  89.             self.status.config(text=f"Saved {os.path.basename(self.filename)}")
  90.         else:
  91.             self.save_as_file()
  92.  
  93.     def save_as_file(self):
  94.         self.filename = filedialog.asksaveasfilename(
  95.             defaultextension=".py",
  96.             filetypes=[("Python Files", "*.py"), ("All Files", "*.*")]
  97.         )
  98.         if self.filename:
  99.             with open(self.filename, 'w') as file:
  100.                 file.write(self.editor.get(1.0, tk.END))
  101.             self.status.config(text=f"Saved As {os.path.basename(self.filename)}")
  102.  
  103.     def run_code(self):
  104.         if self.filename is None:
  105.             self.save_as_file()
  106.  
  107.         if self.filename:
  108.             self.console.delete(1.0, tk.END)
  109.             cmd = f'python "{self.filename}"'
  110.             process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  111.             output, error = process.communicate()
  112.             self.console.insert(tk.END, output.decode())
  113.             self.console.insert(tk.END, error.decode())
  114.  
  115.     def syntax_highlight(self, event=None):
  116.         # Remove previous tags
  117.         self.editor.tag_remove("keyword", "1.0", tk.END)
  118.         self.editor.tag_remove("string", "1.0", tk.END)
  119.         self.editor.tag_remove("comment", "1.0", tk.END)
  120.         self.editor.tag_remove("number", "1.0", tk.END)
  121.  
  122.         # Python keywords
  123.         keywords = r'\b(' + '|'.join(keyword.kwlist) + r')\b'
  124.  
  125.         # Regular expressions for strings, comments, and numbers
  126.         string_pattern = r'(\'[^\']*\'|\"[^\"]*\")'
  127.         comment_pattern = r'#[^\n]*'
  128.         number_pattern = r'\b\d+(\.\d*)?\b'
  129.  
  130.         content = self.editor.get("1.0", tk.END)
  131.  
  132.         # Apply keyword highlighting
  133.         for match in re.finditer(keywords, content):
  134.             start_idx = f"1.0 + {match.start()} chars"
  135.             end_idx = f"1.0 + {match.end()} chars"
  136.             self.editor.tag_add("keyword", start_idx, end_idx)
  137.  
  138.         # Apply string highlighting
  139.         for match in re.finditer(string_pattern, content):
  140.             start_idx = f"1.0 + {match.start()} chars"
  141.             end_idx = f"1.0 + {match.end()} chars"
  142.             self.editor.tag_add("string", start_idx, end_idx)
  143.  
  144.         # Apply comment highlighting
  145.         for match in re.finditer(comment_pattern, content):
  146.             start_idx = f"1.0 + {match.start()} chars"
  147.             end_idx = f"1.0 + {match.end()} chars"
  148.             self.editor.tag_add("comment", start_idx, end_idx)
  149.  
  150.         # Apply number highlighting
  151.         for match in re.finditer(number_pattern, content):
  152.             start_idx = f"1.0 + {match.start()} chars"
  153.             end_idx = f"1.0 + {match.end()} chars"
  154.             self.editor.tag_add("number", start_idx, end_idx)
  155.  
  156.  
  157. if __name__ == "__main__":
  158.     root = tk.Tk()
  159.     ide = PythonIDE(root)
  160.     root.mainloop()
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement