By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,507 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,507 IT Pros & Developers. It's quick & easy.

pyGTK tic-tac-toe question

Expert 100+
P: 849
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
  3.  
  4. class TicTacToe:
  5.     def main(self):
  6.         gtk.main()
  7.  
  8.     def destroy(self, widget, data=None):
  9.         gtk.main_quit()
  10.  
  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!"
  18.  
  19.     def delete_event(self, widget, event, data = None):
  20.         print "Delete event occurred"
  21.         return False
  22.  
  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'
  36.  
  37.         self.window.connect("destroy", window.destroy)
  38.         self.window.connect_object("delete_event", self.delete_event)
  39.  
  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)
  50.  
  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)
  61.  
  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)
  66.  
  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()
  81.  
  82. if __name__ == "__main__":
  83.     tic = TicTacToe()
  84.     tic.main()
Feb 2 '08 #1
Share this Question
Share on Google+
2 Replies


Smygis
100+
P: 126
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 
  3.  
  4. import pygtk
  5. pygtk.require('2.0')
  6. import gtk
  7.  
  8. class TicTacToe:
  9.  
  10.  
  11.     def destroy(self, widget, data=None):
  12.         gtk.main_quit()
  13.  
  14.     def delete_event(self, widget, event, data=None):
  15.         print "delete event!"
  16.         return False
  17.  
  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)
  22.  
  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)
  27.  
  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)
  34.  
  35.     def show(self):
  36.         for button in self.buttons:
  37.             button.show()
  38.         self.table.show()
  39.         self.window.show()
  40.  
  41.     def setOwner(self, widget, data=None):
  42.         pass
  43.  
  44.     def main(self):
  45.         gtk.main()
  46.  
  47.     def __init__(self):
  48.         self.create()
  49.         self.layout()
  50.         self.show()
  51.  
  52. if __name__ == "__main__":
  53.     board = TicTacToe()
  54.     board.main()
  55.  
Feb 3 '08 #2

Expert 100+
P: 849
*facepalm* Thank you...*feels like a moron*
Feb 3 '08 #3

Post your reply

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