473,700 Members | 2,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pyGTK tic-tac-toe question

849 Recognized Expert Contributor
I'm working in pyGTK (completely new to it) and I need to display a tic-tac-toe board. It doesn't have to keep track of the game state, just place Xs and Os on click. Unfortunately, I can't get all 9 buttons to show. Currently, I'm trying to use 3 HButtonBoxes of 3 buttons each packed into an VBox. Unfortunately, I get a blank window when I run.

I'm running Python 2.5.1 on Ubuntu Gutsy.

Expand|Select|Wrap|Line Numbers
  1. import pygtk
  2. import gtk
  4. class TicTacToe:
  5.     def main(self):
  6.         gtk.main()
  8.     def destroy(self, widget, data=None):
  9.         gtk.main_quit()
  11.     def setC(self, widget, event, data=None):
  12.         widget.set_label(self.current)
  13.         if self.current == 'X':
  14.             self.current = 'Y'
  15.         else:
  16.             self.current = 'X'
  17.         print "Clicked!"
  19.     def delete_event(self, widget, event, data = None):
  20.         print "Delete event occurred"
  21.         return False
  23.     def __init__(self):
  24.         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  25.         self.buttonbox1 = gtk.HButtonBox()
  26.         self.button1=gtk.Button()
  27.         self.button2=gtk.Button()
  28.         self.button3=gtk.Button()
  29.         self.button1.connect('clicked', self.setC, None)
  30.         self.button2.connect('clicked', self.setC, None)
  31.         self.button3.connect('clicked', self.setC, None)
  32.         self.buttonbox1.pack_start(self.button1)
  33.         self.buttonbox1.pack_start(self.button2)
  34.         self.buttonbox1.pack_start(self.button3)
  35.         self.current = 'X'
  37.         self.window.connect("destroy", window.destroy)
  38.         self.window.connect_object("delete_event", self.delete_event)
  40.         self.buttonbox2 = gtk.HButtonBox()
  41.         self.button4=gtk.Button()
  42.         self.button5=gtk.Button()
  43.         self.button6=gtk.Button()
  44.         self.button4.connect('clicked', self.setC, None)
  45.         self.button5.connect('clicked', self.setC, None)
  46.         self.button6.connect('clicked', self.setC, None)
  47.         self.buttonbox2.pack_start(self.button4)
  48.         self.buttonbox2.pack_start(self.button5)
  49.         self.buttonbox2.pack_start(self.button6)
  51.         self.buttonbox3 = gtk.HButtonBox()
  52.         self.button7=gtk.Button()
  53.         self.button8=gtk.Button()
  54.         self.button9=gtk.Button()
  55.         self.buttonbox3.pack_start(self.button7)
  56.         self.buttonbox3.pack_start(self.button8)
  57.         self.buttonbox3.pack_start(self.button9)
  58.         self.button7.connect('clicked', self.setC, None)
  59.         self.button8.connect('clicked', self.setC, None)
  60.         self.button9.connect('clicked', self.setC, None)
  62.         self.this = gtk.VBox(False, 2)
  63.         self.this.pack_start(self.buttonbox1)
  64.         self.this.pack_start(self.buttonbox2)
  65.         self.this.pack_start(self.buttonbox3)
  67.         self.button1.show()
  68.         self.button2.show()
  69.         self.button3.show()
  70.         self.button4.show()
  71.         self.button5.show()
  72.         self.button6.show()
  73.         self.button7.show()
  74.         self.button8.show()
  75.         self.button9.show()
  76.         self.buttonbox1.show()
  77.         self.buttonbox2.show()
  78.         self.buttonbox3.show()
  79.         self.this.show()
  80.         self.window.show()
  82. if __name__ == "__main__":
  83.     tic = TicTacToe()
  84.     tic.main()
Feb 2 '08 #1
2 1825
126 New Member
i rewrote your entire program to try and figure out what you done wrong, cuz i culd not see what it whas.

And then when i got to the end of my own program i noticed what whas missing! You forgot to add your VBox 'this' to the window!

self.window.add (self.this)


Here is my version anyway:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2. # coding: UTF-8 
  4. import pygtk
  5. pygtk.require('2.0')
  6. import gtk
  8. class TicTacToe:
  11.     def destroy(self, widget, data=None):
  12.         gtk.main_quit()
  14.     def delete_event(self, widget, event, data=None):
  15.         print "delete event!"
  16.         return False
  18.     def create(self):
  19.         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  20.         self.window.connect("destroy", self.destroy)
  21.         self.window.connect("delete_event", self.delete_event)
  23.         self.table = gtk.Table(rows=3, columns=3, homogeneous=True)
  24.         self.buttons = [gtk.Button(label="button %d" % i) for i in xrange(9)]
  25.         for button in self.buttons:
  26.             button.connect('clicked', self.setOwner)
  28.     def layout(self):
  29.         for widget in zip(self.buttons, [(i,b) for i in range(3) \
  30.                                         for b in range(3)]):
  31.             self.table.attach(widget[0], widget[1][0], widget[1][0]+1,\
  32.                                  widget[1][1], widget[1][1]+1)
  33.         self.window.add(self.table)
  35.     def show(self):
  36.         for button in self.buttons:
  37.             button.show()
  38.         self.table.show()
  39.         self.window.show()
  41.     def setOwner(self, widget, data=None):
  42.         pass
  44.     def main(self):
  45.         gtk.main()
  47.     def __init__(self):
  48.         self.create()
  49.         self.layout()
  50.         self.show()
  52. if __name__ == "__main__":
  53.     board = TicTacToe()
  54.     board.main()
Feb 3 '08 #2
849 Recognized Expert Contributor
*facepalm* Thank you...*feels like a moron*
Feb 3 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Edwin Young | last post by:
Hi, I'm writing a fractal-generating program in a mixture of C and Python. Python handles all the GUI parts using PyGTK. After finishing the calculations, I have a buffer containing the RGB data of the image. I can display this fine on screen using draw_rgb_image. Now I need to save the image data into a file. gtk.gdk.Pixbuf.save() would be ideal, but I'm stumped trying to create a Pixbuf from my image data. In C,...
by: j_mckitrick | last post by:
Hi all. Here is a tiny container for one of each combo box, along with the glade file. Just 2 widgets, so hopefully not too large. How the heck do I get the selection from the ComboBox, as opposed to the ComboEntryBox? I'm pulling my hair out over this one! jonathon
by: Mark Mitchell | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I have a program written in python that tells me it requires PyGTK, so I DLed, ./configure, make, make install and the process seems to go ok. Some errors but the process finishes so I assumed they weren't fatal errors. So now I attempt to run my python program and I get; $ gccp.py Traceback (most recent call last):
by: Egbert Bouwman | last post by:
As a newby to PyGTK I already have produced some gui's I like, but I have difficulties in finding out what method or other instrument I need for doing various things. I have not yet found the tutorial or otherwise that explains concepts like setting (relative) sizes, setting or manipulating focus, setting colors, etc. Also when I think I need a method with a specific word it its name, and I cannot find it, there are two (not...
by: Thomas Bartkus | last post by:
I am experimenting (flailing around?) with glade and python. Both under MS Windows and Linux. I understand why I want to "import gtk" It gives me access to the critical gui program loop gtk.main() and main_quit() I am also very grateful for import gtk.glade This lets me open my xml format glade file
by: TPJ | last post by:
GUI's etc: PyGtk on Windows "(...) So if someone develops mainly for X and just wants to make sure that it is not impossible to run on Windows, you can use PyGTK. (...)", July 2nd, 1999 pyGTK on Windows "(...) > can i use pyGTK under > Windows???
by: Rod W | last post by:
I'm just starting out on Python but my primary goal is to provide applications with some user interface (GUI). Can someone point me to a good comparison of whether I should use wxPython (with wxGlade I assume) or PyGTK (with Glade I assume)? I'd prefer open source (not necessarily GPL though) tools. Rod
by: krishnakant Mane | last post by:
hello, I will be writing some code in PyGTK to run on linux. but I want to know if there are any installers or distutils available for PyGTK on windows? I have heard that installing gimp or even pygtk is next to impossible and is very tedious if at all one gets success. can any one share their experiences with installing and running pygtk on windos? any links for downloading proper packages will be highly appreciated. regards,...
by: He Jibo | last post by:
Hi, Everyone, Could someone help me how to install pygtk? I get some problems with it. Here is the error I get while install pygtk: https://netfiles.uiuc.edu/jibohe2/error.GIF?uniq=-k6678k I use python 2.4, I first installed glade-2.0.1-win32_with_gtk, and then pycairo-1.0.2-1.win32-py2.4, pygobject-2.12.3-1.win32-py2.4, and
by: Michael Palmer | last post by:
On Sep 16, 12:30 pm, binaryjesus <coolman.gu...@gmail.comwrote: I haven't tried it myself, but I came across a blog post the other day that describes a way of building windows installers for pyGTK applications at http://unpythonic.blogspot.com/2007/07/pygtk-py2exe-and-inno-setup-for-single.html
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.