Window in PysimpleGui Resizes after Matplot lib is used

15 views Asked by At

I am trying to make a state machine using a gui imported from Pysimplegui. I have a home screen that has a button labeled "view available stock". I then transtion to the code below, but doing so resizes my orginal homescreen from 1024x600 down. How do I call this code without it resizing my current gui.

"""

-- Engineer:        Taylor Fettig
-- Group:           Hardware Sorting and Dispense
-- Create Date:     03/20/2024
-- Design Name:     hardware_stock_screen.py

"""

import PySimpleGUI as sg
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from state import State

class HARDWARE_STOCK_SCREEN:
    def __init__(self, inventory_data):
        self.inventory_data = inventory_data

        # Adjusted window location
        window_location = (200, 100)

        # Create the plot
        fig = plt.figure(figsize=(7, 7.5))
        fig1 =fig.set_size_inches((10,6))
        self.draw_plot(fig1)

        # Calculate the canvas size
        canvas_size = (1240,600)

        layout = [
            [sg.Button('Exit', button_color=('black', 'white'))],
            [sg.Canvas(size=canvas_size, key='-CANVAS-')],  # Adjusted canvas size
        ]

        # Create the window with specified location
        self.window = sg.Window('Hardware Stock', layout, background_color='#0032A0', location=window_location,
                                finalize=True,resizable=False)

        # Finalize the window
        self.window.finalize()

        # Embed Matplotlib plot into PySimpleGUI window
        canvas_elem = self.window['-CANVAS-'].TKCanvas
        canvas = FigureCanvasTkAgg(fig, master=canvas_elem)
        canvas.draw()
        canvas.get_tk_widget().pack(side='top', fill='both', expand=1)

    def draw_plot(self, fig):
        categories = list(self.inventory_data.keys())
        values = list(self.inventory_data.values())
        bars = plt.bar(categories, values, color='#0032A0', width=0.4)
        plt.xlabel("Hardware")
        plt.ylabel("Quantity")
        plt.title("Hardware Stock")
        plt.xticks(rotation=45)  # Rotate x-axis labels by 45 degrees for better readability

        # Annotate each bar with its count
        for bar in bars:
            height = bar.get_height()
            plt.annotate('{}'.format(height),
                         xy=(bar.get_x() + bar.get_width() / 2, height),
                         xytext=(0, 3),  # 3 points vertical offset
                         textcoords="offset points",
                         ha='center', va='bottom')

        # Adjust margins to prevent title from getting cut off
        plt.tight_layout()
    def handle_event(self, event):
        if event == sg.WINDOW_CLOSED or event == "Exit":
            return State.HOME_SCREEN
        return None

I have tried using the window Resizable paramenter and setting it to False.

0

There are 0 answers