Simple Qt Web Browser

Source code of a simple web browser implemented with PySide/PyQt. Before running the code, make sure the proper packages are installed. If you don't know which one to choose, use PySide 6.

pip install pyside6

Preview:

/images/simple-qt-web-browser/simple-qt-web-browser-preview.png

Read more…

Listbox in Tk (tkinter)

The listbox (Listbox) is a Tcl/Tk widget that allows you to display a list of strings that can be selected by the user.

/images/listbox-in-tk-tkinter/tkinter-listbox.png

Listboxes do not support icons or columns. If you need a table-like widget use the ttk.Treeview class instead.

Read more…

Window Icon in Tk (tkinter)

Both main windows (created via the tk.Tk class) and child windows (tk.Toplevel) have the Tcl/Tk logo icon by default. Setting custom window icons will give our application a more professional look. A window icon is also often displayed by the operating system on the taskbar or application bar. In this post we will see the various ways for configuring window icons on the different platforms supported by Tk (Windows, Linux and Mac).

Tk window objects provide three methods to set an icon:

  • iconbitmap()

  • iconmask()

  • iconphoto()

Read more…

Extract Icon From Executable File (Windows)

The Windows API provides the ExtractIconExW function to extract icons from executable files (.exe) and from the icon files themselves (.ico). Inside a .ico file, whether it is included in an executable or not, there are often multiple images of various sizes: 16x16, 32x32, 48x48, etc. Icons that Microsoft calls "small" (usually 16x16) are generally the ones that appear in windows and the file explorer, while "large" icons (usually 32x32) are shown in the taskbar and when the user presses Alt + Tab to switch between applications.

If you want to call ExtractIconExW from Python to get the small or large icon (the function only supports these two sizes) of an executable file, the optimal solution should be pywin32, which provides a pythonic interface around the Windows API, intended to be consumed from C. Unfortunately, pywin32 does not implement the GetDIBits function, necessary to read the content (= the bitmap, each pixel) of an icon from a HICON, which is what we get from ExtractIconExW. Thus, we must make use of the old reliable, low-level ctypes standard module to access the Windows API.

Read more…

Create Executable File With PyInstaller, cx_Freeze, py2exe

Python is an interpreted language. This implies that users of our console or desktop applications (for example, those developed with tkinter, PyQt/PySide, wxPython, etc.) must have Python installed to run .py files. However, there are multiple tools that allow us to create executable files (.exe, .app, etc.) for Windows, Linux and Mac systems, so that our application can be executed without requiring the installation of Python. This makes it much easier to distribute our programs and improves user experience.

To get you started quickly, in this article we will briefly introduce the three main tools for creating executables and how to use them: PyInstaller, cx_Freeze and py2exe (the latter only for Windows). If you don't know which one to choose, just use PyInstaller.

Read more…

PyAutoGUI - Cross-Platform Automation Module

PyAutoGUI is a Python module for automating tasks on Windows, Linux and MacOS. "To automate" is generally understood as controlling the mouse and keyboard, although in this particular case other tools are included, such as dialogs and screenshots.

The module API is quite simple, as we'll see. Unfortunately, PyAutoGUI functions do not follow the naming convention recommended by the PEP 8 – Style Guide for Python Code.

Read more…

String Methods

The str data type is a built-in class whose instances include more than 30 methods for parsing, transforming, splitting and joining its content. Here is a quick reference to learn the most used string methods.

Parsing

The count() method returns the number of times the specified set of characters appears within the string.

>>> s = "Hello world"
>>> s.count("Hello")
1
>>> s.count("o")
2
>>> s.count("x")
0

Read more…

Send Email via Outlook and SMTP

Microsoft's email service, Outlook (formerly Hotmail), allows application developers to send emails through the SMTP protocol. This is what you need to know about Outlook's SMTP server:

  • Domain: smtp-mail.outlook.com

  • Port: 587

  • Security protocol: TLS

With this information and the help of the standard email and smtplib modules, we can use the following code to send an email from an Outlook account (it also accepts the old @hotmail.com, @live.com, etc.):

Read more…

Send Email via Gmail and SMTP

In our previous post Send HTML Email With Attachments via SMTP we explained how to build a message and send it through an SMTP server in Python. Google's email provider, Gmail, fully supports the SMTP protocol at the smtp.gmail.com address. However, trying to run the following test code to send an email via Gmail from Python will result in an authentication error:

from email.message import EmailMessage
import smtplib
sender = "sender@gmail.com"
recipient = "recipient@example.com"
message = "Hello world!"
email = EmailMessage()
email["From"] = sender
email["To"] = recipient
email["Subject"] = "Sent from Python!"
email.set_content(message)
smtp = smtplib.SMTP_SSL("smtp.gmail.com")
smtp.login(sender, "gmail_password_123")
smtp.sendmail(sender, recipient, email.as_string())
smtp.quit()

Read more…