Advertisement
MizunoBrasil

Gerenciador de Senhas 3

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