1 Video View·Apr 22, 2025
In this video we continue with the Python graphical user interface module known as Tkinter, and building an application for the classic Tic Tac Toe game!
This is Episode 5 in this series for developing an in depth fully functioning practical game using the tkinter and python modules with pycharm as our IDE. This episode covers resizing buttons and other widgets to span multiple columns or rows, we adjust grid settings and change weighting and indexing for different results, and we cover the 'sticky' method of filling all available space for buttons or labels!
Episode 4 covers handling game over situations when the player wins or loses and reports the winner onto the screen! This completes the necessary functionality for a fully playable tic tac toe game!
Episode 3 covers writing the code for a computer opponent, or AI, to play against. This includes randomly selecting a position, checking to see if it is available, and writing the computer's symbol onto the screen when possible. This also covers tracking in a turn based system, and resetting the game when the max turns is met!
The 2nd episode covers creating a game board with the nine move squares available and creating a universal function that can handle inputs from any button to update with the symbol of whichever player just moved! The first episode covered widgets, labels, buttons, grid layouts, the pack method, and updating formatting and style for the app!
If you have any questions let us know about it in the comments and thanks for watching! If you found this or anything else on the channel helpful please subscribe to our channel and leave us a like on the video!
https://www.youtube.com/channel/UCV5Ab39YnXvTZ6Grar9URxQ
Good luck with you code and thanks for watching!
what's going on you guys welcome back to
the channel and today's video will be
the fifth and probably final video in
our using kinter to build a tic-tac-toe
uh gui
um in python so if you missed the
previous four we built
this tic-tac-toe app where you select
whether to play as x's or o's and then
you hit start
and then you can go through and play
tic-tac-toe the computer kind of
randomly picks an open space to play
against and then you can reset and play
again
um
but uh obviously just looking at it here
the buttons are all weird sizes this
isn't really a board you would ever want
to like play in and it's not very
elegant to look at so this video is
really going to be taking a look at
dynamically resizing buttons and your
different options within kinter
for
button and widget sizing so without any
further ado we'll dive into it if you do
find this or anything else on the
channel useful i really appreciate like
on the video subscribe to the channel it
helps me out a ton as we're getting
going and be sure to check out the uh
tons of other videos on the channel and
see if any of that stuff gets interested
so without any further ado let's get
into it um and for this episode like i
said we're going to take a look at
resizing things in kinder and it's a
little bit weird if you haven't
specifically built guise with this
software before
um you do have to do things to the
individual widgets that you want to
change the size of
but if you're using the grid method to
draw your
uh objects into the screen you actually
also have to
modify some settings at the grid level
so that's what we'll start with here
there's really two things there's grid
row configure and there's grid column
configure and you can probably figure
out the difference between those two
without me telling you
but in here there's a couple things you
have to do you have to
say where you want to draw on so all of
our widgets are on the root
screen
and then you give it what's called an
index
so if you're doing row configure this is
what row you'll be looking at and we're
going to start by looking at the actual
game
square buttons so down where you select
x's or o's and play the game those are
what we're going to be looking at so
those are in rows 5 6 and 7.
and then the next thing you're giving it
is a weight so uh
this value really just needs to be
greater than zero you're saying like pay
attention to it and then um what you put
in here relative to others will matter
because it's it's kind of dynamic
but in general i usually actually just
give them all one
for weight unless i'm trying to do
something really fancy so
we're going to go ahead and
assign weight to all of the rows that
have the buttons in them and then we're
gonna give all three columns uh
weight as well here
because
those uh buttons are in all three
columns of our grid so
you could kind of do this as well with a
for loop if you want to that'll probably
be an elegant way of doing it
i'm just doing it kind of quick here and
then we're going to come down to where
we
draw those buttons onto the screen
excuse me
and we're going to use something new uh
this is why we had to assign weight
because when we draw it onto the screen
and we use the dot grid we're going to
use something new called sticky
and you can give it cardinal directions
northeast south or west
and since this is getting a little bit
long
i'm just going to go ahead and make it
its own so this is a little bit easier
to read right we talked about in earlier
videos
when you create a widget if you're just
getting too long like a run online you
can separate out the piece where you dot
grid draw it onto the screen from
defining the widget
and that'll kind of clean things up
so we do not need the new line there
and what i just did with this nesw is i
said that in all four directions expand
to fill the space that you're in so for
column zero uh row five now that top
left button is going to be spanning the
maximum available space for it so if we
run that
yeah you can see
we all the other buttons just kind of
stay their size but this top left one
that we just defined changes size
dynamically
so
that's pretty cool but it's not super
useful until we do that to all of them
so let's go ahead and take just this
same
sticky
and we're going to make all of the
buttons in this system uh
sticky in all four directions
and i am going to
uh
i'm gonna leave them all on the same row
you can see like it's just a little
easier to look at the one that i
separated into two but since i showed it
once i'm going to go ahead and leave
them all in here because it's faster for
the video
but i would probably recommend going
through and doing a dot grid for all of
those in the future
and then the other thing is when we draw
the new buttons onto the screen those
also have to be sticky because we are
overwriting we're basically replacing a
button that was there before and so when
the computer or the player
takes a turn we need that sticky as well
in all directions so let's go ahead and
take a look there
yeah so you can see this is better
and when we expand it the buttons uh
fill the screen which is nice so we're
going in the right direction but you can
tell we got to do something with this
top section right because the first
column is just wider because of all the
labels so the next thing we're going to
take a look at
is different from sticky and setting
grid conditions this is going to be
using the column span function and uh
this is you can use this on any widget
um just like that sticky but what you
want to do is in the grid area um let's
go ahead and find that first label
where it says welcome to tic-tac-toe
so we draw that on the screen down here
and instead of doing a sticky condition
we're going to do column span
and we're going to make it span all
three columns and what you'll see as
soon as we run this is now it's directly
in the center because even though it's
in the top left technically it's in row
zero column zero
we told it column span
of three so if you've used excel it's
like using merge to kind of join all of
these columns together and center it so
we're going to do that for
all the labels that are supposed to take
up the whole screen so then the one
where we tell the player
select a character to play as and for
the buttons that are in row 2 here we're
actually going to make those
sticky
and we'll only do east west for those
because we don't care so much that
they're filling the space north and
south
but you can see this is in now row two
so we do have to go back up um where we
defined the weights and we have to add
row two to our grid weighting
so let's go ahead and do that
and i know i'm covering a lot and i know
i'm going kind of fast so if you have
any questions about anything you see in
this video today be sure to let me know
about in the comments i'll try to get
back to you as soon as possible
but for now i'm also going to go through
and
make any label that is supposed to take
up the entire
bottom or the entire width of the screen
i'm going to make them all
uh column span three so let's see those
are the winners
there's you have selected
we can make that one column span three
as well we can make the start button
those are all that are on
at that time
there we go
and let's see anything else we should
widen
let's go ahead and run it and just take
a look here so uh right away that x and
o i don't love that o
um
i don't love that o in the middle with a
space to the right so i'm actually gonna
move my o button into column two
just a little bit of symmetry i think it
looks a little bit better like that
okay so you can see it's it's actually
all um centered pretty nicely and it all
resizes really well so it all kind of
sticks where you'd want it to stick and
then we hit start and we finally get
like a decent looking game board
i think because we made this label a
column span 3 now we should actually
move it
the winning labels
we should move them back to
column zero but where do we
where we write those
that's in check game over i think
yeah so uh when we say the winner
because we have it spanning across the
whole screen let's draw that in column
zero
and just one more thing i noticed it
doesn't clear out when we draw the board
initially um
so i'm going to go ahead into that
drawboard function
yep
and just down at the bottom i'm going to
create a label
that's just totally blanked out and so
when we do reset and play a new game
it's gonna cover up the whoever won
so here we go let's see if we can get
the computer to win here we go
o wins let's uh start again let's change
to play as o now
and let's try to get them to win again
x wins nice
and you can just see right quick like
it's a rapidly replayable game
it finally looks pretty good it resizes
really well
i
might do another video that's just
purely aesthetic upgrades like um
changing the text and bold and changing
font sizes and background colors and
foreground colors
but i'm not sure if i'm gonna do that or
not if that's something you'd like to
see be sure to let me know about in the
comments below and if i get some
interest for it i'll make another one on
this otherwise i'll probably move on to
another series hopefully you found this
introduction to kinter and tic-tac-toe
and python hope you found it all useful
if you have any recommendations for how
i can improve the channel or anything
you'd like to see in the future be sure
to let me know about in the comments and
if you did like this i really appreciate
a like on the video subscribe to the
channel helps me out a ton and as always
thanks for watching and good luck with
your projects thanks bye