avatarEsteban Thilliez

Summary

This webpage is a tutorial on building user interfaces with Python using the Tkinter library, focusing on the Paned Window, Progress Bar, Notebook, Tree View, Canvas, and Cursor widgets.

Abstract

The tutorial is a continuation of a series on building user interfaces with Python using Tkinter. It covers several widgets, including Paned Window, Progress Bar, Notebook, Tree View, Canvas, and Cursor. The Paned Window allows for a slideable separator in a window containing widgets. The Progress Bar displays the status of running tasks, with options for determinate and indeterminate modes. The Notebook widget enables the creation of tabs, with options for hiding and removing tabs. The Tree View displays items in a tabular and hierarchical format, with customizable headers, columns, and events. The Canvas widget is used for drawing on the screen, with options for drawing lines, rectangles, ovals, and images. The Cursor widget allows for changing the cursor in the application or specific widgets.

Bullet points

  • The tutorial covers several Tkinter widgets, including Paned Window, Progress Bar, Notebook, Tree View, Canvas, and Cursor.
  • The Paned Window allows for a slideable separator in a window containing widgets.
  • The Progress Bar displays the status of running tasks, with options for determinate and indeterminate modes.
  • The Notebook widget enables the creation of tabs, with options for hiding and removing tabs.
  • The Tree View displays items in a tabular and hierarchical format, with customizable headers, columns, and events.
  • The Canvas widget is used for drawing on the screen, with options for drawing lines, rectangles, ovals, and images.
  • The Cursor widget allows for changing the cursor in the application or specific widgets.

How to Build User Interfaces with Python — Tkinter Widgets — Part. 3

Photo by Brett Jordan on Unsplash

This story follows the User Interfaces series. If you have missed the last Tkinter story, you can find it here:

If you’ve followed the series, you should now know a lot of widgets. But there are still a few remaining. We’ll deal with these in this story.

If you want, you can find the code of the story on GitHub. Else, the code we start with is:

Paned Window

A PanedWindow is a window containing widgets and including a slideable separator.

You can set its orient parameter to specify whether the PanedWindow should be horizontal or vertical.

Then, you can just add your widgets to the window, and they will stack in a way depending on your window’s orientation.

Progress Bar

A ProgressBar allows you to display a bar updating its status depending on the running of tasks.

You can specify several parameters:

  • orient
  • length : width or height of the bar, depending on its orientation.
  • mode : either determinate when the max duration is known, else indeterminate .
  • maximum : the maximum value of the bar.
  • value : the actual value of the bar.

When using the inderminate mode, the bar will bounce back and forth between the end of the widget. When using the determinate mode, the bar will be filled over time.

You can specify functions to control the bar. For example:

If you prefer to keep the default behavior of the bar, you can just use command=progress_bar.start .

Notebook

Maybe one of the most important widgets. It allows you to create tabs. You can specify parameters such as height or width to specify its dimensions.

To add a widget to a Notebook, you just use the notebook.add(widget, text="", image=None) method. You can specify the text to be displayed by the tab, and an image if you want.

You can also hide tabs using notebook.hide(id) . The first tab is the index 0, the second the index 1, etc…

Lastly, you can remove tabs using notebook.forget(id) or change the selected tab using notebook.select(id) .

Tree View

A TreeView is used to display several items in a tabular and hierarchical format.

Here are the parameters you can specify when initializing:

  • columns : the columns of the TreeView.
  • show : how data is displayed.

show accepts several values:

  • tree : only shows the first column.
  • headings : shows all the columns and the headings.
  • tree headings : only shows the first column and its heading.
  • "" : shows all the columns but no headings.

You can do many things with TreeViews, such as customizing headers, columns, inserting content, deleting content, binding events when an item is selected, etc…

Check the following code to see some examples of what is possible with TreeViews:

Canvas

A Canvas is a widget you can use to draw on the screen.

You can specify its width, its height, and its background color (using the bg parameter).

Here is an example of the different things you can draw in a Canvas:

You can also draw images using canvas.create_image :

canvas.create_image(100, 100, image=tk.PhotoImage(file="test.png"))

Cursor

You can change the cursor, either in the whole application or just for some widgets.

You do it using this:

widget.config(cursor=cursor)

Here is the whole list of cursors you can use:

As an example, let’s change our cursor when we click on the left mouse button in our canvas widget:

I can’t show it because my cursor disappears when I want to take a screenshot, but trust me, when you click on the canvas widget, the cursor changes.

Final Note

We’re done with the Tkinter widgets. But there are still some things we can see. We’ll also see some examples of applications you can build using Tkinter in the next stories.

So, be sure to follow me if you don’t want to miss these stories!

You can find the other stories of this series here:

To explore more of my Python stories, click here!

If you liked the story, don’t forget to clap and maybe follow me if you want to explore more of my content :)

You can also subscribe to me via email to be notified every time I publish a new story, just click here!

If you’re not subscribed to Medium yet and wish to support me or get access to all my stories, you can use my link:

Python
Software Development
User Interface
Programming
Coding
Recommended from ReadMedium