Tkinter LabelFrame Tutorial
Basic concepts of LabelFrame
This is also a container control that essentially organizes a series of related Widgets in a tabbed frame and then gives it a name. It has the following constructor syntax.
LabelFrame(master, options, ...)
The first parameter of the LabelFrame() method is the parent object, indicating which parent object this label frame will be built inside.
The following are other commonly used options parameters within the LabelFrame() method.
(1) bg or background
: the background color.
(2) borderwidth or bd
: the width of the label border, the default is 2.
(3) cursor
: the shape of the cursor when the mouse cursor is on the frame.
(4) font
: the font of the text in the label frame.
(5) height
: the height of the frame, in pixels.
(6) highlightbackground
: the color of the frame when it does not get focus.
(7) highlightcolor
: the color of the frame when it gets the focus.
(8) highlighthickness
: the thickness of the frame when it gets the focus.
(9) labelAnchor
: set the position to place the label.
(10) relief
: the default is relief=FLAT, which can control the frame’s outer frame.
(11) text
: label content.
(12) width
: the width of the frame, the unit is pixels, the omission will adjust itself to the actual width.
Tkinter LabelFrame Example#1
Frame the account and password fields with a tag that has the text Data Validation
.
from tkinter import *
root = Tk()
root.title("apidemos.com") # Title of the window
msg = "Welcome to apidemos.com"
myGif = PhotoImage(file="apidemos.png") # Logo image file
logo = Label(root,image=myGif,text=msg,compound=BOTTOM)
logo.pack()
# The following is the LabelFrame label frame
labFrame = LabelFrame(root,text="Data Validation") # Create LabelFrame
accountL = Label(labFrame,text="Account") # name Label
accountL.grid(row=0,column=0) # By default: column=0
pwdL = Label(labFrame,text="Password") # address Label
pwdL.grid(row=1,column=0) # By default: column=0
accountE = Entry(labFrame) # name Entry
accountE.grid(row=0,column=1)
pwdE = Entry(labFrame,show="*") # address Entry
pwdE.grid(row=1,column=1,pady=10) # Locate address Entry
labFrame.pack(padx=10,pady=5,ipadx=5,ipady=5)
root.mainloop()
Output: