Monday, December 14, 2009

Here's why I need Quidgets


A friend of mine asked me today to help him with a little bit of code. Basically, he needed to get a number for a user from a Python script. While he is a good coder, he doesn't know PyGtk all that well.

So here's the code that I wrote for him, it grabs a string as a tag from the user:

        dialog = gtk.Dialog("Bug Tag",None,gtk.DIALOG_MODAL,(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
content_area = dialog.get_content_area()
hbox = gtk.HBox(2, 5)
label = gtk.Label("Tag:")
entry = gtk.Entry()
hbox.pack_start(label,False, False)
hbox.pack_end(entry, False, True)
content_area.pack_start(hbox)
hbox.show()
label.show()
entry.show()

result = dialog.run()
dialog.destroy()

if result == gtk.RESPONSE_ACCEPT:
tag = entry.get_text()
else:
return


What? I have to refer to six separate objects just to show a simple dialog? The code should be more like:

       
result, input = whatever_I_name_this_function("Title")
dialog.destroy()

if result == gtk.RESPONSE_ACCEPT:
tag = input
else:
return


I don't think this will take me all that long to right, though making the dialog return a tuple may be a tad harder than I expect, I'll see what I can do. Maybe I'll make a simple object to create instead of a single function call ...

6 comments:

  1. Don't forget:
    dialog.set_has_separator(False)
    content_area.set_border_width(5)

    the defaults really should be changed...

    ReplyDelete
  2. I'm using a subclass of gtk.MessageDialog for simple text input in one of my projects which looks like: http://paste.ubuntu.com/341767

    ReplyDelete
  3. Canned dialogs were probably of those things that, sadly, in Gtk were thought to be a part of GNOME.

    ReplyDelete