0

Hello
I have made a simple gui tic tac toe game using glade and python 2.6/
Now I want to change the text of the button when i click it.
Does anyone know how to do this??

code:

#!/usr/bin/env python

import gtk
import gtk.glade
import sys

class iksoks:

    wTree = None

    def __init__(self):
        self.wTree = gtk.glade.XML("ttt.glade")

        rc = {
            "on_button1_clicked" : self.b1,
            "on_button2_clicked" : self.b2,
            "on_button3_clicked" : self.b3,
            "on_button4_clicked" : self.b4,
            "on_button5_clicked" : self.b5,
            "on_button6_clicked" : self.b6,
            "on_button7_clicked" : self.b7,
            "on_button8_clicked" : self.b8,
            "on_button9_clicked" : self.b9,
            }
        self.wTree.signal_autoconnect(rc)
        self.wnd = self.wTree.get_widget("window1")


        global r
        global a1
        global a2
        global a3
        global b1
        global b2
        global b3
        global c1
        global c2
        global c3

        r = 1

    def b1(self, widget):
        if r == 1:
            self.wTree.get_widget("button1").set_label("x")
            a1 = "x"
            r = 2
        else:
            self.wTree.get_widget("button1").set_label("o")
            a1 = "o"
            r = 1

    def b2(self, widget):
        if r == 1:
            self.wTree.get_widget("button2").set_label("x")
            a2 = "x"
            r = 2
        else:
            self.wTree.get_widget("button2").set_label("o")
            a2 = "o"
            r = 1

    def b3(self, widget):
        if r == 1:
            self.wTree.get_widget("button3").set_label("x")
            a3 = "x"
            r = 2
        else:
            self.wTree.get_widget("button3").set_label("o")
            a3 = "o"
            r = 1

    def b4(self, widget):
        if r == 1:
            self.wTree.get_widget("button4").set_label("x")
            b1 = "x"
            r = 2
        else:
            self.wTree.get_widget("button4").set_label("o")
            b1 = "o"
            r = 1

    def b5(self, widget):
        if r == 1:
            self.wTree.get_widget("button5").set_label("x")
            b2 = "x"
            r = 2
        else:
            self.wTree.get_widget("button5").set_label("o")
            b2 = "o"
            r = 1

    def b6(self, widget):
        if r == 1:
            self.wTree.get_widget("button6").set_label("x")
            b3 = "x"
            r = 2
        else:
            self.wTree.get_widget("button6").set_label("o")
            b3 = "o"
            r = 1

    def b7(self, widget):
        if r == 1:
            self.wTree.get_widget("button7").set_label("x")
            c1 = "x"
            r = 2
        else:
            self.wTree.get_widget("button7").set_label("o")
            c1 = "o"
            r = 1

    def b8(self, widget):
        if r == 1:
            self.wTree.get_widget("button8").set_label("x")
            c2 = "x"
            r = 2
        else:
            self.wTree.get_widget("button8").set_label("o")
            c2 = "o"
            r = 1

    def b9(self, widget):
        if r == 1:
            self.wTree.get_widget("button9").set_label("x")
            c3 = "x"
            r = 2
        else:
            self.wTree.get_widget("button9").set_label("o")
            c3 = "o"
            r = 1

    
        


io = iksoks()
io.wnd.show()
gtk.main()
3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by haro2x
0

Why do you have so many global statements?
Or did glade do that?
You should have self.r, self.a1, etc
instead of them all being global.
Global is bad.

0

In the time i wrote this code i didnt knew for self. Now I have a new code but my problem is when i click one button that the text of the button changes.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.