IronPython and Gtk-Sharp/GTK#

This example of using IronPython and Gtk Sharp will show how to do the following:

  • Layouts with Gtk.VBox
  • Gtk.Buttons
  • Gtk.Entry
  • Widget Events (Callbacks)
  • Message Dialogs

For the latest updates check

To use Gtk Sharp from IronPython first you need to import the clr and add a reference to the gtk-sharp. Once this is finished you can import Gtk. The example below creates one window, adds a Gtk.Entry and Gtk.Button. The button has one event which is the self.HelloWorld function. The self.HelloWorld function displays a MessageDialog that will change the gtk.Entry default value to “Hello World!” if Yes is clicked. A Gtk.VBox is created and added to the window. This vbox is used to pack the self.textentry1 and button vertically. You can also use a Gtk.HBox instead or a combination of Gtk.VBox and Gtk.HBox.

Gtk.Application.Init() must be called before using Gtk and Gtk.Application.Run() starts the Gtk main event loop. The window has the DeleteEvent attached to call the self.DeleteEvent function. The self.DeleteEvent function alls Gtk.Application.Quite() which exits the application.

import clr
import Gtk

class GtkExample(object):
	def __init__(self):
		self.window = Gtk.Window("Hello World")
		self.window.DeleteEvent += self.DeleteEvent
		vbox = Gtk.VBox() 
		button = Gtk.Button("Show Message")
		button.Clicked += self.HelloWorld
		self.textentry1 = Gtk.Entry("Default Text")

	def DeleteEvent(self, widget, event):
	def HelloWorld(self, widget, event):
		m = Gtk.MessageDialog(None, Gtk.DialogFlags.Modal, Gtk.MessageType.Info, \
			Gtk.ButtonsType.YesNo, False, 'Change the text entry to "Hello World?"')

		result = m.Run()
		if result == int(Gtk.ResponseType.Yes):
			self.textentry1.Text = "Hello World!"
if __name__ == "__main__":