Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import ttk, filedialog, messagebox
- import os
- import subprocess
- def save_file(event=None):
- file_name = file_name_entry.get()
- if not file_name.strip():
- messagebox.showerror("Error", "Please enter a valid file name.")
- return
- file_path = filedialog.asksaveasfilename(defaultextension=".txt", initialfile=file_name)
- if file_path:
- with open(file_path, 'w') as file:
- file.write(text_field.get("1.0", tk.END))
- messagebox.showinfo("File Saved", f"File saved successfully: {file_path}")
- def open_file(event=None):
- file_path = filedialog.askopenfilename(filetypes=[("All Files", "*.*")])
- if file_path:
- try:
- os.startfile(file_path)
- except Exception as e:
- messagebox.showerror("Error", f"Could not open file: {e}")
- def run_command(event=None):
- command = command_entry.get()
- if command.strip():
- try:
- output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, text=True)
- messagebox.showinfo("Command Output", output)
- except subprocess.CalledProcessError as e:
- messagebox.showerror("Command Error", e.output)
- else:
- messagebox.showerror("Error", "Please enter a command.")
- # Main application window
- root = tk.Tk()
- root.title("Najeeb File Save And Open And Run Commands")
- # File name field
- file_name_label = tk.Label(root, text="File Name:")
- file_name_label.grid(row=0, column=0, padx=5, pady=5, sticky="w")
- file_name_entry = tk.Entry(root, width=40)
- file_name_entry.grid(row=0, column=1, padx=5, pady=5, sticky="w")
- save_button = tk.Button(root, text="Save File", command=save_file)
- save_button.grid(row=0, column=2, padx=5, pady=5)
- open_button = tk.Button(root, text="Open File", command=open_file)
- open_button.grid(row=0, column=3, padx=5, pady=5)
- # Text field with scrollbars
- text_frame = tk.Frame(root)
- text_frame.grid(row=1, column=0, columnspan=4, padx=5, pady=5, sticky="nsew")
- text_field = tk.Text(text_frame, wrap="none", undo=True)
- text_field.grid(row=0, column=0, sticky="nsew")
- text_scroll_y = ttk.Scrollbar(text_frame, orient="vertical", command=text_field.yview)
- text_scroll_y.grid(row=0, column=1, sticky="ns")
- text_field.configure(yscrollcommand=text_scroll_y.set)
- text_scroll_x = ttk.Scrollbar(text_frame, orient="horizontal", command=text_field.xview)
- text_scroll_x.grid(row=1, column=0, sticky="ew")
- text_field.configure(xscrollcommand=text_scroll_x.set)
- text_frame.rowconfigure(0, weight=1)
- text_frame.columnconfigure(0, weight=1)
- # Command field and Run button
- command_label = tk.Label(root, text="Command:")
- command_label.grid(row=2, column=0, padx=5, pady=5, sticky="w")
- command_entry = tk.Entry(root, width=60)
- command_entry.grid(row=2, column=1, padx=5, pady=5, sticky="w")
- run_button = tk.Button(root, text="Run", command=run_command)
- run_button.grid(row=2, column=2, padx=5, pady=5)
- # Adjust window scaling
- root.rowconfigure(1, weight=1)
- root.columnconfigure(1, weight=1)
- # Bind keyboard shortcuts
- root.bind('<Control-s>', save_file) # Save file with Ctrl+S
- root.bind('<Control-o>', open_file) # Open file with Ctrl+O
- root.bind('<Control-r>', run_command) # Run command with Ctrl+R
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement