Storico degli appunti copiati (CTRL+C)
Categoria: TkinterScrivere uno script in grado di tenere traccia di ogni elemento copiato nella clipboard(CTRL+C o copia).
Aiuto
Per interagire con la clipboard puoi utilizzare questa libreria: pyperclip.
Condizioni:
- Implementare un interfaccia grafica(es. tkinter);
- Salvare ogni testo copiato in un database;
- Visualizzare sull'interfaccia grafica tutto lo storico degli appunti copiati, con tanto di orario;
- Possibilità di eliminare i contenuti visualizzati.
Soluzione
import datetime as dt import sqlite3 import threading import time import pyperclip import tkinter as tk from tkinter import * class ClipBoardHistoryGui(tk.Tk): #gestisce tutti i frame def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) self.title('clipboard history') self.geometry("600x400") container = tk.Frame(self) container.pack(fill = "both", expand = True) container.grid_rowconfigure(0, weight = 1) container.grid_columnconfigure(0, weight = 1) frame = PrimaPagina(container, self) frame.grid(row=0, column=0, sticky="nsew") frame.tkraise() class PrimaPagina(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) titolo = tk.Label(self, text="Python Per Tutti") titolo.pack() scrollbar = tk.Scrollbar(self) scrollbar.pack(side=RIGHT, fill=Y) self.elementi_copiati = tk.Listbox(self, yscrollcommand=scrollbar.set) bottone_cancella = tk.Button(self, text="Cancella elemento", command=self.cancella_elemento) bottone_aggiorna = tk.Button(self, text="Aggiorna Lista", command=self.carica_elementi) bottone_cancella.pack(side=BOTTOM) bottone_aggiorna.pack(side=TOP) self.elementi_copiati.pack(side=LEFT, fill=BOTH, expand=True) scrollbar.config(command=self.elementi_copiati.yview) self.carica_database() self.carica_elementi() thread_ascolto_tastiera = threading.Thread(target=self.clipboard) thread_ascolto_tastiera.daemon = True thread_ascolto_tastiera.start() def carica_database(self): self.conn = sqlite3.connect('clip_history.db', check_same_thread=False) self.c = self.conn.cursor() sql = """CREATE TABLE IF NOT EXISTS history (id integer primary key, testo text, orario text)""" self.c.execute(sql) self.conn.commit() def cancella_elemento(self): self.c.execute("DELETE FROM history WHERE testo=?", [self.elementi_copiati.get(self.elementi_copiati.curselection())]) self.conn.commit() self.carica_elementi() def carica_elementi(self): self.elementi_copiati.delete(0, 'end') self.c.execute("SELECT testo FROM history") rows = self.c.fetchall() for testo, in rows: self.elementi_copiati.insert(END, testo) def aggiungi_elemento(self, element): self.elementi_copiati.insert(END, element) def clipboard(self): testo_precedente = pyperclip.paste() sql = """ INSERT INTO history(testo, orario) values (?,?) """ while True: testo = pyperclip.paste() if testo != testo_precedente: testo_precedente = testo self.c.execute(sql, [testo, dt.datetime.now()]) self.conn.commit() self.aggiungi_elemento(testo) time.sleep(0.2) if __name__ == '__main__': app = ClipBoardHistoryGui() app.mainloop()