0

This is the line that helps me plot on python.

packing_options[best_index].plot_sheets()

This is how it looks on python. This is the picture of the graph. https://i.stack.imgur.com/zcVXJ.jpg

Now, I am trying tkinter. I want the graph to pop up. How can I do this ?

window = tk.Tk()
packing_options[best_index].plot_sheets()
window.mainloop()

I tried this. But didn t work.

Edited : So, "matplotlib is to be used as someone commented. Here is the code :

def plot_sheet(self):
    fig,ax = plt.subplots(1)
    ax.set_xlim([0, self.W]) 
    ax.set_ylim([0, self.L]) 
    recs = []
    for i in range(len(self.rect_list)):
        if self.rect_rotate[i]:
            ax.add_patch(patches.Rectangle((self.rect_pos[i][0], self.rect_pos[i][1]), self.rect_list[i].l, self.rect_list[i].w,linewidth=3,edgecolor='r'))
        else:
            ax.add_patch(patches.Rectangle((self.rect_pos[i][0], self.rect_pos[i][1]), self.rect_list[i].w, self.rect_list[i].l,linewidth=3,edgecolor='r'))
    plt.show()

def plot_sheets(self):
    for i in range(len(self.sheets)):
        self.sheets[i].plot_sheet()

this is the code for plotting. packing_options[best_index] is also a function here. and it plots around 10-20 plots as there is a loop. How do I apply matplotlib backend here?

Devin Maharjan
  • 109
  • 2
  • 10
  • do you want your image on a new window? If so then have a look at [TopLevel widget](http://http://effbot.org/tkinterbook/toplevel.htm) – FrainBr33z3 Apr 15 '19 at 08:55
  • Not the image, but the graph. – Devin Maharjan Apr 15 '19 at 09:00
  • for the graph, you need to use the matplotlib backend for tkinter. An example can be found [here](https://matplotlib.org/gallery/user_interfaces/embedding_in_tk_sgskip.html). Place your canvas on a TopLevel window and draw your graph there. – FrainBr33z3 Apr 15 '19 at 09:19
  • is there any other way for that pop up? – Devin Maharjan Apr 15 '19 at 10:43
  • nope, there is no other way for pop-up windows in tkinter. Implementing the method above is fairly starightforward. – FrainBr33z3 Apr 15 '19 at 11:29
  • How do I apply these lines ? fig = Figure(figsize=(5, 4), dpi=100) t = np.arange(0, 3, .01) fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t)) canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea. canvas.draw() – Devin Maharjan Apr 15 '19 at 12:03
  • yesterday was question how to put matplotlib+seaborn plot in tkinter window. And I put example code. – furas Apr 15 '19 at 12:08
  • @DevinMaharjan: Relevant [tkinter-embed-matplotlib-in-gui](https://stackoverflow.com/questions/4073660/python-tkinter-embed-matplotlib-in-gui) – stovfl Apr 15 '19 at 12:09
  • [How can I integrate Seaborn plot into Tkinter GUI](https://stackoverflow.com/questions/55680827/how-can-i-integrate-seaborn-plot-into-tkinter-gui/55681117#55681117) - it shows how to get `figure` and put in tkinter window. – furas Apr 15 '19 at 12:11

1 Answers1

1

I can't run it but it could be somthing like this

import matplotlib
matplotlib.use('TkAgg')

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

import matplotlib.pyplot as plt

import tkinter as tk

class YourClass():

    def plot_sheet(self):
        fig,ax = plt.subplots(1)
        ax.set_xlim([0, self.W]) 
        ax.set_ylim([0, self.L]) 
        recs = []
        for i in range(len(self.rect_list)):
            if self.rect_rotate[i]:
                ax.add_patch(patches.Rectangle((self.rect_pos[i][0], self.rect_pos[i][1]), self.rect_list[i].l, self.rect_list[i].w,linewidth=3,edgecolor='r'))
            else:
                ax.add_patch(patches.Rectangle((self.rect_pos[i][0], self.rect_pos[i][1]), self.rect_list[i].w, self.rect_list[i].l,linewidth=3,edgecolor='r'))
        #plt.show()
        return fig

#--- main ---

window = tk.Tk()

packing_options = [YourClass(), YourClass(), YourClass()]
best_index = 0

fig = packing_options[best_index].plot_sheets()

dataPlot = FigureCanvasTkAgg(fig, master=master)
dataPlot.show()
dataPlot.get_tk_widget().pack(side='top', fill='both', expand=1) 

window.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148
  • This was helpful. But my case has like 15-20 graphs. So, when I triggered this command, all the plots showed up at once. Is there a way to show maybe with a next button or something? – Devin Maharjan Apr 15 '19 at 12:44
  • you would have to create plots as separated `figures` and then you can display one figure, and you can add two `tk.Button` to display next/previous `figure` – furas Apr 15 '19 at 12:48
  • is there any reference for it? I am having a hard time finding it. – Devin Maharjan Apr 15 '19 at 14:04
  • in my [examples on GitHub](https://prs.moh.gov.sg/prs/internet/profSearch/mshowSearchSummaryByName.action) I found link to example in documentation [Embedding In Tk¶](https://matplotlib.org/gallery/user_interfaces/embedding_in_tk_sgskip.html) – furas Apr 15 '19 at 14:23
  • I am getting this error : NameError: name 'master' is not defined. changed it to self/canvas. Still the same. – Devin Maharjan Apr 16 '19 at 07:18
  • now I see: it has to be `master=window` in `FigureCanvasTkAgg` because there is `window = tk.Tk()` – furas Apr 16 '19 at 11:16
  • Another error popped up now : AttributeError: 'NoneType' object has no attribute 'set_canvas' – Devin Maharjan Apr 16 '19 at 11:43
  • which line makes problem ? did you add `return fig` in `plot_sheet` ? – furas Apr 16 '19 at 12:21
  • maybe create new question on new page and you will have more space to put full code and full Traceback (error message). – furas Apr 16 '19 at 12:22
  • https://stackoverflow.com/questions/55707850/error-in-matplotlib-popup-window-attributeerror-nonetype-object-has-no-attri – Devin Maharjan Apr 16 '19 at 12:23
  • Please check the above link of the new question – Devin Maharjan Apr 16 '19 at 12:30
  • These lines fig = packing_options[best_index].plot_sheets() dataPlot = FigureCanvasTkAgg(fig, master=master) dataPlot.show() dataPlot.get_tk_widget().pack(side='top', fill='both', expand=1) are inside a function. does that make a difference? – Devin Maharjan Apr 16 '19 at 13:01