I made a little program where I can save prompts to execute in the command prompt, with a checkbox to open cmd as admin, and when I execute in Visual Code everything works fine.
Now I made a .exe with pyinstaller (pyinstaller –onefile –windowed myscript.py)
When I use the .exe, I can use my prompts when the checkbox for admin is checked, it opens the cmd as admin and executes. But when the checkbox is unchecked, the cmd doesn’t open. Instead, it opens my .exe again, so 2 windows are open. Does somebody see my mistake?
import tkinter as tk from tkinter import simpledialog, messagebox, filedialog import subprocess, sys, json, os documents_folder = os.path.expanduser('~/Documents') command_saver_folder = os.path.join(documents_folder, 'cmd_prompt_saver') if not os.path.exists(command_saver_folder): os.makedirs(command_saver_folder) file_path = os.path.join(command_saver_folder, 'commands.json') def load_commands(): """Load commands from JSON file""" if os.path.exists(file_path): with open(file_path, "r") as file: return json.load(file) return {} def save_commands(): """Save commands to JSON file""" with open(file_path, "w") as file: json.dump(commands, file, indent=4) def add_command(): command = simpledialog.askstring("Add Command", "Enter the command to save:") if command: name = simpledialog.askstring("Command Name", "Enter a name for this command:") if name: commands[name] = command save_commands() create_buttons() else: messagebox.showerror("Error", "Command name cannot be empty.") def select_directory(): directory = filedialog.askdirectory(title="Select Directory") # Open file explorer return directory def execute_command(cmd, run_as_admin): directory = select_directory() if directory: full_command = f'cd /d {directory} && {cmd}' if run_as_admin: # Check if global 'Run as Admin' is selected subprocess.Popen(f'start powershell -Command "Start-Process cmd -ArgumentList '/k {full_command}' -Verb RunAs"', shell=True) else: subprocess.Popen(f'start cmd /k "{full_command}"', shell=True) def delete_command(name): if name in commands: del commands[name] save_commands() # Save the updated commands create_buttons() # Recreate the buttons to reflect the change def create_buttons(): for widget in frame.winfo_children(): widget.destroy() for name, cmd in commands.items(): button_frame = tk.Frame(frame) button_frame.pack(fill=tk.X, padx=5, pady=2) delete_btn = tk.Button(button_frame, text="Delete", width=8, command=lambda n=name: delete_command(n), bg="#8B0000", fg="white") delete_btn.pack(side="right") cmd_btn = tk.Button(button_frame, text=name, command=lambda c=cmd: execute_command(c, run_as_admin.get())) cmd_btn.pack(side="right", fill=tk.X, padx=5) commands = load_commands() root = tk.Tk() root.title("Command Shortcuts") run_as_admin = tk.BooleanVar() # Global variable to track "Run as Admin" setting frame = tk.Frame(root) frame.pack(pady=10, padx=10) run_as_admin_checkbox = tk.Checkbutton(root, text="Run commands as Admin", variable=run_as_admin) run_as_admin_checkbox.pack(pady=10) add_button = tk.Button(root, text="Add Command", command=add_command, bg="#228B22", fg="white") add_button.pack(pady=5) create_buttons() root.mainloop()
submitted by /u/Nagilion
[link] [comments]
r/learnpython I made a little program where I can save prompts to execute in the command prompt, with a checkbox to open cmd as admin, and when I execute in Visual Code everything works fine. Now I made a .exe with pyinstaller (pyinstaller –onefile –windowed myscript.py) When I use the .exe, I can use my prompts when the checkbox for admin is checked, it opens the cmd as admin and executes. But when the checkbox is unchecked, the cmd doesn’t open. Instead, it opens my .exe again, so 2 windows are open. Does somebody see my mistake? import tkinter as tk from tkinter import simpledialog, messagebox, filedialog import subprocess, sys, json, os documents_folder = os.path.expanduser(‘~/Documents’) command_saver_folder = os.path.join(documents_folder, ‘cmd_prompt_saver’) if not os.path.exists(command_saver_folder): os.makedirs(command_saver_folder) file_path = os.path.join(command_saver_folder, ‘commands.json’) def load_commands(): “””Load commands from JSON file””” if os.path.exists(file_path): with open(file_path, “r”) as file: return json.load(file) return {} def save_commands(): “””Save commands to JSON file””” with open(file_path, “w”) as file: json.dump(commands, file, indent=4) def add_command(): command = simpledialog.askstring(“Add Command”, “Enter the command to save:”) if command: name = simpledialog.askstring(“Command Name”, “Enter a name for this command:”) if name: commands[name] = command save_commands() create_buttons() else: messagebox.showerror(“Error”, “Command name cannot be empty.”) def select_directory(): directory = filedialog.askdirectory(title=”Select Directory”) # Open file explorer return directory def execute_command(cmd, run_as_admin): directory = select_directory() if directory: full_command = f’cd /d {directory} && {cmd}’ if run_as_admin: # Check if global ‘Run as Admin’ is selected subprocess.Popen(f’start powershell -Command “Start-Process cmd -ArgumentList ‘/k {full_command}’ -Verb RunAs”‘, shell=True) else: subprocess.Popen(f’start cmd /k “{full_command}”‘, shell=True) def delete_command(name): if name in commands: del commands[name] save_commands() # Save the updated commands create_buttons() # Recreate the buttons to reflect the change def create_buttons(): for widget in frame.winfo_children(): widget.destroy() for name, cmd in commands.items(): button_frame = tk.Frame(frame) button_frame.pack(fill=tk.X, padx=5, pady=2) delete_btn = tk.Button(button_frame, text=”Delete”, width=8, command=lambda n=name: delete_command(n), bg=”#8B0000″, fg=”white”) delete_btn.pack(side=”right”) cmd_btn = tk.Button(button_frame, text=name, command=lambda c=cmd: execute_command(c, run_as_admin.get())) cmd_btn.pack(side=”right”, fill=tk.X, padx=5) commands = load_commands() root = tk.Tk() root.title(“Command Shortcuts”) run_as_admin = tk.BooleanVar() # Global variable to track “Run as Admin” setting frame = tk.Frame(root) frame.pack(pady=10, padx=10) run_as_admin_checkbox = tk.Checkbutton(root, text=”Run commands as Admin”, variable=run_as_admin) run_as_admin_checkbox.pack(pady=10) add_button = tk.Button(root, text=”Add Command”, command=add_command, bg=”#228B22″, fg=”white”) add_button.pack(pady=5) create_buttons() root.mainloop() submitted by /u/Nagilion [link] [comments]
I made a little program where I can save prompts to execute in the command prompt, with a checkbox to open cmd as admin, and when I execute in Visual Code everything works fine.
Now I made a .exe with pyinstaller (pyinstaller –onefile –windowed myscript.py)
When I use the .exe, I can use my prompts when the checkbox for admin is checked, it opens the cmd as admin and executes. But when the checkbox is unchecked, the cmd doesn’t open. Instead, it opens my .exe again, so 2 windows are open. Does somebody see my mistake?
import tkinter as tk from tkinter import simpledialog, messagebox, filedialog import subprocess, sys, json, os documents_folder = os.path.expanduser('~/Documents') command_saver_folder = os.path.join(documents_folder, 'cmd_prompt_saver') if not os.path.exists(command_saver_folder): os.makedirs(command_saver_folder) file_path = os.path.join(command_saver_folder, 'commands.json') def load_commands(): """Load commands from JSON file""" if os.path.exists(file_path): with open(file_path, "r") as file: return json.load(file) return {} def save_commands(): """Save commands to JSON file""" with open(file_path, "w") as file: json.dump(commands, file, indent=4) def add_command(): command = simpledialog.askstring("Add Command", "Enter the command to save:") if command: name = simpledialog.askstring("Command Name", "Enter a name for this command:") if name: commands[name] = command save_commands() create_buttons() else: messagebox.showerror("Error", "Command name cannot be empty.") def select_directory(): directory = filedialog.askdirectory(title="Select Directory") # Open file explorer return directory def execute_command(cmd, run_as_admin): directory = select_directory() if directory: full_command = f'cd /d {directory} && {cmd}' if run_as_admin: # Check if global 'Run as Admin' is selected subprocess.Popen(f'start powershell -Command "Start-Process cmd -ArgumentList '/k {full_command}' -Verb RunAs"', shell=True) else: subprocess.Popen(f'start cmd /k "{full_command}"', shell=True) def delete_command(name): if name in commands: del commands[name] save_commands() # Save the updated commands create_buttons() # Recreate the buttons to reflect the change def create_buttons(): for widget in frame.winfo_children(): widget.destroy() for name, cmd in commands.items(): button_frame = tk.Frame(frame) button_frame.pack(fill=tk.X, padx=5, pady=2) delete_btn = tk.Button(button_frame, text="Delete", width=8, command=lambda n=name: delete_command(n), bg="#8B0000", fg="white") delete_btn.pack(side="right") cmd_btn = tk.Button(button_frame, text=name, command=lambda c=cmd: execute_command(c, run_as_admin.get())) cmd_btn.pack(side="right", fill=tk.X, padx=5) commands = load_commands() root = tk.Tk() root.title("Command Shortcuts") run_as_admin = tk.BooleanVar() # Global variable to track "Run as Admin" setting frame = tk.Frame(root) frame.pack(pady=10, padx=10) run_as_admin_checkbox = tk.Checkbutton(root, text="Run commands as Admin", variable=run_as_admin) run_as_admin_checkbox.pack(pady=10) add_button = tk.Button(root, text="Add Command", command=add_command, bg="#228B22", fg="white") add_button.pack(pady=5) create_buttons() root.mainloop()
submitted by /u/Nagilion
[link] [comments]