Advertisement
MizunoBrasil

Barra de Progresso em Python com caixa de diálogo

Jun 30th, 2023
1,144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.90 KB | None | 0 0
  1. #Barra de Progresso em Python com caixa de diálogo que mostra cópia de arquivo atualizada em tempo praticamente real.
  2.  
  3. from tkinter import *
  4. from tkinter import ttk
  5. from tkinter import messagebox
  6. from tkinter import filedialog
  7. import shutil
  8. import os
  9.  
  10. root = Tk()
  11. root.title("Cópia de Arquivo com Barra de Progresso - Mizuno")
  12. root.geometry("500x250")
  13.  
  14. def select_source_file():
  15.     filename = filedialog.askopenfilename(title="Selecionar arquivo de origem")
  16.     source_entry.delete(0, END)
  17.     source_entry.insert(0, filename)
  18.  
  19. def select_destination_folder():
  20.     foldername = filedialog.askdirectory(title="Selecionar pasta de destino")
  21.     destination_entry.delete(0, END)
  22.     destination_entry.insert(0, foldername)
  23.  
  24. def copy_file():
  25.     source_file = source_entry.get()
  26.     destination_folder = destination_entry.get()
  27.  
  28.     if not source_file or not destination_folder:
  29.         messagebox.showwarning("Aviso", "Selecione o arquivo de origem e a pasta de destino.")
  30.         return
  31.  
  32.     progress1['value'] = 0
  33.     root.update_idletasks()
  34.  
  35.     # Obtém o tamanho total do arquivo para calcular o progresso
  36.     total_size = os.path.getsize(source_file)
  37.     bytes_copied = 0
  38.  
  39.     destination_file = os.path.join(destination_folder, os.path.basename(source_file))
  40.  
  41.     with open(source_file, "rb") as src, open(destination_file, "wb") as dst:
  42.         while True:
  43.             # Lê e escreve em blocos para melhor desempenho
  44.             buf = src.read(4096)
  45.             if not buf:
  46.                 break
  47.  
  48.             dst.write(buf)
  49.             bytes_copied += len(buf)
  50.  
  51.             # Calcula o progresso e atualiza a barra de progresso
  52.             progress = (bytes_copied / total_size) * 100
  53.             progress1['value'] = progress
  54.             root.update_idletasks()
  55.  
  56.     progress1['value'] = 100
  57.     root.update_idletasks()
  58.  
  59.     messagebox.showinfo("CÓPIA OK", "Cópia realizada com sucesso!")
  60.  
  61. progress1 = ttk.Progressbar(root, orient=HORIZONTAL, length=300, mode='determinate')
  62. progress1.pack(pady=20)
  63.  
  64. source_frame = Frame(root)
  65. source_frame.pack(pady=10)
  66.  
  67. source_label = Label(source_frame, text="Arquivo de origem:")
  68. source_label.pack(side=LEFT)
  69.  
  70. source_entry = Entry(source_frame, width=40)
  71. source_entry.pack(side=LEFT)
  72.  
  73. source_button = Button(source_frame, text="Selecionar arquivo", command=select_source_file)
  74. source_button.pack(side=LEFT)
  75.  
  76. destination_frame = Frame(root)
  77. destination_frame.pack(pady=10)
  78.  
  79. destination_label = Label(destination_frame, text="Pasta de destino:")
  80. destination_label.pack(side=LEFT)
  81.  
  82. destination_entry = Entry(destination_frame, width=40)
  83. destination_entry.pack(side=LEFT)
  84.  
  85. destination_button = Button(destination_frame, text="Selecionar pasta", command=select_destination_folder)
  86. destination_button.pack(side=LEFT)
  87.  
  88. button = Button(root, text="Fazer cópia do arquivo", command=copy_file)
  89. button.pack(pady=20)
  90.  
  91. root.mainloop()
  92.  
  93.  
  94.  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement