2017-07-06 9 views
1

ユーザはログファイルをロードするGUIを持っており、ファイル内の警告とエラーが強調表示されたフレーム上のログファイルを見ることができます。tkinterのファイルからの入力に基づいてグラフを作成する

また、ユーザーが選択したこのログファイルのIDとタイムスタンプを含むグラフを作成して、このファイルのデータを視覚化しようとしています。 matplotlibを使って別にグラフを作成できました。plt.show()を使ってグラフを表示しています。

しかし、私はtkinter guiに埋め込むことができません。私は多くのことを試しましたが、実際の棒グラフではなく、軸を得ることができました。ユーザーがロードするファイルを選択すると、ログファイルが表示されますが、キャンバス領域には軸だけが表示され、プロットは表示されません。ここで

は、私のコードの一部です:

import matplotlib 

matplotlib.use('TkAgg') 

from matplotlib.backends.backend_tkagg import \ 
    FigureCanvasTkAgg,NavigationToolbar2TkAgg 

from matplotlib.figure import Figure 

import matplotlib.pyplot as plt 

from tkinter import * 

from tkinter import ttk 

from tkinter.filedialog import askopenfilename 

link,warn_list,frame_id, timeStamp=[[] for _ in range(4)] 

root= Tk() 

Title=root.title("Tool") 

label=ttk.Label(root, text="Welcome",foreground='purple',font=("Times 20 bold italic")) 

label.pack() 

frame1=ttk.LabelFrame(root,labelanchor=NW,height=500,width=500,text='Static Information') 

frame1.pack(fill=BOTH,expand=True) 

text_static=Text(frame1,width=45, height=15,bg='lightgray') 

text_static.pack(side=LEFT,fill=BOTH) 

def loadfile(): 

    filename=askopenfilename(parent=root,filetypes=(("Text File","*.txt"), ("All Files","*.*")),title='Choose a file') 
    with open(filename, 'r')as log: 
     for num,line in enumerate(log,1): 
      if line.find("cam_req_mgr_process_sof")!=-1: 
        line=line.split() 
        frame_id.append(line [-1]) 
        timeStamp.append(line[3].replace(":", "")) 


menu=Menu(root) 
root.config(menu=menu) 

file=Menu(menu) 

file.add_command(label='Load', command=loadfile) 

file.add_command(label='Exit',command=root.destroy) 

menu.add_cascade(label='Select an option:', menu=file) 

def graph(): 

    fig=plt.Figure()    
    x=frame_id #can use x=range(1,38) 
    y=[1]*len(x) 

    ax=fig.add_subplot(111) 
    ax.bar(x,y,width=0.5, color='lightgreen') 
    return fig 

plot=graph() 

canvas=FigureCanvasTkAgg(plot,frame1) 

canvas.get_tk_widget().pack() 

toolbar=NavigationToolbar2TkAgg(canvas,frame1) 

toolbar.update() 

canvas._tkcanvas.pack() 

root.mainloop() 

see the result here

答えて

0

ほとんど作品をグラフ描画します!しかし、ファイルをロードすることではありません。問題は、新しいデータセットでキャンバスを更新しないで数値にキャストしないことです(浮動小数点型整数)

このツールを設計するには、関数内でframe_idcanvasを参照する必要があるからです。 1つまたは2つのクラスでこれをより簡単に解決できます。私はこの機能を示唆してクイックフィックスとして

def loadfile(): 
    global frame_id # reach outside scope to use frame_id 
    frame_id = [] 
    filename=askopenfilename(parent=root, 
          filetypes=(("Text File","*.txt"), 
             ("All Files","*.*")), 
          title='Choose a file') 
    with open(filename, 'r')as log: 
     for num,line in enumerate(log,1): 
      if line.find("cam_req_mgr_process_sof")!=-1: 
        line=line.split() 
        frame_id.append(float(line [-1])) # cast to float 
        timeStamp.append(line[3].replace(":", "")) 
    newplot = graph() 
    newplot.canvas = canvas 
    canvas.figure = newplot 
    canvas.draw() 
関連する問題