Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import ttk, messagebox
- import threading
- import os
- import pyperclip
- class Application(tk.Tk):
- def __init__(self):
- super().__init__()
- self.title("Gerenciador de Senhas")
- self.geometry("1000x600")
- self.sort_order_site = "asc" # Variável de controle para rastrear o estado da ordenação para a coluna "Site"
- self.sort_order_login = "asc" # Variável de controle para rastrear o estado da ordenação para a coluna "Login"
- self.create_widgets()
- self.create_menu() # Adicionar o menu Arquivo
- # Adicionar rótulo com data no canto inferior esquerdo
- self.date_label = ttk.Label(self, text="Mizuno - 06/05/2024")
- self.date_label.place(x=10, y=self.winfo_height() - 30)
- # Centralizar janela principal
- self.center_window()
- def create_widgets(self):
- self.tree = ttk.Treeview(self, columns=("Site", "URL", "Login", "Senha"), show="headings")
- self.tree.heading("Site", text="Site", command=self.sort_sites) # Vinculando a função de ordenação para a coluna "Site"
- self.tree.heading("URL", text="URL")
- self.tree.heading("Login", text="Login", command=self.sort_login) # Vinculando a função de ordenação para a coluna "Login"
- self.tree.heading("Senha", text="Senha")
- self.tree.pack(pady=10)
- 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()
- # Carregar dados do arquivo ao iniciar
- self.load_data()
- # Adicionar menu de contexto à árvore
- self.tree.bind("<Button-3>", self.popup_menu)
- def create_menu(self):
- # Criar menu "Arquivo"
- 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) # Adicionar a opção "Sobre"
- self.file_menu.add_separator()
- self.file_menu.add_command(label="Sair", command=self.quit_app) # Adicionar a opção "Sair"
- self.menu_bar.add_cascade(label="Arquivo", menu=self.file_menu)
- self.config(menu=self.menu_bar)
- 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):
- # Função para classificar os sites em ordem alfabética ascendente ou descendente
- 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):
- # Função para classificar os logins em ordem alfabética ascendente ou descendente
- 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", "Filtre os dados por Site ou por Login")
- def quit_app(self):
- self.destroy()
- class AddEntryWindow(tk.Toplevel):
- def __init__(self, master):
- super().__init__(master)
- self.title("Adicionar Registro")
- self.geometry("300x250")
- self.site_label = ttk.Label(self, text="Site:")
- self.site_label.pack()
- self.site_entry = ttk.Entry(self)
- self.site_entry.pack()
- self.url_label = ttk.Label(self, text="URL:")
- self.url_label.pack()
- self.url_entry = ttk.Entry(self)
- self.url_entry.pack()
- self.login_label = ttk.Label(self, text="Login:")
- self.login_label.pack()
- self.login_entry = ttk.Entry(self)
- self.login_entry.pack()
- self.password_label = ttk.Label(self, text="Senha:")
- self.password_label.pack()
- self.password_entry = ttk.Entry(self, show="*")
- self.password_entry.pack()
- self.add_button = ttk.Button(self, text="Adicionar", command=self.add_entry)
- self.add_button.pack()
- # Centralizar janela de adicionar registro
- self.center_window()
- 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 add_entry(self):
- site = self.site_entry.get()
- url = self.url_entry.get()
- login = self.login_entry.get()
- password = self.password_entry.get()
- if site and url and login and password:
- self.master.tree.insert("", "end", values=(site, url, login, password))
- self.master.save_data()
- self.destroy()
- else:
- messagebox.showerror("Erro", "Preencha todos os campos.")
- class EditEntryWindow(AddEntryWindow):
- def __init__(self, master, values):
- super().__init__(master)
- self.title("Editar Registro")
- self.site_entry.insert(0, values[0])
- self.url_entry.insert(0, values[1])
- self.login_entry.insert(0, values[2])
- self.password_entry.insert(0, values[3])
- self.add_button.config(text="Salvar")
- self.add_button.config(command=self.save_entry)
- self.values = values
- # Centralizar janela de editar registro
- self.center_window()
- def save_entry(self):
- site = self.site_entry.get()
- url = self.url_entry.get()
- login = self.login_entry.get()
- password = self.password_entry.get()
- if site and url and login and password:
- self.master.tree.item(self.master.tree.selection(), values=(site, url, login, password))
- self.master.save_data()
- self.destroy()
- else:
- messagebox.showerror("Erro", "Preencha todos os campos.")
- def main():
- app = Application()
- app.mainloop()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement