DEV Community

danides450
danides450

Posted on

Creating a GUI/Basic Window with TKINTER(Python)

Installing Tkinter

First, make sure that you have python installed, we recommend installing python 3.9+. Also make sure to install a text editor or a IDE, almost every operating system comes with a text editor, windows comes with notepad. You can also download popular IDE's like Visual Studio Code, Pycharm, Sublime Text.

After that, make sure you have installed tkinter. tkinter is automatically installed once you have installed python. But you can check to confirm you have it installed, there are two ways that you can do this, the first method is that you simply open cmd on windows, terminal on mac, and if you are on linux, you probably know how to open the terminal. After you have done that, type this following command:

pip3 install tkinter

If this command returns you with something like requirement already satisfied, then you have it installed if it gives you an error that pip3 is not a command than try typing this in to the terminal:

pip install tkinter

This should give you the same result as pip3, if this gives you the same error, than that means you have not installed pip, there is no need to install pip again just for the sake of this tutorial.

The second method to verify if tkinter is installed is to simply open a file and type:

from tkinter import *

If this does not show any error, that means that it is installed, if it does show a error, make sure to install it through pip or any other means.

Creating the window

To create the basic window, first type this command:

from tkinter import *

this will import the tkinter module.

then, after that type this command:

root = Tk()

This is important, as if you do not do this then, well, the whole code will not work

after that, type this command:

root.mainloop()

The end code should look like this:

from tkinter import *

root = Tk()

root.mainloop()

Make sure to put all the code inside the 'root = Tk()' and 'root.mainloop()'.

Now lets edit some more details.

Below root = Tk(), put this code:

root.geometry('400x400')

This is telling tkinter the size of the window, you can change this to whatever you want!

Then type this:

root.title('This is a windows')

This is teling tkinter what the name of the window should be.

the end result should look like this

from tkinter import *

root = Tk()

root.geometry('400x400')

root.title('This is a window')

root.mainloop()

and there you go! that is how to create a basic window in tkinter, hope this tutorial helped!

Top comments (0)