Advertisement
MizunoBrasil

Gerenciador de Senhas v. 5.1 (Não Resolvido)

Aug 16th, 2024 (edited)
1,181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.80 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk, messagebox
  3. import os
  4. import pyperclip
  5. import webbrowser
  6.  
  7. class Application(tk.Tk):
  8.     def __init__(self):
  9.         super().__init__()
  10.         self.title("Gerenciador de Senhas - Filtre os dados por Site ou por Login")
  11.         self.geometry("1000x600")
  12.         self.sort_order_site = "asc"
  13.         self.sort_order_login = "asc"
  14.         self.create_widgets()
  15.         self.create_menu()
  16.         self.date_label = ttk.Label(self, text="Mizuno - 06/05/2024")
  17.         self.date_label.place(x=10, y=self.winfo_height() - 30)
  18.         self.center_window()
  19.  
  20.     def create_widgets(self):
  21.         self.search_var = tk.StringVar()
  22.         self.search_entry = ttk.Entry(self, textvariable=self.search_var)
  23.         self.search_entry.pack(pady=(0, 10))
  24.         self.search_button = ttk.Button(self, text="Pesquisar", command=self.search_records)
  25.         self.search_button.pack(pady=(0, 10))
  26.         self.tree = ttk.Treeview(self, columns=("Site", "URL", "Login", "Senha"), show="headings")
  27.         self.tree.heading("Site", text="Site", command=self.sort_sites)
  28.         self.tree.heading("URL", text="URL")
  29.         self.tree.heading("Login", text="Login", command=self.sort_login)
  30.         self.tree.heading("Senha", text="Senha")
  31.         self.tree.pack(pady=10, side="left", fill="both", expand=True)
  32.         scrollbar = ttk.Scrollbar(self, orient="vertical", command=self.tree.yview)
  33.         scrollbar.pack(side="right", fill="y")
  34.         self.tree.configure(yscrollcommand=scrollbar.set)
  35.         self.refresh_button = ttk.Button(self, text="Atualizar", command=self.refresh_tree)
  36.         self.refresh_button.pack()
  37.         self.add_button = ttk.Button(self, text="Adicionar", command=self.add_entry)
  38.         self.add_button.pack()
  39.         self.edit_button = ttk.Button(self, text="Editar", command=self.edit_entry)
  40.         self.edit_button.pack()
  41.         self.delete_button = ttk.Button(self, text="Excluir", command=self.confirm_delete)
  42.         self.delete_button.pack()
  43.         self.load_data()
  44.         self.tree.bind("<Button-3>", self.popup_menu)
  45.         self.tree.bind("<Double-1>", self.open_url)
  46.  
  47.     def search_records(self):
  48.         search_term = self.search_var.get().strip().lower()
  49.         if not search_term:
  50.             messagebox.showinfo("Pesquisa", "Digite um termo de pesquisa.")
  51.             return
  52.  
  53.         # Limpa a TreeView antes de preencher com os resultados da pesquisa
  54.         for item in self.tree.get_children():
  55.             self.tree.delete(item)
  56.  
  57.         found = False
  58.         if os.path.exists("dados.txt"):
  59.             with open("dados.txt", "r") as f:
  60.                 for line in f:
  61.                     data = line.strip().split(",")
  62.                     # Verifica se o termo de pesquisa está em algum dos campos 'Site', 'URL' ou 'Login'
  63.                     if search_term in data[0].lower() or search_term in data[1].lower() or search_term in data[2].lower():
  64.                         self.tree.insert("", "end", values=data)
  65.                         found = True
  66.  
  67.         if not found:
  68.             messagebox.showinfo("Pesquisa", "Nenhum registro correspondente encontrado.")
  69.             self.refresh_tree()  # Chama a função de atualização da árvore diretamente após fechar o alerta
  70.  
  71.     def create_menu(self):
  72.         self.menu_bar = tk.Menu(self)
  73.         self.file_menu = tk.Menu(self.menu_bar, tearoff=0)
  74.         self.file_menu.add_command(label="Sobre", command=self.show_about_dialog)
  75.         self.file_menu.add_separator()
  76.         self.file_menu.add_command(label="Sair", command=self.quit_app)
  77.         self.menu_bar.add_cascade(label="Arquivo", menu=self.file_menu)
  78.         self.config(menu=self.menu_bar)
  79.  
  80.     def open_url(self, event):
  81.         if self.tree.selection():
  82.             item = self.tree.selection()[0]
  83.             url = self.tree.item(item, "values")[1]
  84.             webbrowser.open(url)
  85.  
  86.     def center_window(self):
  87.         self.update_idletasks()
  88.         width = self.winfo_width()
  89.         height = self.winfo_height()
  90.         x = (self.winfo_screenwidth() - width) // 2
  91.         y = (self.winfo_screenheight() - height) // 2
  92.         self.geometry('{}x{}+{}+{}'.format(width, height, x, y))
  93.  
  94.     def load_data(self):
  95.         if os.path.exists("dados.txt"):
  96.             with open("dados.txt", "r") as f:
  97.                 for line in f:
  98.                     data = line.strip().split(",")
  99.                     self.tree.insert("", "end", values=data)
  100.  
  101.     def save_data(self):
  102.         with open("dados.txt", "w") as f:
  103.             for item in self.tree.get_children():
  104.                 values = self.tree.item(item, "values")
  105.                 f.write(",".join(values) + "\n")
  106.  
  107.     def refresh_tree(self):
  108.         for item in self.tree.get_children():
  109.             self.tree.delete(item)
  110.         self.load_data()
  111.  
  112.     def add_entry(self):
  113.         AddEntryWindow(self)
  114.  
  115.     def edit_entry(self):
  116.         try:
  117.             item = self.tree.selection()[0]
  118.             values = self.tree.item(item, "values")
  119.             EditEntryWindow(self, values)
  120.         except IndexError:
  121.             messagebox.showerror("Erro", "Selecione um registro para editar.")
  122.  
  123.     def delete_entry(self):
  124.         selected_item = self.tree.selection()[0]
  125.         self.tree.delete(selected_item)
  126.         self.save_data()
  127.  
  128.     def confirm_delete(self):
  129.         try:
  130.             if messagebox.askyesno("Confirmar", "Tem certeza que deseja excluir este registro?"):
  131.                 self.delete_entry()
  132.         except IndexError:
  133.             messagebox.showerror("Erro", "Selecione um registro para excluir.")
  134.  
  135.     def popup_menu(self, event):
  136.         popup_menu = tk.Menu(self, tearoff=0)
  137.         popup_menu.add_command(label="Copiar URL", command=lambda: self.copy_to_clipboard(1))
  138.         popup_menu.add_command(label="Copiar Login", command=lambda: self.copy_to_clipboard(2))
  139.         popup_menu.add_command(label="Copiar Senha", command=lambda: self.copy_to_clipboard(3))
  140.         popup_menu.post(event.x_root, event.y_root)
  141.  
  142.     def copy_to_clipboard(self, column_index):
  143.         item = self.tree.selection()[0]
  144.         value = self.tree.item(item, "values")[column_index]
  145.         pyperclip.copy(value)
  146.  
  147.     def sort_sites(self):
  148.         items = self.tree.get_children("")
  149.         if self.sort_order_site == "asc":
  150.             sorted_items = sorted(items, key=lambda x: self.tree.item(x, "values")[0])
  151.             self.sort_order_site = "desc"
  152.         else:
  153.             sorted_items = sorted(items, key=lambda x: self.tree.item(x, "values")[0], reverse=True)
  154.             self.sort_order_site = "asc"
  155.         for item in sorted_items:
  156.             self.tree.move(item, "", "end")
  157.  
  158.     def sort_login(self):
  159.         items = self.tree.get_children("")
  160.         if self.sort_order_login == "asc":
  161.             sorted_items = sorted(items, key=lambda x: self.tree.item(x, "values")[2])
  162.             self.sort_order_login = "desc"
  163.         else:
  164.             sorted_items = sorted(items, key=lambda x: self.tree.item(x, "values")[2], reverse=True)
  165.             self.sort_order_login = "asc"
  166.         for item in sorted_items:
  167.             self.tree.move(item, "", "end")
  168.  
  169.     def show_about_dialog(self):
  170.         messagebox.showinfo("Sobre", "Última atualização: 17/08/2024, by Mizuno. Versão 5.1")
  171.  
  172.     def quit_app(self):
  173.         self.destroy()
  174.  
  175. class AddEntryWindow(tk.Toplevel):
  176.     def __init__(self, parent):
  177.         super().__init__(parent)
  178.         self.title("Adicionar Entrada")
  179.         self.geometry("400x300")
  180.         self.create_widgets()
  181.  
  182.     def create_widgets(self):
  183.         self.site_label = ttk.Label(self, text="Site:")
  184.         self.site_label.pack(pady=5)
  185.         self.site_entry = ttk.Entry(self)
  186.         self.site_entry.pack(pady=5)
  187.  
  188.         self.url_label = ttk.Label(self, text="URL:")
  189.         self.url_label.pack(pady=5)
  190.         self.url_entry = ttk.Entry(self)
  191.         self.url_entry.pack(pady=5)
  192.  
  193.         self.login_label = ttk.Label(self, text="Login:")
  194.         self.login_label.pack(pady=5)
  195.         self.login_entry = ttk.Entry(self)
  196.         self.login_entry.pack(pady=5)
  197.  
  198.         self.senha_label = ttk.Label(self, text="Senha:")
  199.         self.senha_label.pack(pady=5)
  200.         self.senha_entry = ttk.Entry(self)
  201.         self.senha_entry.pack(pady=5)
  202.  
  203.         self.save_button = ttk.Button(self, text="Salvar", command=self.save_entry)
  204.         self.save_button.pack(pady=20)
  205.  
  206.     def save_entry(self):
  207.         site = self.site_entry.get().strip()
  208.         url = self.url_entry.get().strip()
  209.         login = self.login_entry.get().strip()
  210.         senha = self.senha_entry.get().strip()
  211.         if site and url and login and senha:
  212.             self.master.tree.insert("", "end", values=(site, url, login, senha))
  213.             self.master.save_data()
  214.             self.destroy()
  215.         else:
  216.             messagebox.showerror("Erro", "Todos os campos devem ser preenchidos.")
  217.  
  218. class EditEntryWindow(tk.Toplevel):
  219.     def __init__(self, parent, values):
  220.         super().__init__(parent)
  221.         self.title("Editar Entrada")
  222.         self.geometry("400x300")
  223.         self.values = values
  224.         self.create_widgets()
  225.  
  226.     def create_widgets(self):
  227.         self.site_label = ttk.Label(self, text="Site:")
  228.         self.site_label.pack(pady=5)
  229.         self.site_entry = ttk.Entry(self)
  230.         self.site_entry.insert(0, self.values[0])
  231.         self.site_entry.pack(pady=5)
  232.  
  233.         self.url_label = ttk.Label(self, text="URL:")
  234.         self.url_label.pack(pady=5)
  235.         self.url_entry = ttk.Entry(self)
  236.         self.url_entry.insert(0, self.values[1])
  237.         self.url_entry.pack(pady=5)
  238.  
  239.         self.login_label = ttk.Label(self, text="Login:")
  240.         self.login_label.pack(pady=5)
  241.         self.login_entry = ttk.Entry(self)
  242.         self.login_entry.insert(0, self.values[2])
  243.         self.login_entry.pack(pady=5)
  244.  
  245.         self.senha_label = ttk.Label(self, text="Senha:")
  246.         self.senha_label.pack(pady=5)
  247.         self.senha_entry = ttk.Entry(self)
  248.         self.senha_entry.insert(0, self.values[3])
  249.         self.senha_entry.pack(pady=5)
  250.  
  251.         self.save_button = ttk.Button(self, text="Salvar", command=self.save_entry)
  252.         self.save_button.pack(pady=20)
  253.  
  254.     def save_entry(self):
  255.         site = self.site_entry.get().strip()
  256.         url = self.url_entry.get().strip()
  257.         login = self.login_entry.get().strip()
  258.         senha = self.senha_entry.get().strip()
  259.         if site and url and login and senha:
  260.             selected_item = self.master.tree.selection()[0]
  261.             self.master.tree.item(selected_item, values=(site, url, login, senha))
  262.             self.master.save_data()
  263.             self.destroy()
  264.         else:
  265.             messagebox.showerror("Erro", "Todos os campos devem ser preenchidos.")
  266.  
  267. def main():
  268.     app = Application()
  269.     app.mainloop()
  270.  
  271. if __name__ == "__main__":
  272.     main()
  273.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement