Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import filedialog, messagebox
- from tkinter.scrolledtext import ScrolledText
- import subprocess
- import os
- import keyword
- import re
- class PythonIDE:
- def __init__(self, root):
- self.root = root
- self.root.title("Najeeb Python IDE")
- self.root.geometry("1000x600")
- self.filename = None
- # Create the Menu Bar
- self.create_menu()
- # Create the text editor
- self.editor = ScrolledText(self.root, undo=True, wrap='word', font=("Times New Roman", 14))
- self.editor.pack(fill='both', expand=True)
- self.editor.bind("<KeyRelease>", self.syntax_highlight)
- # Output console
- self.console = ScrolledText(self.root, height=10, bg="black", fg="white", font=("Consolas", 12))
- self.console.pack(fill='x')
- # Create status bar
- self.status = tk.Label(self.root, text="Welcome to Python IDE", anchor='w')
- self.status.pack(side='bottom', fill='x')
- # Define syntax highlight colors
- self.editor.tag_configure("keyword", foreground="blue")
- self.editor.tag_configure("string", foreground="green")
- self.editor.tag_configure("comment", foreground="gray")
- self.editor.tag_configure("number", foreground="purple")
- def create_menu(self):
- menubar = tk.Menu(self.root)
- self.root.config(menu=menubar)
- # File menu
- file_menu = tk.Menu(menubar, tearoff=0)
- menubar.add_cascade(label="File", menu=file_menu)
- file_menu.add_command(label="New", command=self.new_file)
- file_menu.add_command(label="Open", command=self.open_file)
- file_menu.add_command(label="Save", command=self.save_file)
- file_menu.add_command(label="Save As", command=self.save_as_file)
- file_menu.add_separator()
- file_menu.add_command(label="Exit", command=self.root.quit)
- # Run menu
- run_menu = tk.Menu(menubar, tearoff=0)
- menubar.add_cascade(label="Run", menu=run_menu)
- run_menu.add_command(label="Run", command=self.run_code)
- # Edit menu
- edit_menu = tk.Menu(menubar, tearoff=0)
- menubar.add_cascade(label="Edit", menu=edit_menu)
- edit_menu.add_command(label="Undo", command=lambda: self.editor.edit_undo())
- edit_menu.add_command(label="Redo", command=lambda: self.editor.edit_redo())
- edit_menu.add_separator()
- edit_menu.add_command(label="Cut", command=lambda: self.editor.event_generate("<<Cut>>"))
- edit_menu.add_command(label="Copy", command=lambda: self.editor.event_generate("<<Copy>>"))
- edit_menu.add_command(label="Paste", command=lambda: self.editor.event_generate("<<Paste>>"))
- edit_menu.add_command(label="Select All", command=lambda: self.editor.event_generate("<<SelectAll>>"))
- def new_file(self):
- self.editor.delete(1.0, tk.END)
- self.filename = None
- self.status.config(text="New File")
- def open_file(self):
- self.filename = filedialog.askopenfilename(
- defaultextension=".py",
- filetypes=[("Python Files", "*.py"), ("All Files", "*.*")]
- )
- if self.filename:
- with open(self.filename, 'r') as file:
- self.editor.delete(1.0, tk.END)
- self.editor.insert(1.0, file.read())
- self.status.config(text=f"Opened {os.path.basename(self.filename)}")
- self.syntax_highlight()
- def save_file(self):
- if self.filename:
- with open(self.filename, 'w') as file:
- file.write(self.editor.get(1.0, tk.END))
- self.status.config(text=f"Saved {os.path.basename(self.filename)}")
- else:
- self.save_as_file()
- def save_as_file(self):
- self.filename = filedialog.asksaveasfilename(
- defaultextension=".py",
- filetypes=[("Python Files", "*.py"), ("All Files", "*.*")]
- )
- if self.filename:
- with open(self.filename, 'w') as file:
- file.write(self.editor.get(1.0, tk.END))
- self.status.config(text=f"Saved As {os.path.basename(self.filename)}")
- def run_code(self):
- if self.filename is None:
- self.save_as_file()
- if self.filename:
- self.console.delete(1.0, tk.END)
- cmd = f'python "{self.filename}"'
- process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
- output, error = process.communicate()
- self.console.insert(tk.END, output.decode())
- self.console.insert(tk.END, error.decode())
- def syntax_highlight(self, event=None):
- # Remove previous tags
- self.editor.tag_remove("keyword", "1.0", tk.END)
- self.editor.tag_remove("string", "1.0", tk.END)
- self.editor.tag_remove("comment", "1.0", tk.END)
- self.editor.tag_remove("number", "1.0", tk.END)
- # Python keywords
- keywords = r'\b(' + '|'.join(keyword.kwlist) + r')\b'
- # Regular expressions for strings, comments, and numbers
- string_pattern = r'(\'[^\']*\'|\"[^\"]*\")'
- comment_pattern = r'#[^\n]*'
- number_pattern = r'\b\d+(\.\d*)?\b'
- content = self.editor.get("1.0", tk.END)
- # Apply keyword highlighting
- for match in re.finditer(keywords, content):
- start_idx = f"1.0 + {match.start()} chars"
- end_idx = f"1.0 + {match.end()} chars"
- self.editor.tag_add("keyword", start_idx, end_idx)
- # Apply string highlighting
- for match in re.finditer(string_pattern, content):
- start_idx = f"1.0 + {match.start()} chars"
- end_idx = f"1.0 + {match.end()} chars"
- self.editor.tag_add("string", start_idx, end_idx)
- # Apply comment highlighting
- for match in re.finditer(comment_pattern, content):
- start_idx = f"1.0 + {match.start()} chars"
- end_idx = f"1.0 + {match.end()} chars"
- self.editor.tag_add("comment", start_idx, end_idx)
- # Apply number highlighting
- for match in re.finditer(number_pattern, content):
- start_idx = f"1.0 + {match.start()} chars"
- end_idx = f"1.0 + {match.end()} chars"
- self.editor.tag_add("number", start_idx, end_idx)
- if __name__ == "__main__":
- root = tk.Tk()
- ide = PythonIDE(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement