Advertisement
MizunoBrasil

Bloco de Notas Simples em Python

May 21st, 2024
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import Menu, filedialog, messagebox
  3.  
  4. def new_file():
  5.     """Limpa a área de texto para criar um novo arquivo."""
  6.     text_area.delete(1.0, tk.END)
  7.  
  8. def open_file():
  9.     """Abre um arquivo de texto e exibe o conteúdo na área de texto."""
  10.     try:
  11.         file_path = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt")])
  12.         if file_path:
  13.             with open(file_path, "r") as file:
  14.                 content = file.read()
  15.                 text_area.delete(1.0, tk.END)
  16.                 text_area.insert(tk.END, content)
  17.     except Exception as e:
  18.         messagebox.showerror("Erro", f"Não foi possível abrir o arquivo: {e}")
  19.  
  20. def save_file():
  21.     """Salva o conteúdo da área de texto em um arquivo de texto."""
  22.     try:
  23.         file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt")])
  24.         if file_path:
  25.             with open(file_path, "w") as file:
  26.                 content = text_area.get(1.0, tk.END)
  27.                 file.write(content)
  28.     except Exception as e:
  29.         messagebox.showerror("Erro", f"Não foi possível salvar o arquivo: {e}")
  30.  
  31. def show_about():
  32.     """Exibe uma mensagem de informação sobre o aplicativo."""
  33.     messagebox.showinfo("Sobre", "Bloco de Notas simples feito em Python. 2024, Mizuno")
  34.  
  35. def copy_text(event=None):
  36.     """Copia o texto selecionado."""
  37.     text_area.event_generate('<<Copy>>')
  38.  
  39. def paste_text(event=None):
  40.     """Cola o texto da área de transferência."""
  41.     text_area.event_generate('<<Paste>>')
  42.  
  43. def show_context_menu(event):
  44.     """Exibe o menu de contexto ao clicar com o botão direito."""
  45.     context_menu.tk_popup(event.x_root, event.y_root)
  46.  
  47. # Cria a janela principal
  48. root = tk.Tk()
  49. root.title("Bloco de Notas em Python")
  50.  
  51. # Define a resolução para 1366x768 e centraliza a janela na tela
  52. window_width = 1366
  53. window_height = 768
  54.  
  55. # Obtém a largura e altura da tela
  56. screen_width = root.winfo_screenwidth()
  57. screen_height = root.winfo_screenheight()
  58.  
  59. # Calcula a posição x e y para centralizar a janela
  60. position_x = (screen_width // 2) - (window_width // 2)
  61. position_y = (screen_height // 2) - (window_height // 2)
  62.  
  63. # Define a geometria da janela
  64. root.geometry(f"{window_width}x{window_height}+{position_x}+{position_y}")
  65.  
  66. # Cria a área de texto
  67. text_area = tk.Text(root, wrap='word', undo=True)
  68. text_area.pack(expand=True, fill='both')
  69.  
  70. # Cria a barra de menu
  71. menu_bar = Menu(root)
  72. root.config(menu=menu_bar)
  73.  
  74. # Adiciona os menus
  75. file_menu = Menu(menu_bar, tearoff=0)
  76. menu_bar.add_cascade(label="Arquivo", menu=file_menu)
  77. file_menu.add_command(label="Novo", command=new_file)
  78. file_menu.add_command(label="Abrir", command=open_file)
  79. file_menu.add_command(label="Salvar", command=save_file)
  80. file_menu.add_separator()
  81. file_menu.add_command(label="Sair", command=root.quit)
  82.  
  83. about_menu = Menu(menu_bar, tearoff=0)
  84. menu_bar.add_cascade(label="Sobre", menu=about_menu)
  85. about_menu.add_command(label="Sobre", command=show_about)
  86.  
  87. # Cria o menu de contexto
  88. context_menu = Menu(root, tearoff=0)
  89. context_menu.add_command(label="Copiar", command=copy_text)
  90. context_menu.add_command(label="Colar", command=paste_text)
  91.  
  92. # Vincula o menu de contexto ao clique direito do mouse
  93. text_area.bind("<Button-3>", show_context_menu)
  94.  
  95. # Inicia a aplicação
  96. root.mainloop()
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement