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:
rootis the main application window.root.titlesets the title of the window.root.geometrysets the size of the window.frameis created as a child ofroot.bg="lightblue"sets the background color of the frame.bd=5sets the border width of the frame.relief=tk.RIDGEgives 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
Labelwidget is created and added to the frame with some padding. - A
Buttonwidget is created and added to the frame with some padding.
- A
root.mainloop()starts the Tkinter event loop, allowing the window to display and be interactive.
Comments
Post a Comment