We will use the options highlightbackground='red' and highlightthicknes=5 to add red colour and thickness of 5 to the borders. For grid layout we can use rowconfigure , columnconfigure to manage full width
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("400x200")
frame_top=Frame(my_w,width=380, height=180,
highlightbackground='red',highlightthicknes=5)
frame_top.pack(expand=True)
my_w.mainloop()
Use the option background='yellow' or use Hex code ( #FFFF00 ) to assign background colour to the frame.
If you are using pack layout then keep the option expand=True to cover all available areas.
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("400x400")
frame_top=Frame(my_w,width=390, height=390,background='yellow',
highlightbackground='red',highlightthicknes=3)
frame_top.grid(row=0,column=0,padx=5,pady=5,ipadx=5,ipady=5)
l2 = tk.Label(frame_top,text='yellow background with red border' )
l2.grid(row=0,column=0,padx=5,pady=5)
my_w.mainloop()
Understanding Multiple frame layout
To remove the butom frame ( frame_bottom ) the command option for button is used. command=lambda:frame_bottom.grid_forget(), here in place of grid_forget(), pack_forget() is to be used for pack layout manager.
Example: Data Visualization Dashboard using Tkinter Frame
Creating a simple data visualization dashboard in Tkinter involves combining various widgets and a plotting library like Matplotlib to display charts and information. Here's an example of a dashboard that shows a pie chart, a bar chart, and some numerical stats:
Main Application Window:
The main window (root) is created with a title "Data Visualization Dashboard" and dimensions 800x600 pixels using the geometry() method.
Title Frame:
A Frame widget (title_frame) is used to display the title of the dashboard.
The Label widget (title_label) is placed within this frame and styled with the Arial font and a light blue background.
Stats Frame:
The stats_frame holds statistical information, such as total sales, new users, and revenue.
Each statistic is displayed as a Label with a white background and solid border.
Charts Frame:
The charts_frame is a container for the visualizations (Pie Chart and Bar Chart).
Pie Chart:
A Frame (pie_frame) is used to hold the pie chart visualization.
The pie chart shows product distribution using plt.pie() and is embedded into the Tkinter frame using FigureCanvasTkAgg.
It includes labels ("Product A", "Product B", "Product C") and percentages with distinct colors.
Bar Chart:
A Frame (bar_frame) is used to hold the bar chart visualization.
The bar chart displays monthly sales using plt.bar() and is embedded into the Tkinter frame.
Categories ("Jan", "Feb", "Mar", etc.) represent the months, and the values represent sales.
Embedding Matplotlib Charts:
The FigureCanvasTkAgg class from matplotlib.backends.backend_tkagg integrates Matplotlib charts into Tkinter.
Each chart is rendered and packed into its respective frame (pie_frame and bar_frame).
Running the Application:
The mainloop() method is used to start the Tkinter event loop and keep the application open.