Create a New Window in Tk (tkinter)

Desktop applications can be made up of more than one window. The main window is created by the tk.Tk class and controls the life cycle of the application. Secondary windows, also known as popup or child windows, might be created on application startup or in response to an event (for example, a button press) via the tk.Toplevel class. When the user closes the main window, secondary windows are also closed, thus ending the program execution. However, child windows can be opened and closed multiple times during the application life cycle.

Read more…

The Python Import System

Python includes an import system that let us:

  • organize large code into small reusable pieces;

  • share code with other people and, conversely, use other people's code.

There are two main concepts in the Python import system: module and package. A module is a Python file (usually ending with .py, sometimes .pyd if it is an compiled module). A package is a folder that contains modules or other packages inside. Despite these definitions, pythonists often use the terms "module" and "package" interchangeably, and there isn't much of a problem with that. Generally speaking, anything that can be imported (we will now see what does that mean) is a module.

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.

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()
# Place it within the window., y=50)

This code creates the following interface:


Read more…

Executing Code Every Certain Time

In this post we will see how to schedule a function to run in the background every certain period of time. In other languages this is usually implemented using a Timer class. The Python standard library ships with the threading.Timer class, but it only allows you to execute code once, after a certain time has elapsed.

import threading
def f():
    print("Hello world!")
# Run the function after 3 seconds.
t = threading.Timer(3, f)
print("This runs before the f() function.")

Read more…

Detecting Changes in the File System in Real Time with Watchdog


(The source code for this program is at the bottom.)

Watchdog is a cross-platform Python library that allows you to monitor file system events in real time. It is very useful for automating tasks if we want our program to execute an operation when a file is modified, deleted, moved, etc. Let's see how it works by creating a simple program that logs events for files in a folder.

Read more…

Installing and Using pip on Windows, Linux and macOS

pip is a tool written in Python to facilitate the download and installation of packages hosted in the Python Package Index (PyPI). It runs on all major platforms: Microsoft Windows, Linux and macOS; CPython 2.6+, 3.x and PyPy.

Before using pip, make sure it is installed. Open the terminal and run:

$ python -m pip --version

Windows users might also use:

> py -m pip --version

If you get no error, then jump directly to the last section to learn how to use this tool to download, install and upgrade packages. Otherwise continue reading.

Read more…

Message Box in Tk (tkinter)

A message box is a window with a title, a message, an icon, and one or more buttons. It is used to inform the user about something or encourage him to take an action.

The functions for creating message boxes in a Tcl/Tk application are defined within the tkinter.messagebox standard module (tkMessageBox in Python 2), and are the following:

  • showinfo()

  • showwarning()

  • showerror()

  • askquestion()

  • askyesno()

  • askokcancel()

  • askyesnocancel()

  • askretrycancel()

Read more…

Button in Tk (tkinter)

The button is typically the most common widget in a GUI application. A button is a box with a text and/or an image that can be pressed by the user to execute an operation. In Tk it is represented by the tk.Button and ttk.Button (which has a modern style) classes. From the point of view of the code its use is quite simple.

Like any other Tk widget, to get a button we must create an instance of the proper class:

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.config(width=300, height=200)
root.title("Button in Tk")
button = ttk.Button(text="Hello world!"), y=50)

Read more…