Tk after() Function (tkinter)

The tkinter.Tk.after() function allows you to program the execution of your own function so that it is executed after a certain amount of time. However, it can also be used to tell Tk to execute a function every a given amount of time, which is the most common usage. Let's start by creating a simple window with a label (tk.Label):

import tkinter as tk
root = tk.Tk()
root.title("Tk after() Example")
root.config(width=400, height=300)
label1 = tk.Label(text="Hello world!")
label1.place(x=100, y=70)
root.mainloop()

Read more…

Sets

Sets are unordered collections of unique objects. In Python a set is a built-in data type, like other more common collections, such as lists, tuples and dictionaries. Sets are widely used in logic and mathematics. In Python we can take advantage of their properties to create shorter, more efficient, and more readable code.

To create a set we put its elements between braces:

s = {1, 2, 3, 4}

Read more…

Generating Prime Numbers

How to know if a number is prime? How to get a list of prime numbers in Python? In this post we analyze and provide a simple and efficient method. If you want to skip the explanation and just copy the function for generating prime numbers, the full code is at the bottom.

We are going to build two functions. The first one aims to obtain all the prime numbers from 2 to a certain natural number and return them in a list. The second tells us if a supplied number is prime by calling the above function and checking if it is found in the list.

Read more…