Python Forum
Tkinter Matplotlib - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Tkinter Matplotlib (/thread-29959.html)



Tkinter Matplotlib - Nepo - Sep-27-2020

I just created a tkinter app that diplays numbers separated with semicolons as graphs. The numbers for x and y are inserted in two textboxes and then plotted. The plots can also be refreshed. I use subplots but when I start the tkinter app, It also opens the subplots without the menu in a separate window. I only want the main window with the buttons and textfields to be displayed.
Here is my code
import matplotlib
matplotlib.use('TkAgg')
import tkinter as tk
from tkinter import * 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,  
NavigationToolbar2Tk) 

class Plotwindow():
    def __init__(self, masterframe, size):
        
        (w,h)=size
        inchsize=(w/25.4, h/25.4)
        #self.figure = Figure(inchsize)
        #self.ax1 = self.figure.add_subplot(111)
        #self.ax2 = self.figure.add_subplot(221)
        
        self.figure, (self.ax1, self.ax2) = plt.subplots(1, 2)
        
        # create canvas as matplotlib drawing area
        self.canvas = FigureCanvasTkAgg(self.figure, master=masterframe)
        self.canvas.get_tk_widget().pack()
        toolbar = NavigationToolbar2Tk(self.canvas, masterframe) 
        toolbar.update() 
  
        # placing the toolbar on the Tkinter window 
        self.canvas.get_tk_widget().pack() 

    
    def plotxy(self, x, y):
        self.ax1.plot(x,y)
        self.ax2.scatter(x,y)
        #self.axes.plot(x,y)
        self.canvas.draw()
    
    def clearplot(self):
        self.ax1.cla()
        self.ax2.cla()
        self.canvas.draw()
        
# class Generatetestdata():
#     def __init__(self):
#         self.index=0 # index of function call
#         self.xmin=0.0
#         4
#         self.xmax=10.0
#         self.nbvalues=500
   
#     def getxy(self):
#         n=self.index
#         x=np.linspace(self.xmin, self.xmax, self.nbvalues)
#         y=np.sin(4*x)*np.exp(-n*x/10)
#         self.index+=1
#         #self.xmax+=5.0
#         return x,y
    
    
class getnumbersfromtextbox():
    def getTextInput(self):
        result=textExample.get("1.0","end")
        return(list(map(float,result.split(";"))))

    def getTextInput2(self):
        result=textExample2.get("1.0","end")
        return(list(map(float,result.split(";"))))
        
    
def plotdata():
    x=dattext.getTextInput()
    y=dattext.getTextInput2()
    # x,y=datgen.getxy()
    pw.plotxy(x,y)


def clear():
    pw.clearplot()

if __name__ == "__main__":
    #datgen = Generatetestdata()
    dattext = getnumbersfromtextbox()
    root = tk.Tk()
    mf= tk.Frame()
    pw=Plotwindow(mf,(200,150))
    mf.pack()
    bf=tk.Frame()
    b1=tk.Button(bf,text="Plot", command=plotdata)
    b1.pack(side=tk.LEFT)
    b2=tk.Button(bf,text="Clear", command=clear)
    b2.pack(side=tk.LEFT)
    bf.pack()
    
    textExample=tk.Text(mf, height=2)
    textExample.pack()

    textExample2=tk.Text(mf, height=2)
    textExample2.pack()
    root.mainloop()



RE: Tkinter Matplotlib - Gribouillis - Sep-27-2020

Shouldn't mf and bf be children of root?