Last active
October 12, 2024 21:10
-
-
Save rhcarlosweb/2673ae7f15790b389d74b073959c6f8f to your computer and use it in GitHub Desktop.
UI para crirar symlink em python | Symlink creator with UI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import importlib | |
import subprocess | |
import sys | |
def install_and_import(package): | |
try: | |
importlib.import_module(package) | |
except ImportError: | |
subprocess.check_call([sys.executable, "-m", "pip", "install", package]) | |
finally: | |
globals()[package] = importlib.import_module(package) | |
install_and_import('tkinter') | |
install_and_import('ttkbootstrap') | |
import tkinter as tk | |
from tkinter import filedialog, messagebox | |
import os | |
import ttkbootstrap as ttk | |
from ttkbootstrap.constants import * | |
def criar_directory_junction(): | |
origem = origem_entry.get() | |
destino = destino_entry.get() | |
nome_atalho = nome_atalho_entry.get() | |
if origem and destino and nome_atalho: | |
if os.path.exists(origem) and os.path.exists(destino): | |
destino_com_nome = os.path.join(destino, nome_atalho) | |
try: | |
os.system(f'mklink /J "{destino_com_nome}" "{origem}"') | |
resultado_label.config(text="Directory Junction criado com sucesso!", bootstyle="success") | |
except Exception as e: | |
resultado_label.config(text=f"Erro ao criar Directory Junction: {str(e)}", bootstyle="danger") | |
else: | |
resultado_label.config(text="Certifique-se de que a pasta de origem e de destino existam.", bootstyle="warning") | |
else: | |
resultado_label.config(text="Preencha todos os campos antes de criar o Directory Junction.", bootstyle="warning") | |
def selecionar_origem(): | |
origem = filedialog.askdirectory() | |
origem_entry.delete(0, tk.END) | |
origem_entry.insert(0, origem) | |
def selecionar_destino(): | |
destino = filedialog.askdirectory() | |
destino_entry.delete(0, tk.END) | |
destino_entry.insert(0, destino) | |
def limpar_campos(): | |
origem_entry.delete(0, tk.END) | |
destino_entry.delete(0, tk.END) | |
nome_atalho_entry.delete(0, tk.END) | |
resultado_label.config(text="", bootstyle="default") | |
# Cria a janela principal com tema moderno | |
janela = ttk.Window(themename="darkly") | |
janela.title("Criar Directory Junction") | |
janela.geometry("600x400") | |
# Cria e configura os widgets | |
frame = ttk.Frame(janela, padding=20) | |
frame.pack(fill=BOTH, expand=YES) | |
origem_label = ttk.Label(frame, text="Pasta de Origem:") | |
origem_entry = ttk.Entry(frame, width=40) | |
selecionar_origem_button = ttk.Button(frame, text="Selecionar", command=selecionar_origem, bootstyle="info-outline") | |
destino_label = ttk.Label(frame, text="Pasta de Destino:") | |
destino_entry = ttk.Entry(frame, width=40) | |
selecionar_destino_button = ttk.Button(frame, text="Selecionar", command=selecionar_destino, bootstyle="info-outline") | |
nome_atalho_label = ttk.Label(frame, text="Nome do Atalho:") | |
nome_atalho_entry = ttk.Entry(frame, width=40) | |
criar_directory_junction_button = ttk.Button(frame, text="Criar Directory Junction", command=criar_directory_junction, bootstyle="success") | |
limpar_campos_button = ttk.Button(frame, text="Limpar Campos", command=limpar_campos, bootstyle="secondary") | |
resultado_label = ttk.Label(frame, text="", bootstyle="default") | |
# Organiza os widgets na janela | |
origem_label.grid(row=0, column=0, sticky="w", pady=10) | |
origem_entry.grid(row=0, column=1, padx=10, pady=10) | |
selecionar_origem_button.grid(row=0, column=2, pady=10) | |
destino_label.grid(row=1, column=0, sticky="w", pady=10) | |
destino_entry.grid(row=1, column=1, padx=10, pady=10) | |
selecionar_destino_button.grid(row=1, column=2, pady=10) | |
nome_atalho_label.grid(row=2, column=0, sticky="w", pady=10) | |
nome_atalho_entry.grid(row=2, column=1, padx=10, pady=10) | |
criar_directory_junction_button.grid(row=3, column=1, pady=20) | |
limpar_campos_button.grid(row=3, column=2, pady=20) | |
resultado_label.grid(row=4, columnspan=3, pady=10) | |
# Configura o redimensionamento da janela | |
frame.columnconfigure(1, weight=1) | |
for i in range(5): | |
frame.rowconfigure(i, weight=1) | |
# Inicia a interface gráfica | |
janela.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment