Hey guys I have to create a python version of Tic Tac Toe, but does anyone know if it's possible to make this using Tkinter?
I think I have to have it nice and bright and colourful, therefore TKinter is the only solution i can think of using Python.
Any help, advice, suggestions would be helpful.
Thank you, Darkangelchick

## All 10 Replies

does anyone know if it's possible to make this using Tkinter?

Yes, it is. Coming up with a design for the game board (colors, sizes, etc.) might be the hardest part. We can help you once you get started if you get stuck on anything, just don't forget to post the troublesome code and the related error/problem

Is it you against another human, or you against the computer?

Is it you against another human, or you against the computer?

It has to be both if we can manage it :(

There are two separate parts of course. Start with the logic
1. How are you going to identify the individual squares, by row and column, or will each square have a unique number. And how will you test to see that the square is not already occupied, which will be necessary if playing against the computer (and people who try to cheat).
2. How will you store the moves so you can test for a tic-tac-toe
3. Will input be a separate entry box, and if so you will have to communicate between the two, so to start, test with console input only.

There are two separate parts of course. Start with the logic
1. How are you going to identify the individual squares, by row and column, or will each square have a unique number. And how will you test to see that the square is not already occupied, which will be necessary if playing against the computer (and people who try to cheat).
2. How will you store the moves so you can test for a tic-tac-toe
3. Will input be a separate entry box, and if so you will have to communicate between the two, so to start, test with console input only.

I'm pretty sure each square will have a unique number, and I'm not sure on how to test if a square is already occupied.
I have a text based one and it just reprints the grid with the updated moves, but I don't know how to store them.
I'm really not sure what you mean by console input only.
Sorry I'm kinda new at this :(

You can use a dictionary to store the moves. The square number would be the key, so let's say it is 1 through 9. You can then ask the person which square they want to occupy if using console input. If using a GUI, you would test for the location of the cursor, already knowing the boundaries of the squares. The value for each key (1->9) in the dictionay would be initialized to zero. If the "X" person is number 1 and the "O" person is number 2, then you would modify the dictionary with the corresponding person when they choose a square. You can also initialize to an empty string and modify with the actual "X" or "O". And that might be easier when printing as you would just print the contents of the dictionary instead of testing for 1 and print an "X" if true. To state the obvious, if the square number in the dictionary is not zero (or is not an empty string), the square is occupied

Also, you might also be able to use checkbuttons within each square for the GUI mode. The person would check the square they want. The checkbutton number would equal the square number. Just a thought on how it might be easier to use a GUI. But first things first = get the logic for handling the squares down. As the old say goes, first make it work, then make it fast, then make it pretty.

Ok guys here is a version I have got, but it's just text based.
Will having this make trying to make a TK one any easier?

``````def instructions():

print """\n\tWelcome to the game of tic-tac-toe\n
Have fun with this game against the computer.
Enter the number of the square in which you wish to move.
Press enter after your moves to let the computer take its turn.
Good Luck and Have fun :)

DO NOT FORGET THE ORDER OF THE NUMBERS IN THE KEY BELOW"""

instruc_list = [ '1','2','3','4','5','6','7','8','9' ]

print_board(instruc_list)
print ' '

# this prints the board

def print_board(order):
print " "
print " "
print " ",order[0], "|", order[1], "|", order[2]
print " -----------"
print " ",order[3], "|", order[4], "|", order[5]
print " -----------"
print " ",order[6], "|", order[7], "|", order[8]

# This is where player decides whether the will go first or second
# player is assigned X's if 'first' and O's if 'second'

def x_or_o():

print "\nWould you like to go first or second?\n"

answer = ""
choices = ('first', 'second')
while answer not in choices:
answer = raw_input("please enter 'first' or 'second'(enter here)")
if answer == "first":
print "\nYou will be X's"
if answer == "second":
print "\nHmmm good luck"
print "You will be O's"
return answer

# this asks for a move and checks if it is valid

def answer():
guess = raw_input("please enter your move(enter here)")
while guess not in ('1','2','3','4','5','6','7','8','9'):
guess = raw_input("please enter a number chosen from 1-9(enter here)")
guess = int(guess)
while True:
if position[guess -1] in ('X', 'O'):
guess = raw_input("that space is already occupied, please make another move(enter here)")
guess = int(guess)
else:
break
return guess -1

# tests the computers move

def comp_answer():

raw_input("computer turn - press enter:")

BEST_MOVES = ( 4,0,2,6,8,1,3,7,5 )
# this determines whether computer will win from move

for i in WINNER:
winns = []
if i[0] in comp_moves:
winns.append(i[0])
if i[1] in comp_moves:
winns.append(i[1])
if i[2] in comp_moves:
winns.append(i[2])
if len(winns) == 2:
for k in i:
if k not in winns and k not in moves and k not in comp_moves:
return k

#Works out if the user has to be blocked and
#blocks the user

for i in WINNER:
winns = []
if i[0] in moves:
winns.append(i[0])
if i[1] in moves:
winns.append(i[1])
if i[2] in moves:
winns.append(i[2])
if len(winns) == 2:
for k in i:
if k not in winns and k not in moves and k not in comp_moves:
return k

# this allows the computer to start with the middle,
# then corners, and then moves to the four sides.

for i in BEST_MOVES:
if i not in moves and i not in comp_moves:
return i

# this allows user input
#provides winner function and stdin

def update_moves(answer):
if who_first == 'first':
position[answer] = 'X'
else:
position[answer] = 'O'
moves.append(answer)

#updating the computer moves

def comp_update_moves(comp_answer):
if who_first == 'first':
position[comp_answer] = 'O'
else:
position[comp_answer] = 'X'
comp_moves.append(comp_answer)

#checking to see if there is a winner

def winner(moves,comp_moves):

won = ""

for i in WINNER:
if i[0] in moves and i[1] in moves and i[2] in moves:
won = 0

for i in WINNER:
if i[0] in comp_moves and i[1] in comp_moves and i[2] in comp_moves:
won = 0
return won

#works out if winner s

def congrat(moves):
WINNER = ( (0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7),
(2,5,8), (0,4,8), (2,4,6) )

won = 1
for i in WINNER:
if i[0] in moves and i[1] in moves and i[2] in moves:
won = 0
return won

WINNER = ( (0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7),
(2,5,8), (0,4,8), (2,4,6) )
position = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
moves = []
comp_moves = []
who_first = ""
who_won = 1

instructions()
raw_input('press return to continue')

while  winner(moves,comp_moves) != 0:

# If game is a tie
if len(moves) + len(comp_moves) == 9:
who_won = 0
break

# this starts either X's or O's
# takes first move from either user or computer.

if who_first == "":
who_first = x_or_o()
if who_first == 'first':
print_board(position)
update_moves(answer())
next_turn = 0
else:
print_board(position)
comp_update_moves(comp_answer())
next_turn = 1

print_board(position)
if next_turn == 0:
comp_update_moves(comp_answer())
next_turn = 1
else:
update_moves(answer())
next_turn = 0

print_board(position)

if who_won == 0:
print "\nTIE!! Try again next time!"
elif congrat(moves) == 0:
print "\nCongratulations!! You beat the computer."
else:
print "\nSorry You lost :( better luck next time."``````

Hey again guys, thanks fort he previous help.
So far in TK i have made a frame and labels
I have it displaying what I have in the labels properly.
How can I make it so I can get the actual game inside the frame??

``````# import all the Tkinter methods
from Tkinter import *

# create a window frame
frame1 = Tk()
# create a label
label1 = Label(frame1, text="Hello, world! How are you doing today?")
label2 = Label(frame1, text= "hi smiley")
# pack the label into the window frame
label2.pack()
label1.pack()
frame1.mainloop()
# run the event-loop/program``````

This is what I have. Any help would be muchly appreciated.

If you are looking for alternating colors in a checkerboard fashion, use bg & fg + the color. You might also want to check the canvas widget. It may be overkill though http://infohost.nmt.edu/tcc/help/pubs/tkinter/create_rectangle.html

``````# import all the Tkinter methods
from Tkinter import *

# create a window frame
frame1 = Tk()
# create a label
label1 = Label(frame1, bg="white", fg="black", \
text="Hello, world! How are you doing today?")
label2 = Label(frame1, bg="black", fg="white", text= "hi smiley")
# pack the label into the window frame
label2.pack(side="left")
label1.pack(side="left")
frame1.mainloop()
# run the event-loop/program``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.