简体   繁体   中英

How to resize PySimpleGUI canvas with matplotlib

I'm trying to change the size of my bar chart and having some difficulty. The bars are plotting correctly from my data, but the canvas does not increase in size when I adjust the size argument. I'm not getting any errors, so I'm not sure what I'm missing.

    # define the window layout
    layoutMain = [[sg.Text('Member Access')],
            [sg.Menu(mainmenu_def, pad=(0,0))],
            [sg.Button('Store To Inventory', size = (17,1)), sg.Button('Retrieve From Inventory', size = (17,1)), sg.Button('Inventory Details', size = (17,1)), sg.Button('Exit to Home', button_color = '#36454f', size = (17,1))],
            [sg.Text('E-Stock', font='Any 18')],
            [sg.Canvas(size=(100, 100), key='-CANVAS-')]]
    windowMain = sg.Window('E-Stock', layoutMain, no_titlebar=False, size=(1000,600), finalize=True, resizable=True)
    windowMain.maximize()

    # add the plot to the window
    fig_photo = draw_figure(windowMain['-CANVAS-'].TKCanvas, fig)

Here's example, I define the size of sg.Canvas by the figsize and dpi in matplolib figure.

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import PySimpleGUI as sg


matplotlib.use('TkAgg')
w, h = figsize = (5, 3)     # figure size
fig = matplotlib.figure.Figure(figsize=figsize)
dpi = fig.get_dpi()
size = (w*dpi, h*dpi)       # canvas size
t = np.arange(0, 3, .01)
fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t))

def draw_figure(canvas, figure):
    figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
    figure_canvas_agg.draw()
    figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
    return figure_canvas_agg

layout = [[sg.Text('Plot test')],
          [sg.Canvas(size=size, key='-CANVAS-')],
          [sg.Button('Ok')]]

window = sg.Window('Embedding Matplotlib', layout, finalize=True, element_justification='center', font='Helvetica 18')
fig_canvas_agg = draw_figure(window['-CANVAS-'].TKCanvas, fig)

event, values = window.read()

window.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM