Tkinter frame

A Frame is a container control, when we design a complex GUI application, then we can consider organizing a series of related widget controls in a frame, which is easier to manage. The constructor syntax is as follows.

Frame(parent object, options, ...)

Parameters.

  • First parameter: parent object, indicating the window in which this frame will be built
  • The second parameter: options, with the following parameters
borderwidth
bd  
bg  
background  
cursor  
height
highlightbackground 
highlightcolor  
highlighthickness   
relief  
width

Related course: Python Desktop Apps with Tkinter

Tkinter frame

The initial use of frame Frame

Example: Create three frames with different base colors

import tkinter

root = tkinter.Tk()
for i in ["red", "orange", "lightgreen"]:
    tkinter.Frame(bg=i, height=50, width=150).pack()

root.mainloop()

tkinter frame

The frame is also a Widget control, so in the end it also needs to be wrapped and positioned using the control configuration manager, i.e. pack, grid, etc.

Example: Simultaneously make the mouse cursor have different shapes on different frames

import tkinter

root = tkinter.Tk()
x = {"red": "cross", "orange": "boat", "lightgreen": "clock", "lightblue": "star"}

for i in x:
    tkinter.Frame(bg=i, cursor=x[i], height=50, width=150).pack(side=tkinter.LEFT)

root.mainloop()

horizontal frame

Related course: Python Desktop Apps with Tkinter

Tkinter frame widget

Create widget controls within the framework.

import tkinter

root = tkinter.Tk()
frameUp = tkinter.Frame(root, bg="lightyellow")
frameUp.pack()
butOne = tkinter.Button(frameUp, text="One", fg="red")
butOne.pack(side=tkinter.LEFT, padx=5, pady=5)
butTwo = tkinter.Button(frameUp, text="Two", fg="orange")
butTwo.pack(side=tkinter.LEFT, padx=5, pady=5)
butThree = tkinter.Button(frameUp, text="Three", fg="lightgreen")
butThree.pack(side=tkinter.LEFT, padx=5, pady=5)
frameLow = tkinter.Frame(root, bg="lightblue")
frameLow.pack()
butLast = tkinter.Button(frameLow, text="Last")
butLast.pack(side=tkinter.LEFT, padx=5, pady=5)

root.mainloop()

tkinter frame widget

relief

You can use the properties of the relief property to build the widget control inside the frame

import tkinter

root = tkinter.Tk()
frameOne = tkinter.Frame(width=150, height=80, relief=tkinter.GROOVE, borderwidth=5)
frameOne.pack(side=tkinter.LEFT, padx=5, pady=5)

frameTwo = tkinter.Frame(width=150, height=80, relief=tkinter.RAISED, borderwidth=5)
frameTwo.pack(side=tkinter.LEFT, padx=5, pady=5)

frameThree = tkinter.Frame(width=150, height=80, relief=tkinter.RIDGE, borderwidth=5)
frameThree.pack(side=tkinter.LEFT, padx=5, pady=5)

root.mainloop()

tkinter relief

raised

Create checkboxes within frames containing the raised attribute

import tkinter

root = tkinter.Tk()
frame = tkinter.Frame(width=150, height=80, relief=tkinter.RAISED, borderwidth=5)
frame.pack(padx=10, pady=10)
label = tkinter.Label(frame, text="Menu")
label.pack()
foodOne = tkinter.Checkbutton(frame, text="Veggie")
foodOne.pack(anchor=tkinter.W)
foodTwo = tkinter.Checkbutton(frame, text="Meat")
foodTwo.pack(anchor=tkinter.W)
foodThree = tkinter.Checkbutton(frame, text="Fish")
foodThree.pack(anchor=tkinter.W)

root.mainloop()

tkinter raised

LabelFrame

This is also a container control, which is mainly a series of related Widgets organized in a tag frame and then given a name.

import tkinter

root = tkinter.Tk()
textFirst = "Data"
photo = tkinter.PhotoImage(file="1.png")
Logo = tkinter.Label(root, image=photo, text=textFirst, compound=tkinter.BOTTOM)
Logo.pack()

# LabelFrame
labelFrame = tkinter.LabelFrame(root, text="frame")

accountLabel = tkinter.Label(labelFrame, text="username")
accountLabel.grid(row=0, column=0)

accountEntry = tkinter.Entry(labelFrame)
accountEntry.grid(row=0, column=1)

passWd = tkinter.Label(labelFrame, text="password")
passWd.grid(row=1, column=0)

passWdEntry = tkinter.Entry(labelFrame, show="*")
passWdEntry.grid(row=1, column=1)

labelFrame.pack(padx=10, pady=5, ipadx=5, ipady=5)

root.mainloop()

LabelFrame in tkinter

Related course: Python Desktop Apps with Tkinter