Page 1 of 1

The Exit App (PyGTK)

Posted: Sat Jul 16, 2011 6:06 pm
by mikethedj4
In this tutorial. I show you guys how to make a simple window in PyGTK, as well as some functionality it has. Today we're going to add a button onto it, and when we click that button the application closes.

Yep! You guessed it we're going to create that simple, dumb Exit App in Python. Now why are we doing this again?

Well this should help you understand how you'd add vertical box's as well as horizontal box's to position your buttons, and what not.

So lets grab the code we created in this tutorial of just the window.
Code: Select all
#!/usr/bin/env python

import gtk

class app:

  def __init__(self):
	window = gtk.Window(gtk.WINDOW_TOPLEVEL)
	window.set_position(gtk.WIN_POS_CENTER)
	window.set_title("TestApp")
	window.set_default_size(200, 10)
	window.connect("destroy", gtk.main_quit)
	window.show_all()

app()
gtk.main()
Now right below window.connect("destroy", gtk.main_quit) we're going to call gtk.HBox(), hbox for short...
Code: Select all
hbox = gtk.HBox()
We're then going to add the horizontal box into our window.
Code: Select all
window.add(hbox)
Now we need to add a button that we'll call exit, and we'll also have it say "Exit" on the button. We will also haft to connect the button and state that when we click it, the application will close.
Code: Select all
exit = gtk.Button("Exit")
exit.connect("clicked", gtk.main_quit)
Last, but not least it's time to make our button visible.
Code: Select all
hbox.pack_start(exit, True)
Easy As That! cooll;

Now here's the full source code.
Code: Select all
#!/usr/bin/env python

import gtk

class app:

  def __init__(self):
	window = gtk.Window(gtk.WINDOW_TOPLEVEL)
	window.set_position(gtk.WIN_POS_CENTER)
	window.set_title("TestApp")
	window.set_default_size(200, 10)
	window.connect("destroy", gtk.main_quit)
	hbox = gtk.HBox()
	window.add(hbox)
	exit = gtk.Button("Exit")
	exit.connect("clicked", gtk.main_quit)
	hbox.pack_start(exit, True)
	window.show_all()

app()
gtk.main()