What is Frame Widget?
Frame widget is used as a placeholder or container for other widgets that groups them together. Frame widget can also be used for padding and as a base class for implementing compound widgets.
Example
The following Python script creates a window containing two Frame. The first frame holds one Label and Entry widget, and the second one contains one button.
You need to write following code in any text editor and then save it with .py extension. To execute the application, Type python FrameExample.py command in terminal.
from tkinter import * window = Tk() window.title("Studyfied.com") window.geometry('300x100') # Create top frame topFrame = Frame(window) topFrame.pack() # Add one label and entry widget label = Label(topFrame, text="Enter name : ") label.pack(side=LEFT) txtUsername = Entry(topFrame) txtUsername.pack(side=RIGHT) # Create bottom frame bottomFrame = Frame(window) bottomFrame.pack(side = BOTTOM) # Login button btnLogin = Button(bottomFrame, text="Login") btnLogin.pack(side = BOTTOM) window.mainloop()
Output
The above code produces the following output in windows operating system.
Explanation
Following line creates a Tkinter window
from tkinter import * window = Tk() window.title("Studyfied.com") window.geometry('350x200')
See explanation of Tkinter window – Tkinter top level window
The following block of code creates a top frame with one label and entry widget.
topFrame = Frame(window) topFrame.pack() # Add one label and entry widget label = Label(topFrame, text="Enter name : ") label.pack(side=LEFT) txtUsername = Entry(topFrame) txtUsername.pack(side=RIGHT)
Similarly, this block creates the bottom frame with one button
# Create bottom frame bottomFrame = Frame(window) bottomFrame.pack(side = BOTTOM) # Login button btnLogin = Button(bottomFrame, text="Login") btnLogin.pack(side = BOTTOM)
Some frame properties
Property Name | Description |
---|---|
background / bg | Sets the background color of the widget, Default Is platform specific |
borderwidth / bd | Sets widgets border. The default is system specific but usually 1 to 2 pixels. |
cursor | The cursor to display when the mouse moves over the widget. By default, the standard cursor is used. |
takefocus | If true, the user can navigate to this widget using the Tab key. The default value is 0. |
height | Sets the height of widget |
width | Sets the width of widget |
padx | Horizontal padding to add around the widget. The default is 0 pixel. |
pady | Vertical padding to add around the widget. The default is 0 pixel. |