Advertisement
MizunoBrasil

Aplicativo de Tradução

Jun 23rd, 2023
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import messagebox
  3. from tkinter import ttk
  4. import pyperclip
  5. from googletrans import Translator
  6.  
  7. # Função para traduzir o texto e copiar para a área de transferência
  8. def translate_and_copy():
  9.     text = input_text.get("1.0", "end-1c")
  10.     if text:
  11.         translator = Translator(service_urls=['translate.google.com'])
  12.         try:
  13.             translation = translator.translate(text, dest='pt')
  14.             output_text.delete("1.0", "end")
  15.             output_text.insert("1.0", translation.text)
  16.             pyperclip.copy(translation.text)
  17.         except Exception as e:
  18.             messagebox.showerror("Erro", "Ocorreu um erro durante a tradução:\n{}".format(str(e)))
  19.     else:
  20.         messagebox.showwarning("Atenção", "Por favor, insira um texto para traduzir!")
  21.  
  22. # Configuração da janela principal
  23. window = tk.Tk()
  24. window.title("Aplicativo de Tradução")
  25. window_width = 670
  26. window_height = 700
  27.  
  28. # Obter as dimensões da tela
  29. screen_width = window.winfo_screenwidth()
  30. screen_height = window.winfo_screenheight()
  31.  
  32. # Calcular as coordenadas para centralizar a janela
  33. x = (screen_width - window_width) // 2
  34. y = (screen_height - window_height) // 2
  35.  
  36. # Definir a geometria da janela
  37. window.geometry(f"{window_width}x{window_height}+{x}+{y}")
  38.  
  39. # Rótulo e caixa de texto para entrada do texto
  40. input_label = tk.Label(window, text="Texto:")
  41. input_label.pack()
  42. input_text = tk.Text(window, height=15)  # Aumenta o tamanho vertical da caixa de texto
  43. input_text.pack()
  44.  
  45. # Botão para traduzir e copiar o texto
  46. translate_button = ttk.Button(window, text="Traduzir", command=translate_and_copy)
  47. translate_button.pack(pady=10)
  48.  
  49. # Rótulo e caixa de texto para exibição da tradução
  50. output_label = tk.Label(window, text="Tradução:")
  51. output_label.pack()
  52. output_text = tk.Text(window, height=15)  # Aumenta o tamanho vertical da caixa de texto
  53. output_text.pack()
  54.  
  55. window.mainloop()
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement