Here we are providing basic tutorial in Tkinter required by a beginner python developer. This is our first python GUI program.

First line of the code

from tkinter import *

There is no requirement of installing a seperate library or package if you want to use Tkinter, it is an in-built library in python. For these tutorials we have used Python 3.4. Above line of code must be included in every Tkinter program. By using this line, Tkinter library can be easily included or imported in a python program.

Structure of a program

Below section will give a rough idea of the structure of a Tkinter program

1) Create a GUI window

from tkinter import *
root = Tk()
Above lines of code creates an object namely root (can be any name), this object is used to store the created window object. Basically, above lines created a GUI window.

2) Create and add widgets to that window

from tkinter import *
root = Tk()
theLabel = Label(root, text = "Welcome to PythonGUI")
Above lines created a widget called as label that is being stored in the object theLabel (name can be anything). We used a predefined function Label that takes two parameters in above code i.e name of the window to which widget must be added and text to be stored in that label that has to be displayed on the window.

3) Place these widgets at required positions in the window

from tkinter import *
root = Tk()
theLabel = Label(root, text = "Welcome to PythonGUI")
theLabel.pack()
Above lines of code is used to place the widget i.e theLabel at a random position in the window. Function pack() will be discussed later in detail in Organizing layout

4) Run the window

from tkinter import *
root = Tk()
theLabel = Label(root, text = "Welcome to PythonGUI")
theLabel.pack()
root.mainloop()
To run this final code, mainloop() function is used that will display the window upto infinite time until the close button of the GUI window is clicked.

Output

output