DEV Community 👩‍💻👨‍💻

natamacm
natamacm

Posted on

Tkinter with Python

Tkinter 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 PyQt and wxPython. 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:

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

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

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

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

Related links:

Top comments (0)

git push

Stop by this week's meme thread!