Creating a TIC TAC TOE game using Python and Tkinter | Samarth Paboowal | Skillshare

Creating a TIC TAC TOE game using Python and Tkinter

Samarth Paboowal, iOS Developer

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
1 Lessons (20m)
    • 1. Creating a Tic tac toe game.

      19:38

About This Class

This class is for those who have a little experience in Python Programming Language.

In this class we will see how we can use a module called "Tkinter" in Python to build GUI (Graphical User Interface) applications. By using this module we will create a very famous game called "Tic Tac Toe".

Transcripts

1. Creating a Tic tac toe game.: welcome to this tutorial on In this tutorial, we will build a tic tac toe game using pattern and TK Enter so Dechaine terrorism modeling Patten, which is used to build G Y applications, which basically means graphical user interface. So a sample image off a game called Tick tack toe. In this game there are two different symbols used. The 1st 1 is zero and the other one is cross, so a user wins the game. If anyone symbol is present in three different boxes together, which basically means suppose. In this game you can see that there are three zeros placed on the side by side. So this basically means you can see that these three zeros are placed diagonally. So in this case, a user wins. There are many different conditions in visual user can then ah, symbol can be placed in three different boxes together, either horizontally, vertically or diagonally. So in this example, you can see that zeros are place diagonally. So now let's see how we can build this using beytin antica entered. So here I have the whole court, which will be used to will their tic tac toe game. So let me explain this to you one by one. So on the first line, I'm importing every class and function from the mortal call TK Enter. So I'm simply using the key word from decay. Nder import style here as stick means that I'm importing everything that Desprez isn't inside the decay intermodal. So in the second line, I'm using import Dechaine turn dark message box. So this is a simple message box which is used to display a message that user excessive in on the user. Zero has won the game. So this is a simple message box that you see in each and every G way application on the tire line. I'm creating a deka object so that I can use it to build my whole G way application. So now I am giving the title tick tack toe to my application. This can be done by using the title method present inside the tiki class. So I can simply so I can simply use ticket or title and in the parentis is I can pass the name that I want to give my application. So now I will tell you how we can build different nine buttons on the DK Inter box. So these nine Burtons are basically this which contains nine different values. So here you can see that on the first row there are three columns and second of three columns and also in the third row data three columns toe In total, there will be nine different boxes. So let's see how we can create this using, biting and dig into. So if it if I scroll down in the court, you can see that let me zoom discord a bit. So on the line 40 you can see that I have a very well call button. This basically means I am creating a very well which will store the button number one on the screen. So I'm using a keyword call button which is present inside the Decatur model. So the first argument it takes his d k, which is the object which re created here on the line number tree. So it is sticking that object, which basically means that I want to create the button inside this decaying the window. Now, the second argument is text, which means what will be done before text dispute on the button. So I'm saying that the full text will be a single space, which means nothing is. Nothing will be printed on the bottom. Now the tired argument is deformed, which means that board will be the front of the text and actually present on the bottom. So I'm simply using Attack our Foreign Court Times 26 which is the size and board now. The fourth argument is height, which means I want the height of the butter into before and the weight of double turn Toby it. Now the last argument with the Barton keyword takes is combined. This command means the never click on the button, which function will be done. So I am using Iraqi work, or Lambda, which is basically used to call a function. So in this you can see that I'm calling a function Cory checker. It means that whenever I press the button number one, it will Orton automatically call the function called Checker. And inside this I'm passing the are given by 10 1 which is itself the variable that we have created we were seeing in a bit. Harvey will build Assumption Gold Checker toe make the functionality off detector game. So now on the line 42 you can see that I have used something called Button one dot grade, which basically means Harvey will place the button on our TK inter window so you can see that I am placing the button on my Row zero and column zero, which basically means this first position. Here. You can see that very zero is president, so this basically means rose. Zero column zero and this is the Rose zero column one and rose zero column two and so on. So here you can see that I am giving Rose zero and column zero because this is the first button ritually present on our peeking in the window. Now the third argument which I'm giving is sticky, which means that it will plays a Vigen on the button, which means that it will be stretched on all the four sides because I have given four different arguments. So as sir means out and not e east NW vest. It means that the bartender to be replacing, which will be stressed and will replace near to the bone near to the boundary on all the four sides. So this is basically how we creator single Britain in quite an antique enter. Similarly, we have to create a different a different buttons for building our game. So you can see that I have been created a very well call button to which will before the second button and all those things are seen which basically means a foreign tight and right Because we weren't all of the boxes to go the same height, rich and deformed on the text should be seen. And the function which will call will also be seem only the argument really were different because we want different functionality when you click different button. So similarly No, I'm doing but tend to door grade and here you can see that their differences I'm giving rose zero because it is in the same room by the column is one because now we're creating the button in the place off X, which you considered here there this is row zero and go along with. Similarly, we will create seven different buttons. So you consider that I have returned the court for Burton three. This is for the button four. And now this is for the button. Five. This is for Burton six, but on seven. But an eight and Britain nine. So now you can easily create nine different burdens on our ticket into windows. So now you concede it on the last line. I have used some tingle, decayed out mean look, which basically means that it will automatically refresh our doing Application sold that it will look very clean whenever we played this game. So whenever you were using Arctic Inter Library than it is must to use decay dot mean lou at the end of the court so that our joy application is automatically refreshed. Time for time. This time is very, very low. It cannot be seeing the tower I, but it refreshes automatically very fast. So this was about how we create nine different buttons or you can see boxes on our decaying the window. Now we will see and build the functionality of the off the buttons so that we can play our game and make the user of in. So if you go up, you can see that I have created a variable card click just a simple variable and assigned the value, which is a Boolean value called true. So now I am creating a function called jacker recall that I have positive function checker in all the buttons. So now we will build the functionality off the buttons inside this function, called Checker. This is simply taking in one argument call buttons, which can either be button one button, too, and do better. Nine. So I'm simply on the line line. I'm simply defining a the very well click Toby Global, which basically means that it can be accessed in any part, any part of our program. So now we create our first if condition. So this is our first if condition. It means that if Burton's text it means if the text inside the Burton is equal to an empty space and the very well click is assigned true, then we will assign the button text to be X, and then the very well click will be changed to fall, which basically means whenever we click the Empty Burton because here is in space, which means the bottom is empty, so our Burton will be replaced by Symbolical X and then the value off a click Very well, with the genes to falls and in the LF condition, we can see that if the button is empty and the click is falls. Then our button text will be jeans to zero, and the click very well will be automatically meetings to True. So you can see that this Gord do this is that whenever the first click double turn, it will place X on the button and on the second time you click the button, it'll play zero. So basically it means that on every alternate time it will print X and zero on the button. So if you take all values, which is the first clicked hard click and fifth fifth pelicans one, it will place X on the screen. And if we take even values, which basically means second click Ford click and so on it will place zero on the screen. So now we will building another LF condition, which will be used to declare whether a user has born the game. So there can be a different conditions for X in which a user can win the game. So the first condition is when the button one to entry are X. So if we see our diagram so you can see that on the line number one If this is X disses X and Disses X, which basically means that the whole first line is X the user will win. And also if the whole second line is X, then also the user will win the game and similar is the case for the hardline. So these are three different condition in which a user, Camden and the fourth condition is the vertical condition in which the first block, this is the fourth block and this is just seven flock. So if these all Rx then also user when similarly this is the case five in with the user has X in block number 25 and eight and the six condition is when there is X in three, six and nine block. So these are six different conditions. Now we will see what are the seven and eight condition. So the seven condition is already there. Present in the game, you can see dead the right Dagnall. There are three different zeros and there can also be three different X so the user will win. So the eight condition will be a left Dagnall which you concede that which is the block number three, Block number five and block number seven So these are basically ah, a different conditions by which a user can win the game so you can see that on our first condition, I'm giving that button when equal to expert into equal to X and but country equal to X. So this basically means that if the first line is filled with three different X, then the user will win the games, man. No, but our condition. I'm giving the second condition to win the game, which basically means the button number four, but a number five and butter number six will be a X. The toward condition is but a number seven. It and nine will be an X. So these are the three different conditions to win a game horizontally. Now we will see the three different conditions to win the game vertically. So these are the conditions you consider button one equal to X, but on four, equal to x and button seven equal to X. So this is the first vertical line. If there are three external user 11 the second vertical line is, but I do Burton five and Burton it and the third vertical winning conditioners Button three , Button six and but online. So now we will look at the two weather conditions which basically means that to diagnose. So the first Dagnall is the right Dagnall. So you can see that I've given the condition. But then when equal to X but in five and button name. So in the diagram you can see that this is the button one. This is the Burton five and this is the Burton Lane's. Now we will see the condition for the left Dagnall. So here you can see that the condition is button number three, Britain number five in Britain, number seven. So in the diagram, this is the button number seven, this is the button number five and this is but the number seven. So these are the eight different conditions which will be used to win the game. So you consider it. I'm placing the condition by using an or which basically means this left part will be run whenever anyone off this condition will be true. So if one of the condition out of this nine condition is true, then the user X will win so disk, and either with this whole condition, one the whole condition, too, and similarly, the ninth condition. So now you can see that after this I'm simply displaying a message called take introduced message Bogdan So info. So this is base. This is basically use with the message box to display a message on the screen. So the message will leave in their ex. You have just one again. Similarly, we have to create another LF condition for the zero all the things will be same inside this live box only We have to replace X with zero because the winning conditions will always be seen for forex or foreign A zero. So we have to similar leaps. Copy and paste up, of course, and leopard and replace the X the 20 and inside the last message box. To involve you have to just replace winner zero. You have just one again. So this was the simple gored to build a tic tac toe game using fightin. Now I will save the file and I will run the program and see how does it look? So in our command, prompt If you're using windows and if you are on Mac or line excellent, you can simply open a terminal by pressing control all tea and it will open the terminal and you have to write the command to run the program. So first I will move to my like my directory. We high we re alive present. We're discord. So now I will run my fight in court so I will simply do beytin. And by giving a space, I will write my program name and pressed. Enter so you can see that it has opened a window which is a G y window which is built using the model Kartik Enter on. You can see here the title is Decatur, which we have specified on the line number four Tick tack toe. So now we will see how we can play the game so you can see their data. Nine different boxes. This is First, there's a second. This is third and so on. So you lived. You remember from the core debt. For every old button click, it's it will place X on the screen. So the first click is always art because it is one. So you can if you click here, you can see that it has plays one on the screen. Now the for the for the even blink. It should play zero on the screen. So let's you can see that it is strange. Zero on the screen now third chance will re off X So let's a re click here. You can see that it has displayed X on the screen. Now the fortunes will you off zero. So let's please. Here you can see that it is zero. No, the fifteens will be often X. So if the click here, you can see that it has displayed the message Gold Winner X You have just won again. This is because it'll just satisfies our condition. Number seven which basically means that they're treated three. Which means there there are three X present on the right Dagnall. So you can see that the condition is here for the X. You can see there the button number one equal to expert in number five, equal to X and better number nine equal to X. So if we open our game, you can see that this is the Burton one and it contains the value X. This is the Burton five, which contains the value X, and this is but a number nine which contain the value X So let's close this and now attends the game again for a value zero. Which means that whether the game is working correctly when the user reaches euro wins or not. So this is X. This is zero. This is X zero X and zero. So you can see that it displayed that winner. Zero. You have just won a game. So this is the second vertical condition invested a block number two block number five and block number eight has the same value. So this is our basic detector game with the bill using fightin antique Enter. If you have any questions regarding this all you want any explanation about the score, then you can access you. You can ask me in the discussion. Thanks for watching.