0

I have been working with a few programs and currently am trying to use Matplotlib to put a graph inside an interface made using PySimpleGui. I have two programs which attempt to plot the same graph, one works, the other doesn't. I have shown the code below but skipped some of the coding such as imports and 'location' and 'done' so the below can be read easier, but I skipped the same code in both.

The first program is below and correctly opens the graph inside a pysimplegui window. sg is pysimplegui.

fig=gantt(location,done)              #this is defined in another program, and makes the figure

figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds   

layout = [[sg.Text('Plot test', font='Any 18')],  
[sg.Canvas(size=(figure_w, figure_h), key='-CANVAS-')],  
[sg.OK(pad=((figure_w / 2, 0), 3), size=(4, 2))]] 

window = sg.Window('name', layout, force_toplevel=True, finalize=True)  

fig_photo = draw_figure(window['-CANVAS-'].TKCanvas,fig)   
event, values = window.read()  

window.close()

The next program does not show the graph. It has a main window with a button to open a graph window, and the graph window has a graph. However, there is an error.

fig=gantt(location,done) 

figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds


def window_graph(): 
    layout_graph=[
        [sg.Text('graph')],
        [sg.Button('close graph')],
        [sg.Canvas(size=(figure_w, figure_h), key='-CANVAS-')],
    ]
    return sg.Window('graph', layout_graph, margins=(100,50), modal=True)    

fig_photo = draw_figure(window_graph['-CANVAS-'].TKCanvas, fig)                                     

def open_window_graph():    
    graph_window=window_graph()        
    while True:
        event, values = graph_window.read()           
        if event=="close graph" or event==sg.WIN_CLOSED:
            graph_window.close()
            break
        graph_window.close()

layout_main=[
    [sg.Text("first page")],
    [sg.Button('open')],
    [sg.Button('close')]
]

window_main=sg.Window('main',layout_main, margins=(500,400))
while True:
    event,values=window_main.read()
    if event=='open':
         open_window_graph()
    elif event=='close':
       break 
    else:
        break

The error I get is;

fig_photo = draw_figure(window_graph['-CANVAS-'].TKCanvas, fig)                                     
TypeError: 'function' object is not subscriptable

If I remove this line, then the program works except the second window doesn't have a graph. It is the right size, so I assume the figure_w and figure_h are working.

1 Answer 1

0

window_graph is a function name, you can call it by window_graph(), and not the window_graph['-CANVAS-'], that's why you got TypeError: 'function' object is not subscriptable.

def window_graph(): 
    layout_graph=[
        [sg.Text('graph')],
        [sg.Button('close graph')],
        [sg.Canvas(size=(figure_w, figure_h), key='-CANVAS-')],
    ]
    return sg.Window('graph', layout_graph, margins=(100,50), modal=True)    

fig_photo = draw_figure(window_graph['-CANVAS-'].TKCanvas, fig)  

Change it to

def window_graph():
    layout_graph=[
        [sg.Text('graph')],
        [sg.Button('close graph')],
        [sg.Canvas(size=(figure_w, figure_h), key='-CANVAS-')],
    ]
    window = sg.Window('graph', layout_graph, margins=(100,50), modal=True)
    fig_photo = draw_figure(window['-CANVAS-'].TKCanvas, fig)
    return window
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.