Advertisement
Najeebsk

CMDER.PY

Feb 20th, 2024
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.89 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk
  3. import subprocess
  4.  
  5. class ConsoleWindow(tk.Toplevel):
  6.     def __init__(self, parent):
  7.         super().__init__(parent)
  8.         self.geometry('1000x300')
  9.         self.title("Najeeb CMD ALIASES COMMONDS RUNNER")
  10.         self.iconbitmap('C.ico')  # Add icon to the title bar
  11.         self.text_area = tk.Text(self, wrap="word", state="disabled")
  12.         self.text_area.pack(expand=True, fill="both")
  13.         self.scrollbar = tk.Scrollbar(self, command=self.text_area.yview)
  14.         self.scrollbar.pack(side="right", fill="y")
  15.         self.text_area.config(yscrollcommand=self.scrollbar.set)
  16.  
  17.     def append_text(self, text, tag=None):
  18.         self.text_area.config(state="normal")
  19.         if tag:
  20.             self.text_area.insert("end", text, tag)
  21.         else:
  22.             self.text_area.insert("end", text)
  23.         self.text_area.config(state="disabled")
  24.         self.text_area.see("end")
  25.  
  26. class AliasButtonExample(tk.Tk):
  27.     def __init__(self):
  28.         super().__init__()
  29.         self.geometry('1000x340')
  30.         self.config(bg='green')
  31.         self.title("Najeeb CMD ALIASES COMMONDS RUNNER")
  32.         self.iconbitmap('C.ico')  # Add icon to the title bar
  33.  
  34.         self.aliases = {}
  35.  
  36.         self.create_widgets()
  37.  
  38.     def create_widgets(self):
  39.         style = ttk.Style()
  40.         style.configure("TButton", foreground="blue", background="lightgray", font=('Arial', 10, 'bold'))
  41.         style.configure("TLabel", foreground="darkblue", background="lightgray", font=('Arial', 10))
  42.  
  43.         tk.Label(self, text="Command:").grid(row=0, column=0, padx=5, pady=5)
  44.         tk.Label(self, text="Alias:").grid(row=1, column=0, padx=5, pady=5)
  45.         tk.Label(self, text="Aliases: Najeeb New Aliases Add And Save").grid(row=2, column=0, sticky="n", padx=5, pady=5)
  46.  
  47.         self.command_entry = tk.Entry(self, width=250)  # Increase width here
  48.         self.command_entry.grid(row=0, column=1, padx=5, pady=5)
  49.  
  50.         self.alias_entry = tk.Entry(self, width=250)
  51.         self.alias_entry.grid(row=1, column=1, padx=5, pady=5)
  52.  
  53.         self.alias_button = ttk.Button(self, text="Insert Alias", command=self.insert_alias, style="TButton")
  54.         self.alias_button.grid(row=1, column=2, padx=5, pady=5)
  55.  
  56.         self.run_button = ttk.Button(self, text="Run Command", command=self.run_command, style="TButton")
  57.         self.run_button.grid(row=0, column=2, rowspan=1, padx=5, pady=5, sticky="nsew")
  58.  
  59.         self.save_button = ttk.Button(self, text="Save Aliases", command=self.save_aliases, style="TButton")
  60.         self.save_button.grid(row=2, column=2, padx=5, pady=5)
  61.  
  62.         self.aliases_frame = tk.Frame(self)
  63.         self.aliases_frame.grid(row=3, column=0, columnspan=3, padx=5, pady=5, sticky="nsew")
  64.  
  65.         self.aliases_text = tk.Text(self.aliases_frame, wrap="word", state="normal", height=10, width=120)
  66.         self.aliases_text.grid(row=0, column=0, padx=5, pady=5, sticky="nsew")
  67.  
  68.         self.aliases_scrollbar = tk.Scrollbar(self.aliases_frame, orient="vertical", command=self.aliases_text.yview)
  69.         self.aliases_scrollbar.grid(row=0, column=1, sticky="ns")
  70.         self.aliases_text.config(yscrollcommand=self.aliases_scrollbar.set)
  71.  
  72.         self.search_label = tk.Label(self, text="Search Aliases:")
  73.         self.search_label.grid(row=4, column=0, padx=5, pady=5, sticky="e")
  74.        
  75.         self.search_entry = tk.Entry(self, width=250)
  76.         self.search_entry.grid(row=4, column=1, padx=5, pady=5)
  77.  
  78.         self.search_button = ttk.Button(self, text="Search", command=self.search_aliases, style="TButton")
  79.         self.search_button.grid(row=4, column=2, padx=5, pady=5)
  80.  
  81.         self.console_window = ConsoleWindow(self)
  82.  
  83.         self.grid_columnconfigure(1, weight=1)  # Expand column 1
  84.  
  85.         self.read_aliases()  # Call read_aliases to load aliases when the GUI is initialized
  86.  
  87.     def insert_alias(self):
  88.         alias = self.alias_entry.get()
  89.         print(f"Attempting to insert alias: {alias}")
  90.         if alias in self.aliases:
  91.             print(f"Alias found: {alias}")
  92.             self.command_entry.delete(0, tk.END)
  93.             self.command_entry.insert(tk.END, self.aliases[alias])
  94.         else:
  95.             print("Alias not found")
  96.  
  97.     def run_command(self):
  98.         command = self.command_entry.get()
  99.         process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  100.         stdout, stderr = process.communicate()
  101.         output = stdout.decode()
  102.         error = stderr.decode()
  103.         self.console_window.append_text(output)
  104.         if error:
  105.             self.console_window.append_text(error, tag="error")
  106.  
  107.     def save_aliases(self):
  108.         with open("Aliases.txt", "a") as f:
  109.             alias = self.alias_entry.get()
  110.             command = self.command_entry.get()
  111.             f.write(f"{alias}={command}\n")
  112.             self.aliases[alias] = command
  113.             self.aliases_text.insert("end", f"{alias}: {command}\n")  # Also update the aliases_text field
  114.  
  115.     def read_aliases(self):
  116.         try:
  117.             with open("Aliases.txt", "r") as f:
  118.                 for line in f:
  119.                     if '=' in line:
  120.                         alias, command = line.strip().split("=", 1)
  121.                         self.aliases[alias] = command
  122.                         self.aliases_text.insert("end", f"{alias}: {command}\n")
  123.         except FileNotFoundError:
  124.             # If the file doesn't exist, do nothing
  125.             pass
  126.  
  127.     def search_aliases(self):
  128.         query = self.search_entry.get().strip().lower()
  129.         self.aliases_text.delete('1.0', tk.END)  # Clear previous search results
  130.         for alias, command in self.aliases.items():
  131.             if query in alias.lower() or query in command.lower():
  132.                 self.aliases_text.insert(tk.END, f"{alias}: {command}\n")
  133.  
  134. if __name__ == "__main__":
  135.     app = AliasButtonExample()
  136.     app.mainloop()
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement