Tkinter with Python
Tags: python
Published: 2020-05-10 15:37:11

<a href="https://en.wikipedia.org/wiki/Tkinter">Tkinter</a> is a GUI library that you can use with Python. It can be used to make desktop apps with Python.

Tkinter isn't the only module available for this purpose, others are <a href="https://pythonprogramminglanguage.com/pyqt/">PyQt</a> and <a href="https://pythonspot.com/wxpython-buttons/">wxPython</a>. But it's a module that is straightforward to learn.

## Installation

The first thing to do is to install the tkinter module on your computer.

For Ubuntu users:
```
sudo apt-get install python3-tk
```

For Fedora users:
```
sudo dnf install python3-tkinter
```

You will see something like this:

```

=====================================================================================================================
 Package                         Architecture           Version                        Repository               Size
=====================================================================================================================
Installing:
 python3-tkinter                 x86_64                 3.7.7-1.fc31                   updates                 317 k

Transaction Summary
=====================================================================================================================
Install  1 Package

Total download size: 317 k
Installed size: 1.5 M
Is this ok [y/N]: y
```

## Using tkinter

You can use tkinter as a Python module to make desktop apps. It supports 
basic widgets like buttons, labels and images.

This program shows how to use labels:

```python
from  tkinter  import  *                                                                                             
                                                                                                                     
root = Tk()                                                                                                          
                                                                                                                     
Label(root,bg = 'red',fg = 'blue',text = " Python programming").pack()                                               
Label(root,bg = 'white',fg = 'red',text = "with dev.to").pack()                                                      
Label(root,bg = 'blue',fg = 'white',text = "and tkinter").pack()                                                     
                                                                                                                     
root.mainloop()    
```

Running it will show a window with 3 labels:

![tkinter label](https://dev-to-uploads.s3.amazonaws.com/i/kf6marf9d1u25uasq8iy.png)

The program below adds a label and an image to a window.

```python
from tkinter import *                                                                                                
                                                                                                                     
root = Tk()                                                                                                          
                                                                                                                     
textLabel = Label(root,                                                                                              
                  text = "Cat approves\nPython tkinter",                                                             
                  justify = LEFT,                                                                                    
                  padx = 10)                                                                                         
                                                                                                                     
textLabel.pack(side = LEFT)                                                                                          
                                                                                                                     
photo = PhotoImage(file='cat.gif')                                                                                   
imgLabel = Label(root,image=photo)                                                                                   
imgLabel.pack()                                                                                                      
                                                                                                                     
mainloop()    
```

![python tkinter](https://dev-to-uploads.s3.amazonaws.com/i/09uw9jlugfvjko6w5jay.png)

So you can make all kinds of apps with tkinter and Python.

**Related links:**
* <a href="https://pythonbasics.org/tkinter/">Tkinter tutorial</a>
* <a href="https://pythonprogramminglanguage.com/">Learn Python</a>