Pappenheimer
Pappenheimer

Reputation: 43

Copy-Pasteable Text in pygtk's TreeView

I came across this rather unexpected problem. I have a gtk.TreeView with a single text column, that is rendered by gtk.CellRendererText. What I want is that the user can mark the displayed text using the mouse and get it into the clipboard by pressing ctrl+c. (I am referring to the very basic feature present in every webbrowser and texteditor). However, gtk won't let me do it. I have a simple example here, with non-markable / non-highlightable text:

import gtk

class TreeViewExample(gtk.TreeView):

    def __init__(self):
        gtk.TreeView.__init__(self)
        self.get_selection().set_mode(gtk.SELECTION_NONE)
        self.set_grid_lines(gtk.TREE_VIEW_GRID_LINES_HORIZONTAL)

        # create  model
        self.list_store = gtk.ListStore(str)
        self.list_store.append(['Hello, this is some \n multiline text.'])
        self.list_store.append(['Another text.'])
        self.set_model(self.list_store)

        # create text column
        col = gtk.TreeViewColumn('Text Column')
        self.append_column(col)
        cell = gtk.CellRendererText()
        col.pack_start(cell, True)
        col.add_attribute(cell, 'text', 0)



class MasterWindow(object):

    def destroy(self, widget, data=None):
        gtk.main_quit()

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_size_request(500,500)
        self.window.connect("destroy", self.destroy)       
        self.window.add(TreeViewExample())
        self.window.show_all()


if __name__ == '__main__':
    mw = MasterWindow()
    gtk.main()

I could of course make the cell editable, because the editable mode provides the feature. But that is far from elegant, because it is some kind of popup, that breaks the line wrap and, well, edits the text. What i need is a cell, that is not selectable, editable or anything, but has text, that can be copied.

Does anyone have a solution? Thanks!

Upvotes: 1

Views: 1224

Answers (2)

Maykel Llanes Garcia
Maykel Llanes Garcia

Reputation: 506

I made some changes in the code. Trying to answer questions of @Flimm.I hpe Helps you @Flimm.

import gtk

class TreeViewExample():
    def __init__(self):        
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_size_request(500,500)       

        self.model = gtk.ListStore(str,str)
        self.model.append(['a','b'])
        self.model.append(['c','a'])
        self.model.append(['d','a'])
        self.model.append(['e','a'])            
        self.treeview = gtk.TreeView(self.model)

        self.renderer = gtk.CellRendererText()
        self.renderer.set_property('editable', True)
        self.renderer.connect('edited', self._text_changed, 0)
        self.treeview.insert_column_with_attributes(-1, 'Copy-Pastable-Editable String', self.renderer, text=0)
        self.treeview.insert_column_with_attributes(-1, 'Copy-Pastable-Editable String', self.renderer, text=1)            

        self.window.add(self.treeview)
        self.window.show_all()

    def _text_changed( self, w, row, new_value, column):
        self.model[row][column] = new_value
if __name__ == '__main__':
    TreeViewExample()
    gtk.main()

Upvotes: 0

Maykel Llanes Garcia
Maykel Llanes Garcia

Reputation: 506

I made some changes in the code. Try the code below, this example works well and I am sure that is the answer to your question. Hope this helps you in the future of their knowledge and coding.

import gtk

class TreeViewExample():
    def __init__(self):        
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_size_request(500,500)       

        model = gtk.ListStore(str)
        model.append(['a'])
        model.append(['b'])
        model.append(['c'])

        treeview = gtk.TreeView(model)

        self.renderer = gtk.CellRendererText()
        self.renderer.set_property('editable', True)
        treeview.insert_column_with_attributes(-1, 'Copy-Pastable-Editable String', self.renderer, text=0)

        self.window.add(treeview)
        self.window.show_all()
if __name__ == '__main__':
    TreeViewExample()
    gtk.main()

Upvotes: 1

Related Questions