Advertisement
MizunoBrasil

Gerenciador de Senhas v. 5 (meia boca)

Aug 16th, 2024 (edited)
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.53 KB | None | 0 0
  1. # Código não funciona os botões Adicionar nem o botão Editar
  2.  
  3. import tkinter as tk
  4. from tkinter import ttk, messagebox
  5. import os
  6. import pyperclip
  7. import webbrowser
  8.  
  9. class Application(tk.Tk):
  10.     def __init__(self):
  11.         super().__init__()
  12.         self.title("Gerenciador de Senhas - Filtre os dados por Site ou por Login")
  13.         self.geometry("1000x600")
  14.         self.sort_order_site = "asc"
  15.         self.sort_order_login = "asc"
  16.         self.create_widgets()
  17.         self.create_menu()
  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.         self.center_window()
  21.  
  22.     def create_widgets(self):
  23.         self.search_var = tk.StringVar()
  24.         self.search_entry = ttk.Entry(self, textvariable=self.search_var)
  25.         self.search_entry.pack(pady=(0, 10))
  26.         self.search_button = ttk.Button(self, text="Pesquisar", command=self.search_records)
  27.         self.search_button.pack(pady=(0, 10))
  28.         self.tree = ttk.Treeview(self, columns=("Site", "URL", "Login", "Senha"), show="headings")
  29.         self.tree.heading("Site", text="Site", command=self.sort_sites)
  30.         self.tree.heading("URL", text="URL")
  31.         self.tree.heading("Login", text="Login", command=self.sort_login)
  32.         self.tree.heading("Senha", text="Senha")
  33.         self.tree.pack(pady=10, side="left", fill="both", expand=True)
  34.         scrollbar = ttk.Scrollbar(self, orient="vertical", command=self.tree.yview)
  35.         scrollbar.pack(side="right", fill="y")
  36.         self.tree.configure(yscrollcommand=scrollbar.set)
  37.         self.refresh_button = ttk.Button(self, text="Atualizar", command=self.refresh_tree)
  38.         self.refresh_button.pack()
  39.         self.add_button = ttk.Button(self, text="Adicionar", command=self.add_entry)
  40.         self.add_button.pack()
  41.         self.edit_button = ttk.Button(self, text="Editar", command=self.edit_entry)
  42.         self.edit_button.pack()
  43.         self.delete_button = ttk.Button(self, text="Excluir", command=self.confirm_delete)
  44.         self.delete_button.pack()
  45.         self.load_data()
  46.         self.tree.bind("<Button-3>", self.popup_menu)
  47.         self.tree.bind("<Double-1>", self.open_url)
  48.  
  49.     def search_records(self):
  50.         search_term = self.search_var.get().strip().lower()
  51.         if not search_term:
  52.             messagebox.showinfo("Pesquisa", "Digite um termo de pesquisa.")
  53.             return
  54.  
  55.         # Limpa a TreeView antes de preencher com os resultados da pesquisa
  56.         for item in self.tree.get_children():
  57.             self.tree.delete(item)
  58.  
  59.         found = False
  60.         if os.path.exists("dados.txt"):
  61.             with open("dados.txt", "r") as f:
  62.                 for line in f:
  63.                     data = line.strip().split(",")
  64.                     # Verifica se o termo de pesquisa está em algum dos campos 'Site', 'URL' ou 'Login'
  65.                     if search_term in data[0].lower() or search_term in data[1].lower() or search_term in data[2].lower():
  66.                         self.tree.insert("", "end", values=data)
  67.                         found = True
  68.  
  69.         if not found:
  70.             messagebox.showinfo("Pesquisa", "Nenhum registro correspondente encontrado.")
  71.             self.refresh_tree()  # Chama a função de atualização da árvore diretamente após fechar o alerta
  72.  
  73.     def create_menu(self):
  74.         self.menu_bar = tk.Menu(self)
  75.         self.file_menu = tk.Menu(self.menu_bar, tearoff=0)
  76.         self.file_menu.add_command(label="Sobre", command=self.show_about_dialog)
  77.         self.file_menu.add_separator()
  78.         self.file_menu.add_command(label="Sair", command=self.quit_app)
  79.         self.menu_bar.add_cascade(label="Arquivo", menu=self.file_menu)
  80.         self.config(menu=self.menu_bar)
  81.  
  82.     def open_url(self, event):
  83.         if self.tree.selection():
  84.             item = self.tree.selection()[0]
  85.             url = self.tree.item(item, "values")[1]
  86.             webbrowser.open(url)
  87.  
  88.     def center_window(self):
  89.         self.update_idletasks()
  90.         width = self.winfo_width()
  91.         height = self.winfo_height()
  92.         x = (self.winfo_screenwidth() - width) // 2
  93.         y = (self.winfo_screenheight() - height) // 2
  94.         self.geometry('{}x{}+{}+{}'.format(width, height, x, y))
  95.  
  96.     def load_data(self):
  97.         if os.path.exists("dados.txt"):
  98.             with open("dados.txt", "r") as f:
  99.                 for line in f:
  100.                     data = line.strip().split(",")
  101.                     self.tree.insert("", "end", values=data)
  102.  
  103.     def save_data(self):
  104.         with open("dados.txt", "w") as f:
  105.             for item in self.tree.get_children():
  106.                 values = self.tree.item(item, "values")
  107.                 f.write(",".join(values) + "\n")
  108.  
  109.     def refresh_tree(self):
  110.         for item in self.tree.get_children():
  111.             self.tree.delete(item)
  112.         self.load_data()
  113.  
  114.     def add_entry(self):
  115.         AddEntryWindow(self)
  116.  
  117.     def edit_entry(self):
  118.         try:
  119.             item = self.tree.selection()[0]
  120.             values = self.tree.item(item, "values")
  121.             EditEntryWindow(self, values)
  122.         except IndexError:
  123.             messagebox.showerror("Erro", "Selecione um registro para editar.")
  124.  
  125.     def delete_entry(self):
  126.         selected_item = self.tree.selection()[0]
  127.         self.tree.delete(selected_item)
  128.         self.save_data()
  129.  
  130.     def confirm_delete(self):
  131.         try:
  132.             if messagebox.askyesno("Confirmar", "Tem certeza que deseja excluir este registro?"):
  133.                 self.delete_entry()
  134.         except IndexError:
  135.             messagebox.showerror("Erro", "Selecione um registro para excluir.")
  136.  
  137.     def popup_menu(self, event):
  138.         popup_menu = tk.Menu(self, tearoff=0)
  139.         popup_menu.add_command(label="Copiar URL", command=lambda: self.copy_to_clipboard(1))
  140.         popup_menu.add_command(label="Copiar Login", command=lambda: self.copy_to_clipboard(2))
  141.         popup_menu.add_command(label="Copiar Senha", command=lambda: self.copy_to_clipboard(3))
  142.         popup_menu.post(event.x_root, event.y_root)
  143.  
  144.     def copy_to_clipboard(self, column_index):
  145.         item = self.tree.selection()[0]
  146.         value = self.tree.item(item, "values")[column_index]
  147.         pyperclip.copy(value)
  148.  
  149.     def sort_sites(self):
  150.         items = self.tree.get_children("")
  151.         if self.sort_order_site == "asc":
  152.             sorted_items = sorted(items, key=lambda x: self.tree.item(x, "values")[0])
  153.             self.sort_order_site = "desc"
  154.         else:
  155.             sorted_items = sorted(items, key=lambda x: self.tree.item(x, "values")[0], reverse=True)
  156.             self.sort_order_site = "asc"
  157.         for item in sorted_items:
  158.             self.tree.move(item, "", "end")
  159.  
  160.     def sort_login(self):
  161.         items = self.tree.get_children("")
  162.         if self.sort_order_login == "asc":
  163.             sorted_items = sorted(items, key=lambda x: self.tree.item(x, "values")[2])
  164.             self.sort_order_login = "desc"
  165.         else:
  166.             sorted_items = sorted(items, key=lambda x: self.tree.item(x, "values")[2], reverse=True)
  167.             self.sort_order_login = "asc"
  168.         for item in sorted_items:
  169.             self.tree.move(item, "", "end")
  170.  
  171.     def show_about_dialog(self):
  172.         messagebox.showinfo("Sobre", "Última atualização: 16/08/2024, by Mizuno")
  173.  
  174.     def quit_app(self):
  175.         self.destroy()
  176.  
  177. # Continuação das classes AddEntryWindow e EditEntryWindow segue sem mudanças.
  178.  
  179. def main():
  180.     app = Application()
  181.     app.mainloop()
  182.  
  183. if __name__ == "__main__":
  184.     main()
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement