Does anyone have any example code of how to use selections with pygtk
1.99.13. I've made a couple of attempts but I'm not getting anything in
the received SelectionData. I've attached my test code.
Thanks
John
#!/usr/bin/env python
# example getselection.py
import gtk
import struct
class GetSelectionExample:
SELECTION_PRIMARY = 1
SELECTION_TYPE_STRING = 31
SELECTION_TYPE_ATOM = 4
# Signal handler invoked when user clicks on the
# "Get String Target" button
def get_stringtarget(self, widget):
# And request the "STRING" target for the primary selection
ret = widget.selection_convert("PRIMARY",
"STRING")
print ret
return
# Signal handler invoked when user clicks on the "Get Targets" button
def get_targets(self, widget):
# And request the "TARGETS" target for the primary selection
ret = widget.selection_convert("PRIMARY",
"TARGETS")
print ret
return
# Signal handler called when the selections owner returns the data
def selection_received(self, widget, selection_data, *data):
# Make sure we got the data in the expected form
print selection_data.selection, selection_data.target, selection_data.type,
selection_data.format, selection_data.data, selection_data.get_targets(),
selection_data.get_text(), data
if selection_data.type == self.SELECTION_TYPE_STRING:
# Print out the string we received
print "STRING TARGET: %s" % selection_data.data
elif selection_data.type == self.SELECTION_TYPE_ATOM:
# Print out the atoms we received
atoms = struct.unpack(
'L'*(selection_data.length*8/selection_data.format),
selection_data.data)
for atom in atoms:
name = repr(atom)
if name != None:
print "%s" % name
else:
print "(bad atom)"
else:
print "Selection was not returned as \"STRING\" or \"ATOM\"!"
return
def __init__(self):
# Create the toplevel window
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title("Get Selection")
window.set_border_width(10)
window.connect("destroy", gtk.mainquit)
vbox = gtk.VBox(gtk.FALSE, 0)
window.add(vbox)
vbox.show()
# Create a button the user can click to get the string target
button = gtk.Button("Get String Target")
button.connect("clicked", self.get_stringtarget)
button.connect("selection_received", self.selection_received)
vbox.pack_start(button)
button.show()
# Create a button the user can click to get targets
button = gtk.Button("Get Targets")
button.connect("clicked", self.get_targets)
button.connect("selection_received", self.selection_received)
vbox.pack_start(button)
button.show()
window.show()
def main():
gtk.main()
return 0
if __name__ == "__main__":
GetSelectionExample()
main()