PyGtk text entry dialog

PyGtk doesn’t come with a pre-made prompt-for-a-string dialog, so I rolled my own.

Text entry dialog
Text entry dialog

The code follows:

#!/usr/bin/env python
import gtk
def responseToDialog(entry, dialog, response):
def getText():
	#base this on a message dialog
	dialog = gtk.MessageDialog(
	dialog.set_markup('Please enter your <b>name</b>:')
	#create the text input field
	entry = gtk.Entry()
	#allow the user to press enter to do ok
	entry.connect("activate", responseToDialog, dialog, gtk.RESPONSE_OK)
	#create a horizontal box to pack the entry and a label
	hbox = gtk.HBox()
	hbox.pack_start(gtk.Label("Name:"), False, 5, 5)
	#some secondary text
	dialog.format_secondary_markup("This will be used for <i>identification</i> purposes")
	#add it and show it
	dialog.vbox.pack_end(hbox, True, True, 0)
	#go go go
	text = entry.get_text()
	return text
if __name__ == '__main__':
	print "The name was %s" % getText()

In order to turn this into a password dialog, add in the part in bold:

entry = gtk.Entry()
#allow the user to press enter to do ok

PyGtk text entry dialog

7 thoughts on “PyGtk text entry dialog

  1. Hi dude!

    This entry is really intresting, but I have a problem doing something similar. My code is:

    cuadro = gtk.MessageDialog(None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_QUESTION, gtk.BUTTONS_OK, None)
    cuadro.set_markup("bla bla")

    I want that dialog to show information and get close when the user press the OK button, but it doesn't work, it just freezes. How can I do that? Could you help me please?


  2. jebavarde says:

    Were you doing this in IDLE?

    That code works fine when run from the command line (after being saved into a file) but causes IDLE to crash. Not sure why that would be the case… but just my observations.

    Sorry I couldn’t be further help.

    1. jebavarde says:

      On linux pygtk just matches the current gtk theme (as on windows I think). It *is* possible to change the gtk theme on windows (there should have been a gtk theme selector installed with gtk), but then it’s probably a better idea to stick with the default since then you get at least some visual conformity (i.e. I *imagine* the default tries to match the native windows style).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s