Tkinter Frame Tutorial
Frame is a container control, when we design a complex GUI application, then we can consider organizing a series of related widgets in a frame, which can be easily managed.
The syntax of its constructor is as follows.
Frame(master, options, ...) # The parent object can be ignored
The first argument to the Frame( ) method is the parent object, indicating which parent object this frame will be built inside. The following are other commonly used options parameters within the Frame( ) method.
(1) bg or background
: the background color.
(2) borderwidth or bd
: label border width, the default is 2.
(3) cursor
: the shape of the cursor when the mouse cursor is on the frame.
(4) height
: the height of the frame, the unit is pixels.
(5) highlightbackground
: the color of the frame when it doesn’t get the focus.
(6) highlightcolor
: the color of the frame when it gets the focus.
(7) highlighthickness
: the thickness of the frame when it gets the focus.
(8) relief
: default is relief=FLAT, you can control the frame outer frame from this.
(9) width
: the width of the frame, the unit is pixels, will be adjusted to the actual width when omitted.
Tkinter Frame Tutorial Example#1
Create three frames with different undertones.
from tkinter import *
root = Tk()
root.title("apidemos.com")
for fm in ["red","green","blue"]: # Create three frames with different undertones.
Frame(root,bg=fm,height=50,width=250).pack()
root.mainloop()
Output:
Execution results from the above example should be understood that the framework is also a Widget control, so in the end also need to use the control configuration administrator packaging and positioning, in this case is the use of pack ().
Tkinter Frame Tutorial Example#2
Omit the parent object when calling the Frame constructor.
from tkinter import *
root = Tk()
root.title("apidemos.com")
for fm in ["red","green","blue"]: # Create three frames with different undertones
# Frame(root,bg=fm,height=50,width=250).pack()
Frame(bg=fm,height=50,width=250).pack() # Omitting the parent object when calling the constructor method
root.mainloop()
Output:
Tkinter Frame Tutorial Example#3
Use the horizontal configuration method (side=LEFT) while allowing the mouse cursor to have different shapes on different frames.
from tkinter import *
root = Tk()
root.title("apidemos.com")
# Store frame colors and cursor shapes with dictionaries
fms = {
"red":"cross",
"green":"boat",
"blue":"clock"
}
for fmColor in fms.keys(): # Create three frames with different base colors and cursor shapes
# Frame(root,bg=fm,height=50,width=250).pack()
Frame(root,bg=fmColor,cursor=fms[fmColor],
height=50,width=250).pack(side=LEFT) # Omitting the parent object when calling the constructor method
root.mainloop()
Output: