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

Adjusting Frame Appearance

00:00 Adjusting Frame Appearance With Reliefs Frame widgets can be configured with a relief attribute that creates a border around the frame. You can set relief to any of the values seen on screen.

00:15 To apply the border effect, you must set the borderwidth attribute to a value greater than one. This attribute adjusts the width of the border in pixels. As ever, the best way to get a feel for the effect is to see them for yourself. On screen, you can see a script that packs five frame widgets into a window, each with a different value for the relief argument.

00:39 First, you create a border_effects variable, which is a dictionary whose keys are the names of the different relief effects available in Tkinter and the values of the corresponding Tkinter objects.

00:56 Here you start a for loop to loop over each item in the border_effects dictionary. This line creates a new frame widget and assigns it to the window object.

01:07 The relief attribute is set to the corresponding relief in the border_effects dictionary, and the borderwidth attribute is set to five so that the effect is visible.

01:18 Next, you pack the frame into the window using pack(). The side keyword argument tells Tkinter which direction to pack the frame objects.

01:26 You’ll see more about this later in the course.

01:33 These lines create a label widget to display the name of the relief and pack it into the frame object you’ve just created.

01:42 Once saved, you can run the script as you would any other.

01:47 The window should look similar to the one you see on screen.

01:52 Here you can see the five effects: flat creates a frame that appears to be flat, sunken adds a border that gives the frame the appearance of being sunken into the window, raised gives a border which makes it appear to stick out of the window, groove adds a border that appears as a sunken groove around an otherwise flat frame, and ridge gives the appearance of a raised lip around the edge of the frame.

02:15 These effects should give your Tkinter application a bit of visual appeal. When you create a widget, you can give it any name you like as long as it’s a valid Python identifier.

02:26 It’s usually a good idea to include the name of the widget class in the variable name that you assign to the widget instance. For example, if a Label widget is used to display a user’s name, then you might name the widget label_user_name.

02:41 An Entry widget to collect a user’s age might be called entry_age. When you include the widget class name in the variable name, you help yourself and anyone else who needs to read your code to understand what type of widget the variable name refers to.

02:55 And remember that someone else could be you in the future. But using the full name of the widget class can lead to long variable names, so you may want to adopt a shorthand for referring to each widget type.

03:09 For the rest of this course, the shorthand prefixes seen on screen will be used.

03:15 Sometimes you may define a new widget without assigning it to a variable. You’ll call its pack() method directly on the same line of code. This might be helpful when you don’t intend to refer to the widget’s instance later on.

03:29 Due to automatic memory management in Python, it would normally garbage collect such unassigned objects, but tkinter prevents that by registering every new widget internally. At this point, you can make some plain windows that display messages, but you’ve yet to create a full-blown application.

03:48 In the next section, you’ll learn how to control the layout of your applications using Tkinter’s powerful Geometry Managers.

Become a Member to join the conversation.