Background Tasks with PyQt

Every library for desktop applications development works with a main loop that manages events such as displaying, moving, or resizing the window, responding to a button or keyboard press, or any other interaction with the user interface. Some of those events might be connected to our own functions; for example, a button1_pressed() method might be invoked by a library when the user presses the button1 widget. When working with Qt, the proper way to respond to those events is by connecting a signal to a slot.

But the problem arises when, while responding to a certain event or during the interface setup, we execute a task whose duration is not negligible (we could say that any task that lasts for more than one second is not negligible). When this happens, the processor is busy executing our heavy task and is not able to execute our application's main loop, thus our interface stops responding: we cannot move it, close it, resize it, nor interact with it in any other way.

Read more…

The None Datatype

Python is a dynamic-typed language, which means that it is not necessary to indicate the type of a variable when creating it, nor is mandatory for a variable to keep its datatype during the program execution. A variable might be created as an integer, then changed to a string, then to a floating point, etc. However, every variable in a program has some datatype (or, more precisely, every variable is an instance of some class, since everything in Python is an object) which can be known via the type() built-in function:

>>> pi = 3.14
>>> type(pi)
<class 'float'>

Read more…

Placing Widgets in Tk (tkinter)

Tk provides three methods to position widgets within a window, which are represented in code by the pack(), place() and grid() functions. They differ in versatility and restrictiveness, so which one you should use it will depend on the result you want to achieve. Let's make an approach to each one of them and see how they work. It is worth noting from the begining that these methods should not be mixed in a single window.

Read more…

'python' is not recognized as an internal or external command

'python' is not recognized as an internal or external command, operable program or batch file. When this error happens while typing python in the Windows terminal (or command prompt), then you first need to ensure that Python is installed. For this you can press or click the Start button (the one with the Windows logo) and then type "python" (without quotes). On Windows 8, look for the search button at the top-right corner of the screen.


If you don't see something similar to the previous image, then you don't have Python installed. Download the latest version from and when running the installer make sure to check the "Add Python 3.x to PATH" option. But if you do have Python installed, and nevertheless the python command is not working in the terminal, then keep reading.

Read more…

Background Tasks with Tk (tkinter)

During the development of a desktop application with the tkinter standard module, it is usual to reach the situation where a heavy operation (i. e. a task which lasts at least two or three seconds) freezes our window and every widget within it. When this happens, the user is not allowed to interact with our application anymore, nor can our code make changes to the interface (like increasing the value of a progress bar). This might be the case, for example, when we try to download a file via HTTP, to open a big file, to send an email via SMTP, to execute a command via subprocess, etc.

Read more…

Tetris with PyGame

This time we present the source code of a simple implementation of the classic Tetris game using the PyGame library for 2D game development. The program has less than 500 lines, but we admit it was not an easy task as it seemed a priori. Although the game is very simple, its development is a meticolous task that requires patience.


Read more…

Textbox (Entry) in Tk (tkinter)

A textbox lets the user enter any one-line text. In Tk it is represented by the ttk.Entry class, which inherits the features of a more primitive widget called tk.Entry.

Widget creation

To create a textbox let's first create an instance of the ttk.Entry class.

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.config(width=300, height=200)
# Create the textbox.
entry = ttk.Entry(root)
# Place it within the window., y=50)

This code creates the following interface:


As with all widgets, the first argument indicates the parent control. In this case, the parent is the root window, which is also the default value, so it could be omitted.

Read more…

Send File via Socket

The following code establishes a client-server connection between two Python programs via the socket standard module and sends a file from the client to the server. The file transfer logic is contained in two functions: the client defines a send_file() function to send a file through a socket and inversely the server defines receive_file() to receive it. This way the functions can be easily moved from this post to other programs that already implement a client-server connection. The code is ready to send any file format and size. Python 3.8 or greater is required.

Read more…