0

I'm working on a project where my result will be displayed in a graph. I'm working with CustomTkinter and my graphs are displayed with Mathplotlib. I'm having trouble with removing the borders around my graphs or just making them smaller mainly on the sides. this is what it looks like now:

enter image description here

this is my code:

import time
import tkinter
import tkinter.messagebox
from CTkMessagebox import CTkMessagebox
import customtkinter
from customtkinter import *
from matplotlib.figure import Figure
from CTkListbox import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
from tkinter import ttk
import pandas as pd
import csv
import matplotlib.dates as mdates
import datetime
import numpy as np

customtkinter.set_appearance_mode(
    "System"
)  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme(
    "blue"
)  # Themes: "blue" (standard), "green", "dark-blue"

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        self.db_manager = DatabaseManager(
            "database_master.db"
        )  # Create an instance of DatabaseManager

        # configure window
        self.title("Smart Home Simulation")
        # self.geometry(f"{1280}x{760}")

        # configure grid layout (2x3)
        self.grid_columnconfigure(0, weight=0)
        self.grid_columnconfigure(1, weight=1)
        self.grid_rowconfigure(0, weight=1)
        self.grid_rowconfigure(1, weight=0)

        customtkinter.set_appearance_mode("Dark")


        # create sidebar frame with widgets
        self.sidebar_frame = customtkinter.CTkFrame(
            self, corner_radius=0, border_width=2
        )
        self.sidebar_frame.grid(row=0, column=0, columnspan=2, sticky="nsew")
        #self.sidebar_frame.grid_rowconfigure(18, weight=1)
        #self.sidebar_frame.grid_propagate(False)  # Prevent resizing
        
        #####################################################
        
        # self.sidebar_button_1 = customtkinter.CTkButton(
        #     self.sidebar_frame,
        #     text="Start Simulation",
        #     command=lambda: self.start_process(
        #         self.entry_latitude.get(),
        #         self.entry_longitude.get(),
        #         self.entry_start_date.get(),
        #         self.entry_end_date.get()
        #     ),
        # )
        # self.sidebar_button_1.grid(row=0, column=0, padx=(20,20))
        
        self.sidebar_button_1 = customtkinter.CTkButton(
            self.sidebar_frame,
            text="Start Simulation",
            command=self.start_process,
        )
        self.sidebar_button_1.grid(row=0, column=0, pady=(10,10), padx=(20,20))
        
        ######################################################
        
        self.sidebar_button_2 = customtkinter.CTkButton(
            self.sidebar_frame,
            text="Simulation Parameters",
            command=self.simulation_parameters,
        )
        self.sidebar_button_2.grid(row=0, column=1, padx=(20,20))
        
        ######################################################
        
        # Option Menu for Simulation scale
        self.label_scale = customtkinter.CTkLabel(
            self.sidebar_frame,
            text="Visualisation:",
            font=customtkinter.CTkFont(size=20, weight="bold"),
        )
        self.label_scale.grid(row=0, column=2, padx=(40,0))
        
        self.optionmenu_scale = customtkinter.CTkOptionMenu(
            self.sidebar_frame,
            dynamic_resizing=False, values=["PER YEAR", "PER MONTH", "PER WEEK", "SPECIFIC MONTH", "SPECIFIC WEEK", "SPECIFIC DAY"],
            command=self.update_time_options
        )
        self.optionmenu_scale.grid(row=0, column=3, padx=(20,0))
        
        self.optionmenu_time = customtkinter.CTkOptionMenu(
            self.sidebar_frame,
            width=60,
            values=[""]
        )
        self.optionmenu_time.grid(row=0, column=4, padx=(20, 20))

        #######################################################

        # Update layout after adding widgets
        self.sidebar_frame.update_idletasks()
        self.sidebar_frame_width = (
            self.sidebar_frame.winfo_reqwidth()
        )  # Get the required width of the sidebar frame
        self.sidebar_frame.grid_configure(padx=(0, 10))  # Adjust padding as needed

        # set default values
        customtkinter.set_appearance_mode("Dark")

        #######################################################
    
        # create Matplotlib graphs
        self.fig, (self.ax1, self.ax2) = plt.subplots(
            2, 1, figsize=(13, 7), gridspec_kw={"height_ratios": [2, 1]}
        )
        self.fig.patch.set_facecolor("#c4d404")
        
        self.ax1.set_facecolor("#c4d404")  # Change to your desired color
        self.ax2.set_facecolor("#c4d404")  # Change to your desired color

        self.canvas1 = FigureCanvasTkAgg(self.fig, master=self)

        self.canvas1.get_tk_widget().grid(
            row=1, column=0, padx=(5, 5), pady=(5, 5), sticky="nsew"
        )

        # Adjust layout to add space between subplots
        plt.subplots_adjust(hspace=0.4)
        
        ######################################################

        # create sidebar frame with widgets
        self.sidebar_frame2 = customtkinter.CTkFrame(
            self, corner_radius=0, border_width=2
        )
        self.sidebar_frame2.grid(row=1, column=1, sticky="nsew")
        
        
        self.logo_label1 = customtkinter.CTkLabel(
            self.sidebar_frame2,
            text="Sim Results",
            font=customtkinter.CTkFont(size=20, weight="bold"),
        )
        self.logo_label1.grid(row=0, column=0, pady=(20,20), columnspan=3)
        
        self.logo_label2 = customtkinter.CTkLabel(
            self.sidebar_frame2,
            text="Default",
            font=customtkinter.CTkFont(size=15),
        )
        self.logo_label2.grid(row=1, column=1, padx=(0,10))
        
        self.logo_label3 = customtkinter.CTkLabel(
            self.sidebar_frame2,
            text="Optimized",
            font=customtkinter.CTkFont(size=15),
        )
        self.logo_label3.grid(row=1, column=2, padx=(0,20))

        ############################################################

        self.label_extraction = customtkinter.CTkLabel(
            self.sidebar_frame2,
            text="Total Grid Extraction:",
            font=customtkinter.CTkFont(size=15),
        )
        self.label_extraction.grid(row=2, column=0, padx=20, pady=(5, 0), sticky="w")
        
        self.label_extraction_result_1 = customtkinter.CTkLabel(
            self.sidebar_frame2, text="...", font=customtkinter.CTkFont(size=10)
        )
        self.label_extraction_result_1.grid(row=2, column=1, pady=(5, 0) , padx=(0,10))
        
        self.label_extraction_result_2 = customtkinter.CTkLabel(
            self.sidebar_frame2, text="...", font=customtkinter.CTkFont(size=10)
        )
        self.label_extraction_result_2.grid(row=2, column=2, pady=(5, 0) )

        ############################################################

        self.label_injection = customtkinter.CTkLabel(
            self.sidebar_frame2,
            text="Total Grid Injection:",
            font=customtkinter.CTkFont(size=15),
        )
        self.label_injection.grid(row=3, column=0, padx=20, pady=(5, 0) , sticky="w")
        
        self.label_injection_result_1 = customtkinter.CTkLabel(
            self.sidebar_frame2, text="...", font=customtkinter.CTkFont(size=10)
        )
        self.label_injection_result_1.grid(row=3, column=1, pady=(5, 0) , padx=(0,10))
        
        self.label_injection_result_2 = customtkinter.CTkLabel(
            self.sidebar_frame2, text="...", font=customtkinter.CTkFont(size=10)
        )
        self.label_injection_result_2.grid(row=3, column=2, pady=(5, 0) )

        ###########################################################
        
        self.label_charge = customtkinter.CTkLabel(
            self.sidebar_frame2,
            text="Total Bat. Charge:",
            anchor="w",
            font=customtkinter.CTkFont(size=15),
        )
        self.label_charge.grid(row=4, column=0, padx=20, pady=(5, 0) , sticky="w")
        
        self.label_charge_result_1 = customtkinter.CTkLabel(
            self.sidebar_frame2, text="...", font=customtkinter.CTkFont(size=10)
        )
        self.label_charge_result_1.grid(row=4, column=1, pady=(5, 0) , padx=(0,10))
        
        self.label_charge_result_2 = customtkinter.CTkLabel(
            self.sidebar_frame2, text="...", font=customtkinter.CTkFont(size=10)
        )
        self.label_charge_result_2.grid(row=4, column=2, pady=(5, 0) )

        ###########################################################
        
        self.label_discharge = customtkinter.CTkLabel(
            self.sidebar_frame2,
            text="Total Bat. Discharge:",
            anchor="w",
            font=customtkinter.CTkFont(size=15),
        )
        self.label_discharge.grid(row=5, column=0, padx=20, pady=(5, 0) , sticky="w")
        
        self.label_discharge_result_1 = customtkinter.CTkLabel(
            self.sidebar_frame2, text="...", font=customtkinter.CTkFont(size=10)
        )
        self.label_discharge_result_1.grid(row=5, column=1, pady=(5, 0) , padx=(0,10))
        
        self.label_discharge_result_2 = customtkinter.CTkLabel(
            self.sidebar_frame2, text="...", font=customtkinter.CTkFont(size=10)
        )
        self.label_discharge_result_2.grid(row=5, column=2, pady=(5, 0) )

        ###########################################################
        
        self.label_pv_production = customtkinter.CTkLabel(
            self.sidebar_frame2,
            text="Total Pv Production:",
            anchor="w",
            font=customtkinter.CTkFont(size=15),
        )
        self.label_pv_production.grid(row=6, column=0, padx=20, pady=(5, 0) , sticky="w")
        
        self.label_pv_production_result_1 = customtkinter.CTkLabel(
            self.sidebar_frame2, text="...", font=customtkinter.CTkFont(size=10)
        )
        self.label_pv_production_result_1.grid(row=6, column=1, pady=(5, 0) , padx=(0,10))
        
        self.label_pv_production_result_2 = customtkinter.CTkLabel(
            self.sidebar_frame2, text="...", font=customtkinter.CTkFont(size=10)
        )
        self.label_pv_production_result_2.grid(row=6, column=2, pady=(5, 0) )

        ###########################################################
        
        self.label_power_use = customtkinter.CTkLabel(
            self.sidebar_frame2,
            text="Total Power Use:",
            anchor="w",
            font=customtkinter.CTkFont(size=15),
        )
        self.label_power_use.grid(row=7, column=0, padx=20, pady=(5, 0) , sticky="w")
        
        self.label_power_use_result_1 = customtkinter.CTkLabel(
            self.sidebar_frame2, text="...", font=customtkinter.CTkFont(size=10)
        )
        self.label_power_use_result_1.grid(row=7, column=1, pady=(5, 0) , padx=(0,10))
        
        self.label_power_use_result_2 = customtkinter.CTkLabel(
            self.sidebar_frame2, text="...", font=customtkinter.CTkFont(size=10)
        )
        self.label_power_use_result_2.grid(row=7, column=2, pady=(5, 0) )

        ###########################################################
        
        self.label_earning = customtkinter.CTkLabel(
            self.sidebar_frame2,
            text="Total Earning:",
            anchor="w",
            font=customtkinter.CTkFont(size=15),
        )
        self.label_earning.grid(row=8, column=0, padx=20, pady=(5, 0) , sticky="w")
        
        self.label_earning_result_1 = customtkinter.CTkLabel(
            self.sidebar_frame2, text="...", font=customtkinter.CTkFont(size=10)
        )
        self.label_earning_result_1.grid(row=8, column=1, pady=(5, 0) , padx=(0,10))
        
        self.label_earning_result_2 = customtkinter.CTkLabel(
            self.sidebar_frame2, text="...", font=customtkinter.CTkFont(size=10)
        )
        self.label_earning_result_2.grid(row=8, column=2, pady=(5, 0) )

        ###########################################################
        
        self.label_cost = customtkinter.CTkLabel(
            self.sidebar_frame2,
            text="Total Cost:",
            anchor="w",
            font=customtkinter.CTkFont(size=15),
        )
        self.label_cost.grid(row=9, column=0, padx=20, pady=(5, 0) , sticky="w")
        
        self.label_cost_result_1 = customtkinter.CTkLabel(
            self.sidebar_frame2, text="...", font=customtkinter.CTkFont(size=10)
        )
        self.label_cost_result_1.grid(row=9, column=1, pady=(5, 0) , padx=(0,10))
        
        self.label_cost_result_2 = customtkinter.CTkLabel(
            self.sidebar_frame2, text="...", font=customtkinter.CTkFont(size=10)
        )
        self.label_cost_result_2.grid(row=9, column=2, pady=(5, 0) )

        ###########################################################
        
        
        
        
        
        #set default values
        #self.entry_start_date.insert(tkinter.END, '2023-01-01')
        #self.entry_end_date.insert(tkinter.END, '2024-01-01')
        #self.entry_latitude.insert(tkinter.END, "50.92549")
        #self.entry_longitude.insert(tkinter.END, "5.39328")
        self.optionmenu_time.set("/")

    def simulation_parameters():
        return
    
    def update_graphs_with_new_data(self, data_points):
        return

    # TODO this doesnt work yet
    def populate_battery_options(self):
        # Fetch battery data from the database
        battery_data = self.db_manager.fetch_battery_data()
        print(battery_data)
        # Extract battery names
        battery_options = [battery[1] for battery in battery_data]
        # Update option menu with battery names
        #self.optionmenu_battery.option_clear
        #self.optionmenu_battery.configure(values=battery_options)
        #self.optionmenu_battery.set(battery_options[0])

    def edit_battery(self):
        edit_dialog = BatteryManager(
            self.db_manager, callback=self.populate_battery_options
        )
        self.populate_battery_options()
        
    def update_time_options(self, event):
        selected_scale = self.optionmenu_scale.get()
        time_options = []

        if selected_scale == "SPECIFIC MONTH":
            time_options = [str(x) for x in range(1, 13)]  # Months from 0 to 12
        elif selected_scale == "SPECIFIC WEEK":
            time_options = [str(x) for x in range(1, 53)]  # Weeks from 0 to 52
        elif selected_scale == "SPECIFIC DAY":
            for month in range(1, 13):
                for day in range(1, 32):
                    try:
                        date = datetime.date(2023, month, day)
                        time_options.append((date.strftime("%d-%m")))
                    except ValueError:
                        # Handle cases where the day is out of range for the month
                        pass
        else: 
            time_options = ["/"]
        
        # Update the option menu for time values
        self.optionmenu_time.option_clear
        self.optionmenu_time.configure(values=time_options)
        self.optionmenu_time.set(time_options[0])

    def start_process(): #self, latitude, longitude, start_date, end_date):
        return
        #selected_battery = self.optionmenu_battery.get()
        #selected_battery_data = self.db_manager.fetch_battery_by_name(selected_battery)
        #selected_user_profile = self.optionmenu_consumer.get()

if __name__ == "__main__":
    app = App()
    app.mainloop()

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.