Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Código não funciona os botões Adicionar nem o botão Editar
- import tkinter as tk
- from tkinter import ttk, messagebox
- import os
- import pyperclip
- import webbrowser
- class Application(tk.Tk):
- def __init__(self):
- super().__init__()
- self.title("Gerenciador de Senhas - Filtre os dados por Site ou por Login")
- self.geometry("1000x600")
- self.sort_order_site = "asc"
- self.sort_order_login = "asc"
- self.create_widgets()
- self.create_menu()
- self.date_label = ttk.Label(self, text="Mizuno - 06/05/2024")
- self.date_label.place(x=10, y=self.winfo_height() - 30)
- self.center_window()
- def create_widgets(self):
- self.search_var = tk.StringVar()
- self.search_entry = ttk.Entry(self, textvariable=self.search_var)
- self.search_entry.pack(pady=(0, 10))
- self.search_button = ttk.Button(self, text="Pesquisar", command=self.search_records)
- self.search_button.pack(pady=(0, 10))
- self.tree = ttk.Treeview(self, columns=("Site", "URL", "Login", "Senha"), show="headings")
- self.tree.heading("Site", text="Site", command=self.sort_sites)
- self.tree.heading("URL", text="URL")
- self.tree.heading("Login", text="Login", command=self.sort_login)
- self.tree.heading("Senha", text="Senha")
- self.tree.pack(pady=10, side="left", fill="both", expand=True)
- scrollbar = ttk.Scrollbar(self, orient="vertical", command=self.tree.yview)
- scrollbar.pack(side="right", fill="y")
- self.tree.configure(yscrollcommand=scrollbar.set)
- self.refresh_button = ttk.Button(self, text="Atualizar", command=self.refresh_tree)
- self.refresh_button.pack()
- self.add_button = ttk.Button(self, text="Adicionar", command=self.add_entry)
- self.add_button.pack()
- self.edit_button = ttk.Button(self, text="Editar", command=self.edit_entry)
- self.edit_button.pack()
- self.delete_button = ttk.Button(self, text="Excluir", command=self.confirm_delete)
- self.delete_button.pack()
- self.load_data()
- self.tree.bind("<Button-3>", self.popup_menu)
- self.tree.bind("<Double-1>", self.open_url)
- def search_records(self):
- search_term = self.search_var.get().strip().lower()
- if not search_term:
- messagebox.showinfo("Pesquisa", "Digite um termo de pesquisa.")
- return
- # Limpa a TreeView antes de preencher com os resultados da pesquisa
- for item in self.tree.get_children():
- self.tree.delete(item)
- found = False
- if os.path.exists("dados.txt"):
- with open("dados.txt", "r") as f:
- for line in f:
- data = line.strip().split(",")
- # Verifica se o termo de pesquisa está em algum dos campos 'Site', 'URL' ou 'Login'
- if search_term in data[0].lower() or search_term in data[1].lower() or search_term in data[2].lower():
- self.tree.insert("", "end", values=data)
- found = True
- if not found:
- messagebox.showinfo("Pesquisa", "Nenhum registro correspondente encontrado.")
- self.refresh_tree() # Chama a função de atualização da árvore diretamente após fechar o alerta
- def create_menu(self):
- self.menu_bar = tk.Menu(self)
- self.file_menu = tk.Menu(self.menu_bar, tearoff=0)
- self.file_menu.add_command(label="Sobre", command=self.show_about_dialog)
- self.file_menu.add_separator()
- self.file_menu.add_command(label="Sair", command=self.quit_app)
- self.menu_bar.add_cascade(label="Arquivo", menu=self.file_menu)
- self.config(menu=self.menu_bar)
- def open_url(self, event):
- if self.tree.selection():
- item = self.tree.selection()[0]
- url = self.tree.item(item, "values")[1]
- webbrowser.open(url)
- def center_window(self):
- self.update_idletasks()
- width = self.winfo_width()
- height = self.winfo_height()
- x = (self.winfo_screenwidth() - width) // 2
- y = (self.winfo_screenheight() - height) // 2
- self.geometry('{}x{}+{}+{}'.format(width, height, x, y))
- def load_data(self):
- if os.path.exists("dados.txt"):
- with open("dados.txt", "r") as f:
- for line in f:
- data = line.strip().split(",")
- self.tree.insert("", "end", values=data)
- def save_data(self):
- with open("dados.txt", "w") as f:
- for item in self.tree.get_children():
- values = self.tree.item(item, "values")
- f.write(",".join(values) + "\n")
- def refresh_tree(self):
- for item in self.tree.get_children():
- self.tree.delete(item)
- self.load_data()
- def add_entry(self):
- AddEntryWindow(self)
- def edit_entry(self):
- try:
- item = self.tree.selection()[0]
- values = self.tree.item(item, "values")
- EditEntryWindow(self, values)
- except IndexError:
- messagebox.showerror("Erro", "Selecione um registro para editar.")
- def delete_entry(self):
- selected_item = self.tree.selection()[0]
- self.tree.delete(selected_item)
- self.save_data()
- def confirm_delete(self):
- try:
- if messagebox.askyesno("Confirmar", "Tem certeza que deseja excluir este registro?"):
- self.delete_entry()
- except IndexError:
- messagebox.showerror("Erro", "Selecione um registro para excluir.")
- def popup_menu(self, event):
- popup_menu = tk.Menu(self, tearoff=0)
- popup_menu.add_command(label="Copiar URL", command=lambda: self.copy_to_clipboard(1))
- popup_menu.add_command(label="Copiar Login", command=lambda: self.copy_to_clipboard(2))
- popup_menu.add_command(label="Copiar Senha", command=lambda: self.copy_to_clipboard(3))
- popup_menu.post(event.x_root, event.y_root)
- def copy_to_clipboard(self, column_index):
- item = self.tree.selection()[0]
- value = self.tree.item(item, "values")[column_index]
- pyperclip.copy(value)
- def sort_sites(self):
- items = self.tree.get_children("")
- if self.sort_order_site == "asc":
- sorted_items = sorted(items, key=lambda x: self.tree.item(x, "values")[0])
- self.sort_order_site = "desc"
- else:
- sorted_items = sorted(items, key=lambda x: self.tree.item(x, "values")[0], reverse=True)
- self.sort_order_site = "asc"
- for item in sorted_items:
- self.tree.move(item, "", "end")
- def sort_login(self):
- items = self.tree.get_children("")
- if self.sort_order_login == "asc":
- sorted_items = sorted(items, key=lambda x: self.tree.item(x, "values")[2])
- self.sort_order_login = "desc"
- else:
- sorted_items = sorted(items, key=lambda x: self.tree.item(x, "values")[2], reverse=True)
- self.sort_order_login = "asc"
- for item in sorted_items:
- self.tree.move(item, "", "end")
- def show_about_dialog(self):
- messagebox.showinfo("Sobre", "Última atualização: 16/08/2024, by Mizuno")
- def quit_app(self):
- self.destroy()
- # Continuação das classes AddEntryWindow e EditEntryWindow segue sem mudanças.
- def main():
- app = Application()
- app.mainloop()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement