473,382 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

pyGTK tic-tac-toe question

849 Expert 512MB
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
2 1787
Smygis
126 100+
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
Laharl
849 Expert 512MB
*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

6
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...
4
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...
6
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...
5
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...
1
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...
25
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...
14
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...
1
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...
0
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 ...
0
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.