Search code examples
python-3.xtkintercopycopy-paste

copy-paste data from different application


My Tkinter program contains the ctrl+c hotkey function for selected row ( The program will keep at least one row selected for the entire time ), while running this program if i try to copy-paste the text from any other application using ctrl+c, it will copy the selected row from my program. it's becoming a big issue, i couldn't copy-paste anything from other application if my code is running. Please help me with the idea to solve this.

Code that i used in my program:

k.add_hotkey("ctrl+c",lambda:self.copy())



def copy(self):
    try:
        self.master.clipboard_clear()
        curItems = self.treeview.selection()
    
        for i in curItems:
            s=str(self.treeview.item(i)['values'])
            self.master.clipboard_append(s+'\n')
        self.master.update()
        
    except:
        pass

Solution

  • Don't use keyboard to bind to the ctrl+c hotkey. Use tkinter's bind method instead.

    self.treeview.bind('<Control-c>', self.copy)
    

    This will only bind to control c when you are focused on the treeview object.