Playing Tic Tac Toe with Python and GTK

Foreword


The new GTK + blog made me happy with an article for beginners and I decided to try something simpler than C ++ / C. Python came in handy. The amount of code in Python for working with GTK is much less than in C ++, which is good news.

PyGTK


PyGTK is a binding of the GTK library for the Python language, PyGTK is used in many open programs (for example, IM Gajim). The library can be very interesting for Python programmers, as it is easy to use and completely hides the implementation of GTK.

Under cat application example.

Application example


To learn PyGTK, I decided to write a simple example - the game Tic Tac Toe.
The game window will be as simple as possible - a square of 3x3 buttons.

Game window:
Tic-Tac-Toe game window

So let's get started. The program has a couple of classes: XO_Field and XO_Win, the first stores information about the field, the second creates a GUI for the application and processes events.

For example, I used the pygtk module installed from the package manager in Ubuntu, on other systems it is worth using packages or python easy-install, and for Windows there is an installer.

We use the pygtk module version 2.0 or later:
import pygtk
pygtk.require('2.0')
import gtk

Let's create a simple window, upon closing of which we will interrupt the GTK event loop. The destroy function will be given a bit later.
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("destroy", self.destroy)
self.window.set_title("Крестики-Нолики.py!")
self.window.set_border_width(10)
self.window.set_size_request(400,400)

For the window, we will use a vertical layout of three lines with a horizontal one.
So the buttons will be located just in the form of a 3x3 square. A clicked event handler is added for each of the buttons, the add syntax is very similar to signals and slots in Qt.

self.vbox = gtk.VBox(False,0)
self.window.add(self.vbox)
for i in range(3):
    box = gtk.HBox(False,0)
    self.boxes.append(box)
    for j in range(3):
        self.buttons[i].append(self.create_button(box))
        self.buttons[i][j].connect("clicked",self.on_btn_click,i,j)
    self.vbox.pack_start(box,True,True,0)
    box.show()

Function creating the button:
def create_button(self,box):
    button = gtk.Button(self.field.chr)
    box.pack_start(button,True,True,0)
    button.show()
    return button

Starting the main GTK event loop and destroying the window:
def main(self):
    gtk.main()
def destroy(self, widget, data=None):
    gtk.main_quit()

By pressing the buttons, the character is placed in the indicated position and the move to the next player is transferred, if the winner has not yet been determined. If the game is completed, the result is displayed in the window title.

The game of Tic Tac Toe and its implementation is quite trivial, a link to the source code is given below. Of great interest is the cycle of the Python application in conjunction with GTK, for example, the garbage collector when collecting links to GTK objects also calls destructors for them, since UI elements contain many links to resources.

PS
The best solution would be to use GtkTable (a container for aligning widgets on a grid) with 3 rows and 3 columns, but somehow remembered it late.
Over time, the GTK API is updated, as noted in the comments, there is already a binding for gtk 3. It is worth gradually switching to it, but the question with support for gtk 3 on platforms other than linux is not completely clear.

All sample source code

Sources

  1. PyGTK on the Wiki
  2. PyGTK project site
  3. About layout and widgets

Also popular now: