Getting to know core Tkinter widgets

In this iteration, we will get to know all core Tkinter widgets. We have already seen two of them in the previous example—the Label and Button widgets. Let's now see all other core Tkinter widgets.

Prepare for Lift Off

Tkinter includes 21 core widgets. These are as follows:

Toplevel widget

Label widget

Button widget

Canvas widget

Checkbutton widget

Entry widget

Frame widget

LabelFrame widget

Listbox widget

Menu widget

Menubutton widget

Message widget

OptionMenu widget

PanedWindow widget

Radiobutton widget

Scale widget

Scrollbar widget

Spinbox widget

Text widget

Bitmap Class widget

Image Class widget

Let's write a program to include these widgets on our root window.

Engage Thrusters

The format for adding widgets is the same as we discussed in the previous task. To give you a flavor, here's some sample code for adding some common widgets:

Label(parent, text=" Enter your Password:")    
Button(parent, text="Search")
Checkbutton(parent, text='RememberMe', variable=v, value=True)
Entry(parent, width=30)
Radiobutton(parent, text=Male, variable=v, value=1)
Radiobutton(parent, text=Female, variable=v, value=2)
OptionMenu(parent, var, "Select Country", "USA", "UK", "India", Others")
Scrollbar(parent, orient=VERTICAL, command=mytext.yview)

Can you spot the pattern common to each widget? Can you spot the differences?

As a reminder, the syntax for adding a widget is:

Widget-name (its container window, *configuration options)

Tip

The method for creating all the previously mentioned widgets is the same. Most of the configuration options will also be similar. However, a few configuration options vary from widget to widget.

For example, the Button and Label widgets will have an option to configure their text, but scrollbars do not have a text-configuration option.

Using the same pattern, let's now add all the 21 core Tkinter widgets into a dummy application (code 01.03.py).

Tip

Do not be intimidated by the size of the program. Instead look for a common pattern that is used to initialize and display all the widgets. To reiterate, the syntax for adding a widget is:

mywidget = Widget-name (container, all widget-options)

Notice how the configuration options for each widget differ slightly from each other depending on the type of widget being initialized.

Refer to the code 1.03.py for a demo of all Tkinter widgets. A summarized code description for 1.03.py is as follows:

  • We create a Toplevel window and create a main loop as seen in the earlier examples.
  • We add a Frame widget that we named menubar. Note that Frame widgets are just holder widgets that hold other widgets. Frame widgets are great for grouping widgets together. The syntax for adding a frame is the same as that of all other widgets:
    myframe = Frame(root)
    myframe.pack()
  • Keeping the menubar frame as the container, we add two widgets to it, the Menubutton and Menu widgets.
  • We create another frame and name it myframe1. Keeping myframe1 as the container/parent widget, we add seven widgets to it:
    • The Label, Entry, Button, Checkbutton, Radiobutton, OptionMenu, and Bitmap Class widgets.
  • We then proceed to create myframe2, another Frame widget. We add six more widgets to it:
    • The Image Class, Listbox, Spinbox, Scale, LabelFrame, and Message widgets.
  • We then create myframe3, another Frame widget. We add two more widgets to it, the Text and Scrollbar widgets.
  • Finally we create the last frame, myframe4, another Frame widget. We add two more widgets to it, the Canvas and PanedWindow widgets.

All these widgets constitute the 21 core widgets of Tkinter.

Note

Read through the code explanation, and find the corresponding piece of code in the example code 01.03.py. Look at how each widget is created. Try to identify each widget's class name as used in Tkinter. Look what remains the same in all widgets, and what changes between one widget and another?

A few minutes spent reading and understanding the code in 1.03.py will really help you appreciate the simplicity and overall structure of a Tkinter program.

Finally, note that we have used .pack() on each widget to display it inside its container frame. We discuss .pack() in the next task. However, for now just note that we have used something called pack(), without which the widgets would not have displayed at all.

Objective Complete – Mini Debriefing

You have reached a major milestone in your GUI programming effort.

You now know all the 21 core widgets of Tkinter. You can identify them by their class names, and you can create them on a root frame or on a subframe within the root. You now know how to configure options of widgets.

With this you have now seen the first and the most important building block of a Tkinter program. You have mastered Tkinter widgets.

Classified Intel

Widget options can be set at instantiation time as we have done in the examples so far. Alternatively, the options can be configured after instantiation using the following syntax:

widget.configure(**options)

This is a very handy tool that lets you change widget options dynamically after the widget has been created. We will be using this very often in all our projects.

For common widget configuration options, refer to the Options common to widgets section in Appendix B, Quick Reference Sheets.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.140.204