[Tutor] Why is it not working?
Antonio Zagheni
zagheni at yahoo.com
Thu Feb 5 01:12:35 CET 2015
Hi there,
I am a begginer in python and I'm trying to learn something about Tkinter.
I did a game (the code is below) using Tkinter were two players have to fill a row, a column or a diagonal with either 'X' or 'O'.
When it happens, the code was supposed to recognize the winner ('Jogador 1 or 2 vence!' --> translating from portuguese means 'player 1 or 2 wins!').
The problem is that sometimes the code doesn't recognize the winner and I can't find the problem.
I don't have any error message.
I belive that the problem is related with the fact that after clicking a button it calls a function an inside that function another function is called (analise()), but I am not sure...
If somebody could take a look and give me some tips to solve the problem I will apreciate a lot.
The problem occurs either using Windows 7 or Ubuntu 14.04 in a 64 bit computer, both running idle with python's version 2.7.7.
Regards,
Antonio Zagheni.
#-------------------------------------------------------------------------------
from Tkinter import *
janela = Tk()
flag_botaoA1 = "1"
flag_botaoA2 = "2"
flag_botaoA3 = "3"
flag_botaoB1 = "4"
flag_botaoB2 = "5"
flag_botaoB3 = "6"
flag_botaoC1 = "7"
flag_botaoC2 = "8"
flag_botaoC3 = "9"
jogador = 1
rodada = 1
fim = False
rotulo_botaoA1 = StringVar()
rotulo_botaoA2 = StringVar()
rotulo_botaoA3 = StringVar()
rotulo_botaoB1 = StringVar()
rotulo_botaoB2 = StringVar()
rotulo_botaoB3 = StringVar()
rotulo_botaoC1 = StringVar()
rotulo_botaoC2 = StringVar()
rotulo_botaoC3 = StringVar()
active_player = StringVar()
active_player.set(str(jogador))
def analise():
global jogador, rodada
print flag_botaoA1, flag_botaoA2, flag_botaoA3
print flag_botaoB1, flag_botaoB2, flag_botaoB3
print flag_botaoC1, flag_botaoC2, flag_botaoC3
print jogador, rodada
if rodada == 9:
active_player.set('Game Over!')
fim = True
if flag_botaoA1 == flag_botaoB1 == flag_botaoC1 or flag_botaoA2 == flag_botaoB2 == flag_botaoC2 or flag_botaoA3 == flag_botaoB3 == flag_botaoC3:
active_player.set(str(jogador) + ' vence!')
fim = True
elif flag_botaoA1 == flag_botaoA2 == flag_botaoA3 or flag_botaoB1 == flag_botaoB2 == flag_botaoB3 or flag_botaoC1 == flag_botaoC2 == flag_botaoC3:
active_player.set(str(jogador) + ' vence!')
fim = True
elif flag_botaoA1 == flag_botaoB2 == flag_botaoC3 or flag_botaoC1 == flag_botaoB2 == flag_botaoA3:
active_player.set(str(jogador) + ' vence!')
fim = True
rodada += 1
def clickA1():
global jogador, flag_botaoA1
if jogador == 1 and flag_botaoA1 == "1" and fim == False:
rotulo_botaoA1.set("X")
flag_botaoA1 = 'X'
analise()
jogador = 2
if jogador == 2 and flag_botaoA1 == "1" and fim == False:
rotulo_botaoA1.set("O")
flag_botaoA1 = 'O'
analise()
jogador = 1
active_player.set(str(jogador))
def clickA2():
global jogador, flag_botaoA2
if jogador == 1 and flag_botaoA2 == "2" and fim == False:
rotulo_botaoA2.set("X")
flag_botaoA2 = 'X'
analise()
jogador = 2
if jogador == 2 and flag_botaoA2 == "2" and fim == False:
rotulo_botaoA2.set("O")
flag_botaoA2 = 'O'
analise()
jogador = 1
active_player.set(str(jogador))
def clickA3():
global jogador, flag_botaoA3
if jogador == 1 and flag_botaoA3 == "3" and fim == False:
rotulo_botaoA3.set("X")
flag_botaoA3 = 'X'
analise()
jogador = 2
if jogador == 2 and flag_botaoA3 == "3" and fim == False:
rotulo_botaoA3.set("O")
flag_botaoA3 = 'O'
analise()
jogador = 1
active_player.set(str(jogador))
def clickB1():
global jogador, flag_botaoB1
if jogador == 1 and flag_botaoB1 == "4" and fim == False:
rotulo_botaoB1.set("X")
flag_botaoB1 = 'X'
analise()
jogador = 2
if jogador == 2 and flag_botaoB1 == "4" and fim == False:
rotulo_botaoB1.set("O")
flag_botaoB1 = 'O'
analise()
jogador = 1
active_player.set(str(jogador))
def clickB2():
global jogador, flag_botaoB2
if jogador == 1 and flag_botaoB2 == "5" and fim == False:
rotulo_botaoB2.set("X")
flag_botaoB2 = 'X'
analise()
jogador = 2
if jogador == 2 and flag_botaoB2 == "5" and fim == False:
rotulo_botaoB2.set("O")
flag_botaoB2 = 'O'
analise()
jogador = 1
active_player.set(str(jogador))
def clickB3():
global jogador, flag_botaoB3
if jogador == 1 and flag_botaoB3 == "6" and fim == False:
rotulo_botaoB3.set("X")
flag_botaoB3 = 'X'
analise()
jogador = 2
if jogador == 2 and flag_botaoB3 == "6" and fim == False:
rotulo_botaoB3.set("O")
flag_botaoB3 = 'O'
analise()
jogador = 1
active_player.set(str(jogador))
def clickC1():
global jogador, flag_botaoC1
if jogador == 1 and flag_botaoC1 == "7" and fim == False:
rotulo_botaoC1.set("X")
flag_botaoC1 = 'X'
analise()
jogador = 2
if jogador == 2 and flag_botaoC1 == "7" and fim == False:
rotulo_botaoC1.set("O")
flag_botaoC1 = 'O'
analise()
jogador = 1
active_player.set(str(jogador))
def clickC2():
global jogador, flag_botaoC2
if jogador == 1 and flag_botaoC2 == "8" and fim == False:
rotulo_botaoC2.set("X")
flag_botaoC2 = 'X'
analise()
jogador = 2
if jogador == 2 and flag_botaoC2 == "8" and fim == False:
rotulo_botaoC2.set("O")
flag_botaoC2 = 'O'
analise()
jogador = 1
active_player.set(str(jogador))
def clickC3():
global jogador, flag_botaoC3
if jogador == 1 and flag_botaoC3 == "9" and fim == False:
rotulo_botaoC3.set("X")
flag_botaoC3 = 'X'
analise()
jogador = 2
if jogador == 2 and flag_botaoC3 == "9" and fim == False:
rotulo_botaoC3.set("O")
flag_botaoC3 = 'O'
analise()
jogador = 1
tabuleiro = Frame(janela)
tabuleiro.pack()
janela.title('Jogo da Velha')
linha1 = Frame(janela)
linha1.pack()
linha2 = Frame(janela)
linha2.pack()
linha3 = Frame(janela)
linha3.pack()
linha4 = Frame(janela)
linha4.pack()
titulo = Label (tabuleiro, text = 'Jogo da Velha')
titulo.pack()
autor = Label (tabuleiro, text = 'by Antonio Zagheni')
autor.pack()
player = Label (linha4, text = 'Jogador')
player.pack(side = LEFT)
actual_player = Label (linha4, textvariable = active_player)
actual_player.pack(side = LEFT)
botaoA1 = Button (linha1, textvariable = rotulo_botaoA1, command = clickA1, width = 1)
botaoA1.pack(side = LEFT)
botaoB1 = Button (linha2, textvariable = rotulo_botaoB1, command = clickB1, width = 1)
botaoB1.pack(side = LEFT)
botaoC1 = Button (linha3, textvariable = rotulo_botaoC1, command = clickC1, width = 1)
botaoC1.pack(side = LEFT)
botaoA2 = Button (linha1, textvariable = rotulo_botaoA2, command = clickA2, width = 1)
botaoA2.pack(side = LEFT)
botaoB2 = Button (linha2, textvariable = rotulo_botaoB2, command = clickB2, width = 1)
botaoB2.pack(side = LEFT)
botaoC2 = Button (linha3, textvariable = rotulo_botaoC2, command = clickC2, width = 1)
botaoC2.pack(side = LEFT)
botaoA3 = Button (linha1, textvariable = rotulo_botaoA3, command = clickA3, width = 1)
botaoA3.pack(side = LEFT)
botaoB3 = Button (linha2, textvariable = rotulo_botaoB3, command = clickB3, width = 1)
botaoB3.pack(side = LEFT)
botaoC3 = Button (linha3, textvariable = rotulo_botaoC3, command = clickC3, width = 1)
botaoC3.pack(side = LEFT)
janela.mainloop()
More information about the Tutor
mailing list