Development Team/Almanac/GTK's Clipboard Module: Difference between revisions

New page: === How do I copy and paste text from GTK widgets? === According to the [http://www.pygtk.org/pygtk2tutorial/ch-NewInPyGTK2.2.html PyGtk Tutorial], widgets of type Entry, SpinButton and T...
 
Line 24: Line 24:
         #### Create the copy button and assign callback
         #### Create the copy button and assign callback
         copyClipInput = gtk.Button(label='Copy Text')
         copyClipInput = gtk.Button(label='Copy Text')
         '''copyClipInput.connect("clicked", self._copyClipInput_cb, None)'''
         copyClipInput.connect("clicked", self._copyClipInput_cb, None) # CALLBACK USED WHEN COPY BUTTON PRESSED!
         button_row.pack_start(copyClipInput, expand=False, fill=False)
         button_row.pack_start(copyClipInput, expand=False, fill=False)
         #### Create the paste button and assign callback
         #### Create the paste button and assign callback
         pasteClipInput = gtk.Button(label='Paste Text')
         pasteClipInput = gtk.Button(label='Paste Text')
         '''pasteClipInput.connect("clicked", self._pasteClipInput_cb, None)'''
         pasteClipInput.connect("clicked", self._pasteClipInput_cb, None) # CALLBACK USED WHEN PASTE BUTTON PRESSED!
         #### Put everything together in the UI
         #### Put everything together in the UI
         button_row.pack_start(copyClipInput, expand=False, fill=False)
         button_row.pack_start(copyClipInput, expand=False, fill=False)
Line 38: Line 38:
         first_page.pack_start(gtk.Label(''), expand=True, fill=True) #for appearance purposes
         first_page.pack_start(gtk.Label(''), expand=True, fill=True) #for appearance purposes


    #### Method: _copyClipInput_cb, which is called when the "Copy Text" button is clicked
    def _copyClipInput_cb(self, widget, data=None):
        #Select all text in self.copyClipInput and copy it to the clipboard
        self.clipInput.select_region(0, -1)
        self.clipInput.cut_clipboard()
        _logger.debug("$$$$$$$$$$$$$$$$$$$ Copy button pressed")


    #### Method: _pasteClipInput_cb, which is called when the "Paste Text" button is clicked
    def _pasteClipInput_cb(self, widget, data=None):
        self.clipInput.paste_clipboard()
        _logger.debug("******************* Paste button pressed.")


</pre>
</pre>
Note that when you cut or copy text programmatically, you must first select a region in the widget. That is why we have the call to select_region() in the _copyClipInput_cb method. The -1 argument ensures that all text in the widget is selected. <ref>[http://www.pygtk.org/docs/pygtk/class-gtkeditable.html#method-gtkeditable--select-region]</ref>
= Notes =
<references />