Окно в PysimpleGui изменяет размер после использования библиотеки Matplot ⇐ Python

Программы на Python
Anonymous
Окно в PysimpleGui изменяет размер после использования библиотеки Matplot

Сообщение Anonymous »

Я пытаюсь создать конечный автомат, используя графический интерфейс, импортированный из Pysimplegui. У меня есть главный экран с кнопкой «Просмотреть доступный товар». Затем я перехожу к приведенному ниже коду, но при этом размер исходного домашнего экрана изменяется с 1024x600 вниз. Как мне вызвать этот код, не изменяя размер моего текущего графического интерфейса.

Код: Выделить всё

-- 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
Я попробовал использовать параметр Window Resizable и установить для него значение False.

Подробнее здесь: https://stackoverflow.com/questions/782 ... ib-is-used

Вернуться в «Python»