⬆️ ⬇️

Playing Tic-Tac-Toe with Python and GTK

Foreword



The new blog GTK + pleased with the article for beginners and I decided to try something simpler than C ++ / C. Python was very helpful. The volume of Python code for working with GTK is significantly less than in C ++, which is good news.



PyGTK



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



Under the cut an example application.

')

Sample application



To learn PyGTK, I decided to write a simple example - the Tic-Tac-Toe game.

The game window will be as simple as possible - a square of 3x3 buttons.



Game window:

Tic-tac-toe game window



So proceed to implement. The program has a couple of classes: XO_Field and XO_Win, the first one stores information about the field, the second one creates the GUI for the application and processes the events.



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



Use the pygtk module version 2.0 or later:

import pygtk pygtk.require('2.0') import gtk 


Create a simple window, which, when closed, will interrupt the GTK event cycle. The destroy function will be introduced 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. For each of the buttons, a “clicked” event handler is added, the adding 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() 


The function that creates the button:

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


Running the main GTK event loop and killing a window:

 def main(self): gtk.main() def destroy(self, widget, data=None): gtk.main_quit() 


By clicking on the buttons, the mark is set to the specified position and the move to the next player is passed, if the winner has not yet been determined. If the game is complete, the result is displayed in the title bar.



The game of Tic-Tac-Toe and its implementation is rather trivial, the link to the source code is given below. Of greater interest is the very cycle of the Python application in conjunction with GTK, for example, when collecting garbage links to GTK objects, the garbage collector also causes 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 the grid) with 3 rows and 3 columns, but somehow I remembered about 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 moving gradually to it, but the issue with gtk 3 support on platforms other than linux is not completely clear.



All example source code



Sources

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

Source: https://habr.com/ru/post/135833/



All Articles