def show_context_menu(self, event): try: index = self.listbox.nearest(event.y) if index >= 0: self.listbox.selection_clear(0, tk.END) self.listbox.selection_set(index) menu = tk.Menu(self.root, tearoff=0) menu.add_command(label="Copy to clipboard", command=self.paste_selected) menu.add_command(label="Pin / Unpin", command=lambda: self.toggle_pin(index)) menu.add_command(label="Delete", command=lambda: self.delete_selected(index)) menu.post(event.x_root, event.y_root) except: pass
def on_close(self): self.running = False self.save_history() self.root.destroy() if == " main ": root = tk.Tk() # Add placeholder text workaround class EntryWithPlaceholder(tk.Entry): def init (self, master=None, placeholder="", **kwargs): super(). init (master, **kwargs) self.placeholder = placeholder self.bind("<FocusIn>", self.on_focus_in) self.bind("<FocusOut>", self.on_focus_out) self.on_focus_out() def on_focus_in(self, e): if self.get() == self.placeholder: self.delete(0, tk.END) self.config(fg="black") def on_focus_out(self, e): if not self.get(): self.insert(0, self.placeholder) self.config(fg="grey") tk.Entry = EntryWithPlaceholder windows clipboard history
self.history = [] self.pinned = set() self.load_history() self.last_text = pyperclip.paste() self.running = True # GUI self.create_widgets() self.update_history_display() # Start background monitor self.monitor_thread = threading.Thread(target=self.monitor_clipboard, daemon=True) self.monitor_thread.start() self.root.protocol("WM_DELETE_WINDOW", self.on_close) def show_context_menu(self, event): try: index = self
def monitor_clipboard(self): while self.running: try: current = pyperclip.paste() if current != self.last_text and current.strip() != "": self.last_text = current # Avoid duplicates of same consecutive text if not self.history or self.history[0]["text"] != current: self.history.insert(0, "text": current, "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S") ) # Trim history while len(self.history) > MAX_HISTORY: removed = self.history.pop() self.pinned.discard((removed["text"], removed["timestamp"])) self.save_history() # Update GUI in main thread self.root.after(0, self.update_history_display) except Exception as e: print("Monitor error:", e) time.sleep(0.5) = 0: self.listbox.selection_clear(0