Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Assigning Widgets

00:00 Assigning Widgets to Frames With Frame Widgets In this course, you’ll only be working with five widgets. These are the four you’ve seen so far, plus the Frame widget.

00:12 Frame widgets are important for organizing the layout of your widgets in an application. Before you get into the details of visual presentation, let’s take a closer look at how Frame widgets work and how you can assign other widgets to them.

00:27 The code on screen creates a blank Frame widget and assigns it to the main application window.

00:37 .frame.pack() packs the frame into the window so that the window sizes itself as small as possible to encompass the frame. When you run the script, you’ll get some uninteresting output.

00:50 If you’re on Windows, then you’ll see a very minimal window with just the basic controls as seen on screen.

00:57 If you’re on macOS or Linux, these commands may create a window which is next to invisible that you’ll need to expand using the cursor.

01:08 You may find it’s easier to just kill the Python process and the window along with it. An empty frame widget is practically invisible. Frames are best thought of as containers for other widgets.

01:21 You can assign a widget to a frame by setting the widget’s .master attribute. To see how this works, on screen, you’ll see a script which creates two frame widgets called frame_a and frame_b.

01:34 frame_a contains a label with the text, “I’m in Frame A,” and frame_b contains a label with the text “I’m in Frame B.”

01:57 Note that because frame_a is packed into the window before frame_b, the window shows the label in frame_a above the label in frame_b.

02:12 If you reverse the order of the packing, you can see the change on screen.

02:26 Now label_b is on top. Because label_b is assigned to frame_b, it moves to wherever frame_b is positioned.

02:35 All four of the widget types you’ve learned about—Label, Button, Entry, and Text—have a .master attribute that is set when you instantiate them.

02:44 That way, you can control which frame a widget is assigned to. If you omit the .master argument when creating a new widget instance, then it will be placed inside the top-level window by default.

02:56 Frame widgets are great for organizing other widgets in a logical manner. Related widgets can be assigned to the same frame so that if a frame is moved in a window, the related widgets stay together.

03:09 In addition to grouping the widgets locally, Frame widgets can add a little flare to the visual presentation of your application. So in the next section of the course, you’ll see how you can adjust the appearance of frames.

Become a Member to join the conversation.