Hi, (CCing Python Tutor again). I meant something like below. I am sending this from my phone, which is cumbersome to use an understatement, so the indentation is lost. But I hope you get the idea. You need to inherit from Frame.
Btw, this is useful if you would like to create an interactive graph: http://stackoverflow.com/questions/22052532/matplotlib-python-clickable-points from Tkinter import * import matplotlib matplotlib.use("TkAgg") from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg from matplotlib.figure import Figure class PlotGraph(Frame):. # inherits from Frame def __init__(self, root, xaxis, yaxis): Frame.__init__(self, root, *args, **kwargs) # execute __init__ from super class self.root = root root.title("Plotting a graph") self.xaxis = xaxis self.yaxis = yaxis self.f = Figure(figsize = (5,5), dpi = 90) self.a = self.f.add_subplot(111) self.a.plot(self.xaxis,self.yaxis) self.canvas = FigureCanvasTkAgg(self.f,self.root) self.canvas.show() self.canvas.get_tk_widget().pack(side = TOP, fill = BOTH, expand = True) # Navigation Toolbar: self.toolbar = NavigationToolbar2TkAgg(self.canvas,self.root) self.toolbar.update() self.canvas._tkcanvas.pack() if __name__ == '__main__': root = Tk() root.title( "Creating a plot") xaxis = [1,2,3,4,5,6] yaxis = [7,8,9,10,11,12] my_plot = PlotGraph(root, xaxis, yaxis, relief=SUNKEN) # you can add any of the arguments for the Frame initializer my_plot.grid(row=0, column=0) # use grid here button = Button (root, text="quit", fg='red', command=root.quit) button.grid(row=1, column=0) root.mainloop() ________________________________ From: Pooja Bhalode <poojabhalod...@gmail.com> Sent: Sunday, February 26, 2017 3:47:37 PM To: Albert-Jan Roskam Subject: Re: [Tutor] Matplotlib in Tkinter Hi Albert, Thank you so much for getting back to me. I tried your approach but it still fails for me. I created a class with pack(). But when I try using grid for the Label highlighted below, the gui hangs up and does not show anything. Can you please shed more light on how to use my_plot as one unit and use grid packing for its instances? Thank you so much. Also, does using class with a pack layout help with screen blackening or two plots getting created? Please let me know. Yours truly, Pooja Code: import matplotlib matplotlib.use("TkAgg") from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg from matplotlib.figure import Figure from Tkinter import * root = Tk() Label(root, text = "Creating a plot").pack() class PlotGraph: def __init__(self, root, xaxis, yaxis): self.root = root root.title("Plotting a graph") self.xaxis = xaxis self.yaxis = yaxis self.f = Figure(figsize = (5,5), dpi = 90) self.a = self.f.add_subplot(111) self.a.plot(self.xaxis,self.yaxis) self.canvas = FigureCanvasTkAgg(self.f,self.root) self.canvas.show() self.canvas.get_tk_widget().pack(side = TOP, fill = BOTH, expand = True) # Navigation Toolbar: self.toolbar = NavigationToolbar2TkAgg(self.canvas,self.root) self.toolbar.update() self.canvas._tkcanvas.pack() self.CloseButton = Button(root, text = "Close", command = root.quit) self.CloseButton.pack() xaxis = [1,2,3,4,5,6] yaxis = [7,8,9,10,11,12] my_plot = PlotGraph(root, xaxis, yaxis) root.mainloop() On Sun, Feb 26, 2017 at 1:22 AM, Albert-Jan Roskam <sjeik_ap...@hotmail.com<mailto:sjeik_ap...@hotmail.com>> wrote: (Sorry for top-posting) Hi, I recently ran into the same problem when I wanted to embed an interactive Matplotlib choropleth in my Tkinter app. I solved it by creating a separate class MyPlot for plot + toolbar (it inherited from Tkinter.Frame). MyPlot internally uses the pack geometry manager as given in the example from the mpl website. Then, you use MyPlot as 'one unit', and you use the grid method on its instances. Best wishes, Albert-Jan ________________________________ From: Tutor <tutor-bounces+sjeik_appie=hotmail....@python.org<mailto:hotmail....@python.org>> on behalf of Pooja Bhalode <poojabhalod...@gmail.com<mailto:poojabhalod...@gmail.com>> Sent: Sunday, February 19, 2017 7:36:32 PM To: tutor@python.org<mailto:tutor@python.org> Subject: [Tutor] Matplotlib in Tkinter Hi, I am trying to create a graph in Tkinter window. And following is a snipet of the code. Code: simroot = Toplevel(root) simroot.title("Simulation of experiments") Label(simroot, text = "Displaying concentration profiles with respect to time:").pack(side = TOP) Label(simroot, text = "Select the experiments to be displayed: ").pack(side = TOP) optionexp = ['Experiment 1', 'Experiment 2', 'Experiment 3', 'Experiment 4', 'Experiment 5'] expsim = StringVar() dropexp = OptionMenu(simroot, expsim, *optionexp) dropexp.pack(side = TOP) dropexp.config(width = 15) # Row 1 would print the name of the experiment selected and the species displayed would be in row3. Label(simroot, text = "Select concentration species:").pack(side = TOP) concsim = StringVar() optionlistsim = ['A', 'B', 'C', 'D', 'E'] dropsim = OptionMenu(simroot, concsim, *optionlistsim) dropsim.pack(side = TOP) dropsim.config(width = 8) Label(simroot, text = "").pack(side = LEFT) def Submitplot(): print "Create plot" f = Figure(figsize = (5,5), dpi = 80) a = f.add_subplot(111) a.plot([1,2,3,4,5],[10,11,12,14,15]) canvas = FigureCanvasTkAgg(f, simroot) canvas.show() canvas.get_tk_widget().pack(side = BOTTOM) toolbar = NavigationToolbar2TkAgg(canvas, simroot) toolbar.update() canvas._tkcanvas.pack(side = BOTTOM) Button(simroot, text = "Submit and Plot", command = Submitplot).pack(side = BOTTOM) Here, the output comes as: [image: Inline image 1] The problem is I am not able to use grid layout instead of pack (used here). I want to place the text to the left, top side in the window. Also, when I hover the mouse over the figure and the x and y values are shown, it flickers really fast and the white space in the Tkinter window turns black when it flickers. Can someone please tell me what the issue could be? Also, the toolbar should have been at the bottom but it shows up at the top. I am using pack in this Toplevel of the main root whereas in the rest of the code, I am using grid layout. I switched to pack for this snipet because I could not figure out how grid would work for this part. Can someone please let me know how to correct these issues? Thankyou so much. Pooja _______________________________________________ Tutor maillist - Tutor@python.org<mailto:Tutor@python.org> To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor