A Qua, 2004-01-14 às 14:22, John Hunter escreveu:
> While I know it is generally frowned up on to use stock buttons for
> purposes other than their original intentions to preserve consistent
> look and feel across applications, I often find myself wanting to use
> a different label with a stock icon.
> 
> I suspect this isn't possible from my reading of the docs, but I
> thought I'd ask.  I'd like to be able to do something like
> 
>     button = gtk.Button(stock=gtk.STOCK_CANCEL)
>     button.show()
>     button.set_use_stock(gtk.TRUE)
>     button.set_label('Hide')

  I copied some code from gedit that did this.  Later I converted it to
python.  You can find it in attachment.

> 
> But this doesn't work.
> 
> Is this possible?
> 
> Thanks,
> John Hunter
> _______________________________________________
> pygtk mailing list   [EMAIL PROTECTED]
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
-- 
Gustavo João Alves Marques Carneiro
<[EMAIL PROTECTED]> <[EMAIL PROTECTED]>
The universe is always one step beyond logic.
#! /usr/bin/env python
import pygtk; pygtk.require("2.0")
import bonobo
import Bonobo
import gnome.vfs
import gtk, gobject

# missing consts from typelib
Bonobo_Storage_READ        = 1
Bonobo_Storage_WRITE       = 2
Bonobo_Storage_CREATE      = 4
Bonobo_Storage_FAILIFEXIST = 8
Bonobo_Storage_COMPRESSED  = 16
Bonobo_Storage_TRANSACTED  = 32


def get_stream(uri, for_writing):
    """Returns a Bonobo_Stream for reading or writing to the specified URI"""
    vfs_uri   = gnome.vfs.URI(uri)
    shortname = vfs_uri.short_name
    dirname   = str(vfs_uri.parent)
    iid = "vfs:" + bonobo.moniker_util_escape(dirname, 0)
    storage = bonobo.get_object(iid, "IDL:Bonobo/Storage:1.0")
    try:
	if for_writing:
	    try:
		storage.erase(shortname)
	    except Bonobo.Storage.NoPermission:
		pass # ignore error (file may not exist)
	    stream = storage.openStream(shortname, Bonobo_Storage_WRITE)
	else:
	    stream = storage.openStream(shortname, Bonobo_Storage_READ)
    finally:
	storage.unref()
    return stream


# copied from gedit; converted to python
def button_new_with_stock_image(text, stock_id):

    button = gtk.Button()
    if button.child:
	button.remove(button.child)
	
    label = gobject.new(gtk.Label, label=text, use_underline=True)

    item = gtk.stock_lookup(stock_id)
    if item is not None:
	label.set_mnemonic_widget(button)
	image = gtk.image_new_from_stock(stock_id, gtk.ICON_SIZE_BUTTON)
	hbox = gtk.HBox(False, 2)
	align = gtk.Alignment(0.5, 0.5, 0.0, 0.0)
	hbox.pack_start(image, False, False, 0)
	hbox.pack_end(label, False, False, 0)
	button.add(align)
	align.add(hbox)
	align.show_all()
    else:
	label.set_mnemonic_widget(button)
	label.set_alignment(0.5, 0.5)
	label.show()
	button.add(label)
	gtk_widget_show(label);

    return button


# copied from gedit, converted to python
def dialog_add_button(dialog, text, stock_id, response_id):
    button = button_new_with_stock_image(text, stock_id)
    button.set_flags(gtk.CAN_DEFAULT)
    button.show()
    dialog.add_action_widget(button, response_id)
    return button


if __name__ == '__main__':
    # unit testing

    print "trying to get stream for writing"
    stream = get_stream("file:///tmp/foo.bar", True)
    stream.write("hello world")
    stream.unref()

    print "trying to get stream for reading"
    stream = get_stream("file:///tmp/foo.bar", False)
    print stream.read(bonobo.stream_client_get_length(stream))
    stream.unref()

    print "seems to work"

_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to