Mundo em Python

Mover ficheiros (usando Tkinter)

from tkinter import *
from tkinter import filedialog, messagebox
import shutil
import os # Configuração da janela
root = Tk()
root.geometry("600x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Mover Ficheiro") # Título
titulo = Label(root, text="Mover Ficheiro", font=("Arial", 28, "bold"), bg="#103030", fg="#49e3e3")
titulo.place(relx=0.25, rely=0.05) # Labels
texto_sub1 = Label(root, text="Ficheiro de Origem :", font=("Arial", 18, "bold"), bg="#103030", fg="#49e3e3")
texto_sub1.place(relx=0.05, rely=0.28) texto_sub2 = Label(root, text="Pasta de Destino :", font=("Arial", 18, "bold"), bg="#103030", fg="#49e3e3")
texto_sub2.place(relx=0.05, rely=0.45) # Inputs
Origem = StringVar()
Origem_entrada = Entry(root, textvariable=Origem, font=("Arial", 12, "bold"), bg="white", fg="blue", justify='center')
Origem_entrada.place(relx=0.47, rely=0.29, relwidth=0.47) Destino = StringVar()
Destino_entrada = Entry(root, textvariable=Destino, font=("Arial", 12, "bold"), bg="white", fg="blue", justify='center')
Destino_entrada.place(relx=0.47, rely=0.46, relwidth=0.47) # Função para selecionar arquivo de origem
def escolher_origem():
caminho = filedialog.askopenfilename(title="Escolha o arquivo")
Origem.set(caminho) # Função para selecionar pasta de destino
def escolher_destino():
caminho = filedialog.askdirectory(title="Escolha a pasta de destino")
Destino.set(caminho) # Função para mover arquivo
def app():
orig = Origem.get()
des = Destino.get() if not os.path.exists(orig):
messagebox.showerror("Erro", "O arquivo de origem não existe!")
return
if not os.path.isdir(des):
messagebox.showerror("Erro", "O destino deve ser uma pasta válida!")
return

try:
shutil.move(orig, os.path.join(des, os.path.basename(orig)))
messagebox.showinfo("Sucesso", "Arquivo movido com sucesso!")
limpar()
except Exception as e:
messagebox.showerror("Erro", f"Erro ao mover arquivo: {str(e)}") # Função para limpar os campos
def limpar():
Origem_entrada.delete(0, END)
Destino_entrada.delete(0, END) # Botões de seleção
but_origem = Button(root, text="Selecionar Arquivo", bd=2, bg='#107db2', fg='white', font=('verdana', 10, 'bold'), command=escolher_origem)
but_origem.place(relx=0.05, rely=0.35, relwidth=0.25, relheight=0.07) but_destino = Button(root, text="Selecionar Pasta", bd=2, bg='#107db2', fg='white', font=('verdana', 10, 'bold'), command=escolher_destino)
but_destino.place(relx=0.05, rely=0.52, relwidth=0.25, relheight=0.07) # Botões principais
but1 = Button(root, text="Mover", bd=2, bg='#107db2', fg='white', font=('verdana', 12, 'bold'), command=app)
but1.place(relx=0.1, rely=0.65, relwidth=0.25, relheight=0.1) but_limpar = Button(root, text="Limpar", bd=2, bg='#107db2', fg='white', font=('verdana', 12, 'bold'), command=limpar)
but_limpar.place(relx=0.4, rely=0.65, relwidth=0.25, relheight=0.1) but_sair = Button(root, text="Sair", bd=2, bg='#107db2', fg='white', font=('verdana', 12, 'bold'), command=root.destroy)
but_sair.place(relx=0.7, rely=0.65, relwidth=0.25, relheight=0.1) # Loop da interface gráfica
root.mainloop()
0