Advertisement
Najeebsk

SAVE.pyw

Nov 21st, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk, filedialog, messagebox
  3. import os
  4. import subprocess
  5.  
  6. def save_file(event=None):
  7.     file_name = file_name_entry.get()
  8.     if not file_name.strip():
  9.         messagebox.showerror("Error", "Please enter a valid file name.")
  10.         return
  11.     file_path = filedialog.asksaveasfilename(defaultextension=".txt", initialfile=file_name)
  12.     if file_path:
  13.         with open(file_path, 'w') as file:
  14.             file.write(text_field.get("1.0", tk.END))
  15.         messagebox.showinfo("File Saved", f"File saved successfully: {file_path}")
  16.  
  17. def open_file(event=None):
  18.     file_path = filedialog.askopenfilename(filetypes=[("All Files", "*.*")])
  19.     if file_path:
  20.         try:
  21.             os.startfile(file_path)
  22.         except Exception as e:
  23.             messagebox.showerror("Error", f"Could not open file: {e}")
  24.  
  25. def run_command(event=None):
  26.     command = command_entry.get()
  27.     if command.strip():
  28.         try:
  29.             output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, text=True)
  30.             messagebox.showinfo("Command Output", output)
  31.         except subprocess.CalledProcessError as e:
  32.             messagebox.showerror("Command Error", e.output)
  33.     else:
  34.         messagebox.showerror("Error", "Please enter a command.")
  35.  
  36. # Main application window
  37. root = tk.Tk()
  38. root.title("Najeeb File Save And Open And Run Commands")
  39.  
  40. # File name field
  41. file_name_label = tk.Label(root, text="File Name:")
  42. file_name_label.grid(row=0, column=0, padx=5, pady=5, sticky="w")
  43.  
  44. file_name_entry = tk.Entry(root, width=40)
  45. file_name_entry.grid(row=0, column=1, padx=5, pady=5, sticky="w")
  46.  
  47. save_button = tk.Button(root, text="Save File", command=save_file)
  48. save_button.grid(row=0, column=2, padx=5, pady=5)
  49.  
  50. open_button = tk.Button(root, text="Open File", command=open_file)
  51. open_button.grid(row=0, column=3, padx=5, pady=5)
  52.  
  53. # Text field with scrollbars
  54. text_frame = tk.Frame(root)
  55. text_frame.grid(row=1, column=0, columnspan=4, padx=5, pady=5, sticky="nsew")
  56.  
  57. text_field = tk.Text(text_frame, wrap="none", undo=True)
  58. text_field.grid(row=0, column=0, sticky="nsew")
  59.  
  60. text_scroll_y = ttk.Scrollbar(text_frame, orient="vertical", command=text_field.yview)
  61. text_scroll_y.grid(row=0, column=1, sticky="ns")
  62. text_field.configure(yscrollcommand=text_scroll_y.set)
  63.  
  64. text_scroll_x = ttk.Scrollbar(text_frame, orient="horizontal", command=text_field.xview)
  65. text_scroll_x.grid(row=1, column=0, sticky="ew")
  66. text_field.configure(xscrollcommand=text_scroll_x.set)
  67.  
  68. text_frame.rowconfigure(0, weight=1)
  69. text_frame.columnconfigure(0, weight=1)
  70.  
  71. # Command field and Run button
  72. command_label = tk.Label(root, text="Command:")
  73. command_label.grid(row=2, column=0, padx=5, pady=5, sticky="w")
  74.  
  75. command_entry = tk.Entry(root, width=60)
  76. command_entry.grid(row=2, column=1, padx=5, pady=5, sticky="w")
  77.  
  78. run_button = tk.Button(root, text="Run", command=run_command)
  79. run_button.grid(row=2, column=2, padx=5, pady=5)
  80.  
  81. # Adjust window scaling
  82. root.rowconfigure(1, weight=1)
  83. root.columnconfigure(1, weight=1)
  84.  
  85. # Bind keyboard shortcuts
  86. root.bind('<Control-s>', save_file)  # Save file with Ctrl+S
  87. root.bind('<Control-o>', open_file)  # Open file with Ctrl+O
  88. root.bind('<Control-r>', run_command)  # Run command with Ctrl+R
  89.  
  90. root.mainloop()
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement