Example1: Frame Widget

 import tkinter as tk


# Create the main application window

root = tk.Tk()

root.title("Frame Widget Example")

root.geometry("300x200")


# Create a frame within the main window

frame = tk.Frame(root, bg="lightblue", bd=5, relief=tk.RIDGE)

frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)


# Add widgets to the frame

label = tk.Label(frame, text="This is a label inside the frame", bg="lightblue")

label.pack(pady=10)


button = tk.Button(frame, text="Click Me")

button.pack(pady=10)


# Run the application

root.mainloop()

Explanation:

  • root is the main application window.
  • root.title sets the title of the window.
  • root.geometry sets the size of the window.
  • frame is created as a child of root.
  • bg="lightblue" sets the background color of the frame.
  • bd=5 sets the border width of the frame.
  • relief=tk.RIDGE gives the frame a ridge border style.
  • frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10) packs the frame into the window, making it expand to fill the available space with padding around it.
    • A Label widget is created and added to the frame with some padding.
    • A Button widget is created and added to the frame with some padding.
  • root.mainloop() starts the Tkinter event loop, allowing the window to display and be interactive.




Comments