Advertisement
MizunoBrasil

Gerenciador de Senhas

May 5th, 2024
1,042
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.84 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk, messagebox
  3. import threading
  4. import os
  5. import pyperclip
  6.  
  7.  
  8. class Application(tk.Tk):
  9.     def __init__(self):
  10.         super().__init__()
  11.  
  12.         self.title("Gerenciador de Senhas")
  13.         self.geometry("1000x600")
  14.  
  15.         self.create_widgets()
  16.  
  17.         # Adicionar rótulo com data no canto inferior esquerdo
  18.         self.date_label = ttk.Label(self, text="Mizuno - 06/05/2024")
  19.         self.date_label.place(x=10, y=self.winfo_height() - 30)
  20.  
  21.     def create_widgets(self):
  22.         self.tree = ttk.Treeview(self, columns=("Site", "URL", "Login", "Senha"), show="headings")
  23.         self.tree.heading("Site", text="Site")
  24.         self.tree.heading("URL", text="URL")
  25.         self.tree.heading("Login", text="Login")
  26.         self.tree.heading("Senha", text="Senha")
  27.         self.tree.pack(pady=10)
  28.  
  29.         self.refresh_button = ttk.Button(self, text="Atualizar", command=self.refresh_tree)
  30.         self.refresh_button.pack()
  31.  
  32.         self.add_button = ttk.Button(self, text="Adicionar", command=self.add_entry)
  33.         self.add_button.pack()
  34.  
  35.         self.edit_button = ttk.Button(self, text="Editar", command=self.edit_entry)
  36.         self.edit_button.pack()
  37.  
  38.         self.delete_button = ttk.Button(self, text="Excluir", command=self.confirm_delete)
  39.         self.delete_button.pack()
  40.  
  41.         # Carregar dados do arquivo ao iniciar
  42.         self.load_data()
  43.  
  44.         # Adicionar menu de contexto à árvore
  45.         self.tree.bind("<Button-3>", self.popup_menu)
  46.  
  47.         # Centralizar janela principal
  48.         self.center_window()
  49.  
  50.     def center_window(self):
  51.         self.update_idletasks()
  52.         width = self.winfo_width()
  53.         height = self.winfo_height()
  54.         x = (self.winfo_screenwidth() - width) // 2
  55.         y = (self.winfo_screenheight() - height) // 2
  56.         self.geometry('{}x{}+{}+{}'.format(width, height, x, y))
  57.  
  58.     def load_data(self):
  59.         if os.path.exists("dados.txt"):
  60.             with open("dados.txt", "r") as f:
  61.                 for line in f:
  62.                     data = line.strip().split(",")
  63.                     self.tree.insert("", "end", values=data)
  64.  
  65.     def save_data(self):
  66.         with open("dados.txt", "w") as f:
  67.             for item in self.tree.get_children():
  68.                 values = self.tree.item(item, "values")
  69.                 f.write(",".join(values) + "\n")
  70.  
  71.     def refresh_tree(self):
  72.         for item in self.tree.get_children():
  73.             self.tree.delete(item)
  74.         self.load_data()
  75.  
  76.     def add_entry(self):
  77.         AddEntryWindow(self)
  78.  
  79.     def edit_entry(self):
  80.         try:
  81.             item = self.tree.selection()[0]
  82.             values = self.tree.item(item, "values")
  83.             EditEntryWindow(self, values)
  84.         except IndexError:
  85.             messagebox.showerror("Erro", "Selecione um registro para editar.")
  86.  
  87.     def delete_entry(self):
  88.         selected_item = self.tree.selection()[0]
  89.         self.tree.delete(selected_item)
  90.         self.save_data()
  91.  
  92.     def confirm_delete(self):
  93.         try:
  94.             if messagebox.askyesno("Confirmar", "Tem certeza que deseja excluir este registro?"):
  95.                 self.delete_entry()
  96.         except IndexError:
  97.             messagebox.showerror("Erro", "Selecione um registro para excluir.")
  98.  
  99.     def popup_menu(self, event):
  100.         popup_menu = tk.Menu(self, tearoff=0)
  101.         popup_menu.add_command(label="Copiar URL", command=lambda: self.copy_to_clipboard(1))
  102.         popup_menu.add_command(label="Copiar Login", command=lambda: self.copy_to_clipboard(2))
  103.         popup_menu.add_command(label="Copiar Senha", command=lambda: self.copy_to_clipboard(3))
  104.         popup_menu.post(event.x_root, event.y_root)
  105.  
  106.     def copy_to_clipboard(self, column_index):
  107.         item = self.tree.selection()[0]
  108.         value = self.tree.item(item, "values")[column_index]
  109.         pyperclip.copy(value)
  110.  
  111.  
  112. class AddEntryWindow(tk.Toplevel):
  113.     def __init__(self, master):
  114.         super().__init__(master)
  115.  
  116.         self.title("Adicionar Registro")
  117.         self.geometry("300x250")
  118.  
  119.         self.site_label = ttk.Label(self, text="Site:")
  120.         self.site_label.pack()
  121.         self.site_entry = ttk.Entry(self)
  122.         self.site_entry.pack()
  123.  
  124.         self.url_label = ttk.Label(self, text="URL:")
  125.         self.url_label.pack()
  126.         self.url_entry = ttk.Entry(self)
  127.         self.url_entry.pack()
  128.  
  129.         self.login_label = ttk.Label(self, text="Login:")
  130.         self.login_label.pack()
  131.         self.login_entry = ttk.Entry(self)
  132.         self.login_entry.pack()
  133.  
  134.         self.password_label = ttk.Label(self, text="Senha:")
  135.         self.password_label.pack()
  136.         self.password_entry = ttk.Entry(self, show="*")
  137.         self.password_entry.pack()
  138.  
  139.         self.add_button = ttk.Button(self, text="Adicionar", command=self.add_entry)
  140.         self.add_button.pack()
  141.  
  142.         # Centralizar janela de adicionar registro
  143.         self.center_window()
  144.  
  145.     def center_window(self):
  146.         self.update_idletasks()
  147.         width = self.winfo_width()
  148.         height = self.winfo_height()
  149.         x = (self.winfo_screenwidth() - width) // 2
  150.         y = (self.winfo_screenheight() - height) // 2
  151.         self.geometry('{}x{}+{}+{}'.format(width, height, x, y))
  152.  
  153.     def add_entry(self):
  154.         site = self.site_entry.get()
  155.         url = self.url_entry.get()
  156.         login = self.login_entry.get()
  157.         password = self.password_entry.get()
  158.  
  159.         if site and url and login and password:
  160.             self.master.tree.insert("", "end", values=(site, url, login, password))
  161.             self.master.save_data()
  162.             self.destroy()
  163.         else:
  164.             messagebox.showerror("Erro", "Preencha todos os campos.")
  165.  
  166.  
  167. class EditEntryWindow(AddEntryWindow):
  168.     def __init__(self, master, values):
  169.         super().__init__(master)
  170.  
  171.         self.title("Editar Registro")
  172.  
  173.         self.site_entry.insert(0, values[0])
  174.         self.url_entry.insert(0, values[1])
  175.         self.login_entry.insert(0, values[2])
  176.         self.password_entry.insert(0, values[3])
  177.  
  178.         self.add_button.config(text="Salvar")
  179.         self.add_button.config(command=self.save_entry)
  180.  
  181.         self.values = values
  182.  
  183.         # Centralizar janela de editar registro
  184.         self.center_window()
  185.  
  186.     def save_entry(self):
  187.         site = self.site_entry.get()
  188.         url = self.url_entry.get()
  189.         login = self.login_entry.get()
  190.         password = self.password_entry.get()
  191.  
  192.         if site and url and login and password:
  193.             self.master.tree.item(self.master.tree.selection(), values=(site, url, login, password))
  194.             self.master.save_data()
  195.             self.destroy()
  196.         else:
  197.             messagebox.showerror("Erro", "Preencha todos os campos.")
  198.  
  199.  
  200. def main():
  201.     app = Application()
  202.     app.mainloop()
  203.  
  204.  
  205. if __name__ == "__main__":
  206.     main()
  207.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement