Advertisement
MizunoBrasil

Gerenciador de Senhas 2

May 9th, 2024
1,007
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.12 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. class Application(tk.Tk):
  8.     def __init__(self):
  9.         super().__init__()
  10.  
  11.         self.title("Gerenciador de Senhas")
  12.         self.geometry("1000x600")
  13.  
  14.         self.sort_order_site = "asc"  # Variável de controle para rastrear o estado da ordenação para a coluna "Site"
  15.         self.sort_order_login = "asc"  # Variável de controle para rastrear o estado da ordenação para a coluna "Login"
  16.  
  17.         self.create_widgets()
  18.         self.create_menu()  # Adicionar o menu Arquivo
  19.  
  20.         # Adicionar rótulo com data no canto inferior esquerdo
  21.         self.date_label = ttk.Label(self, text="Mizuno - 06/05/2024")
  22.         self.date_label.place(x=10, y=self.winfo_height() - 30)
  23.  
  24.         # Centralizar janela principal
  25.         self.center_window()
  26.  
  27.     def create_widgets(self):
  28.         self.tree = ttk.Treeview(self, columns=("Site", "URL", "Login", "Senha"), show="headings")
  29.         self.tree.heading("Site", text="Site", command=self.sort_sites)  # Vinculando a função de ordenação para a coluna "Site"
  30.         self.tree.heading("URL", text="URL")
  31.         self.tree.heading("Login", text="Login", command=self.sort_login)  # Vinculando a função de ordenação para a coluna "Login"
  32.         self.tree.heading("Senha", text="Senha")
  33.         self.tree.pack(pady=10)
  34.  
  35.         self.refresh_button = ttk.Button(self, text="Atualizar", command=self.refresh_tree)
  36.         self.refresh_button.pack()
  37.  
  38.         self.add_button = ttk.Button(self, text="Adicionar", command=self.add_entry)
  39.         self.add_button.pack()
  40.  
  41.         self.edit_button = ttk.Button(self, text="Editar", command=self.edit_entry)
  42.         self.edit_button.pack()
  43.  
  44.         self.delete_button = ttk.Button(self, text="Excluir", command=self.confirm_delete)
  45.         self.delete_button.pack()
  46.  
  47.         # Carregar dados do arquivo ao iniciar
  48.         self.load_data()
  49.  
  50.         # Adicionar menu de contexto à árvore
  51.         self.tree.bind("<Button-3>", self.popup_menu)
  52.  
  53.     def create_menu(self):
  54.         # Criar menu "Arquivo"
  55.         self.menu_bar = tk.Menu(self)
  56.         self.file_menu = tk.Menu(self.menu_bar, tearoff=0)
  57.         self.file_menu.add_command(label="Sobre", command=self.show_about_dialog)  # Adicionar a opção "Sobre"
  58.         self.file_menu.add_separator()
  59.         self.file_menu.add_command(label="Sair", command=self.quit_app)  # Adicionar a opção "Sair"
  60.         self.menu_bar.add_cascade(label="Arquivo", menu=self.file_menu)
  61.         self.config(menu=self.menu_bar)
  62.  
  63.     def center_window(self):
  64.         self.update_idletasks()
  65.         width = self.winfo_width()
  66.         height = self.winfo_height()
  67.         x = (self.winfo_screenwidth() - width) // 2
  68.         y = (self.winfo_screenheight() - height) // 2
  69.         self.geometry('{}x{}+{}+{}'.format(width, height, x, y))
  70.  
  71.     def load_data(self):
  72.         if os.path.exists("dados.txt"):
  73.             with open("dados.txt", "r") as f:
  74.                 for line in f:
  75.                     data = line.strip().split(",")
  76.                     self.tree.insert("", "end", values=data)
  77.  
  78.     def save_data(self):
  79.         with open("dados.txt", "w") as f:
  80.             for item in self.tree.get_children():
  81.                 values = self.tree.item(item, "values")
  82.                 f.write(",".join(values) + "\n")
  83.  
  84.     def refresh_tree(self):
  85.         for item in self.tree.get_children():
  86.             self.tree.delete(item)
  87.         self.load_data()
  88.  
  89.     def add_entry(self):
  90.         AddEntryWindow(self)
  91.  
  92.     def edit_entry(self):
  93.         try:
  94.             item = self.tree.selection()[0]
  95.             values = self.tree.item(item, "values")
  96.             EditEntryWindow(self, values)
  97.         except IndexError:
  98.             messagebox.showerror("Erro", "Selecione um registro para editar.")
  99.  
  100.     def delete_entry(self):
  101.         selected_item = self.tree.selection()[0]
  102.         self.tree.delete(selected_item)
  103.         self.save_data()
  104.  
  105.     def confirm_delete(self):
  106.         try:
  107.             if messagebox.askyesno("Confirmar", "Tem certeza que deseja excluir este registro?"):
  108.                 self.delete_entry()
  109.         except IndexError:
  110.             messagebox.showerror("Erro", "Selecione um registro para excluir.")
  111.  
  112.     def popup_menu(self, event):
  113.         popup_menu = tk.Menu(self, tearoff=0)
  114.         popup_menu.add_command(label="Copiar URL", command=lambda: self.copy_to_clipboard(1))
  115.         popup_menu.add_command(label="Copiar Login", command=lambda: self.copy_to_clipboard(2))
  116.         popup_menu.add_command(label="Copiar Senha", command=lambda: self.copy_to_clipboard(3))
  117.         popup_menu.post(event.x_root, event.y_root)
  118.  
  119.     def copy_to_clipboard(self, column_index):
  120.         item = self.tree.selection()[0]
  121.         value = self.tree.item(item, "values")[column_index]
  122.         pyperclip.copy(value)
  123.    
  124.     def sort_sites(self):
  125.         # Função para classificar os sites em ordem alfabética ascendente ou descendente
  126.         items = self.tree.get_children("")
  127.         if self.sort_order_site == "asc":
  128.             sorted_items = sorted(items, key=lambda x: self.tree.item(x, "values")[0])
  129.             self.sort_order_site = "desc"
  130.         else:
  131.             sorted_items = sorted(items, key=lambda x: self.tree.item(x, "values")[0], reverse=True)
  132.             self.sort_order_site = "asc"
  133.         for item in sorted_items:
  134.             self.tree.move(item, "", "end")
  135.  
  136.     def sort_login(self):
  137.         # Função para classificar os logins em ordem alfabética ascendente ou descendente
  138.         items = self.tree.get_children("")
  139.         if self.sort_order_login == "asc":
  140.             sorted_items = sorted(items, key=lambda x: self.tree.item(x, "values")[2])
  141.             self.sort_order_login = "desc"
  142.         else:
  143.             sorted_items = sorted(items, key=lambda x: self.tree.item(x, "values")[2], reverse=True)
  144.             self.sort_order_login = "asc"
  145.         for item in sorted_items:
  146.             self.tree.move(item, "", "end")
  147.  
  148.     def show_about_dialog(self):
  149.         messagebox.showinfo("Sobre", "Filtre os dados por Site ou por Login")
  150.  
  151.     def quit_app(self):
  152.         self.destroy()
  153.  
  154.  
  155. class AddEntryWindow(tk.Toplevel):
  156.     def __init__(self, master):
  157.         super().__init__(master)
  158.  
  159.         self.title("Adicionar Registro")
  160.         self.geometry("300x250")
  161.  
  162.         self.site_label = ttk.Label(self, text="Site:")
  163.         self.site_label.pack()
  164.         self.site_entry = ttk.Entry(self)
  165.         self.site_entry.pack()
  166.  
  167.         self.url_label = ttk.Label(self, text="URL:")
  168.         self.url_label.pack()
  169.         self.url_entry = ttk.Entry(self)
  170.         self.url_entry.pack()
  171.  
  172.         self.login_label = ttk.Label(self, text="Login:")
  173.         self.login_label.pack()
  174.         self.login_entry = ttk.Entry(self)
  175.         self.login_entry.pack()
  176.  
  177.         self.password_label = ttk.Label(self, text="Senha:")
  178.         self.password_label.pack()
  179.         self.password_entry = ttk.Entry(self, show="*")
  180.         self.password_entry.pack()
  181.  
  182.         self.add_button = ttk.Button(self, text="Adicionar", command=self.add_entry)
  183.         self.add_button.pack()
  184.  
  185.         # Centralizar janela de adicionar registro
  186.         self.center_window()
  187.  
  188.     def center_window(self):
  189.         self.update_idletasks()
  190.         width = self.winfo_width()
  191.         height = self.winfo_height()
  192.         x = (self.winfo_screenwidth() - width) // 2
  193.         y = (self.winfo_screenheight() - height) // 2
  194.         self.geometry('{}x{}+{}+{}'.format(width, height, x, y))
  195.  
  196.     def add_entry(self):
  197.         site = self.site_entry.get()
  198.         url = self.url_entry.get()
  199.         login = self.login_entry.get()
  200.         password = self.password_entry.get()
  201.  
  202.         if site and url and login and password:
  203.             self.master.tree.insert("", "end", values=(site, url, login, password))
  204.             self.master.save_data()
  205.             self.destroy()
  206.         else:
  207.             messagebox.showerror("Erro", "Preencha todos os campos.")
  208.  
  209.  
  210. class EditEntryWindow(AddEntryWindow):
  211.     def __init__(self, master, values):
  212.         super().__init__(master)
  213.  
  214.         self.title("Editar Registro")
  215.  
  216.         self.site_entry.insert(0, values[0])
  217.         self.url_entry.insert(0, values[1])
  218.         self.login_entry.insert(0, values[2])
  219.         self.password_entry.insert(0, values[3])
  220.  
  221.         self.add_button.config(text="Salvar")
  222.         self.add_button.config(command=self.save_entry)
  223.  
  224.         self.values = values
  225.  
  226.         # Centralizar janela de editar registro
  227.         self.center_window()
  228.  
  229.     def save_entry(self):
  230.         site = self.site_entry.get()
  231.         url = self.url_entry.get()
  232.         login = self.login_entry.get()
  233.         password = self.password_entry.get()
  234.  
  235.         if site and url and login and password:
  236.             self.master.tree.item(self.master.tree.selection(), values=(site, url, login, password))
  237.             self.master.save_data()
  238.             self.destroy()
  239.         else:
  240.             messagebox.showerror("Erro", "Preencha todos os campos.")
  241.  
  242.  
  243. def main():
  244.     app = Application()
  245.     app.mainloop()
  246.  
  247.  
  248. if __name__ == "__main__":
  249.     main()
  250.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement