我对python和Tkinter还是很陌生,并且我无法想到在您提出的示例中该文件数据正在发生什么问题。有人可以说服我吗?我计划在CeleRacing的软件中使用它,该项目已经进行了一年。
我正在使用python 3和matplotlib在Spyder 3.7中工作。我尝试了替代方法,但没有一个能如我所愿。目的是输入一个结构如下的txt文档:
0,3.15
1,6.43
2,5.97
3,4.88
etc.
并将其绘制在图形中,该图形每1/10秒更新一次,因为该文档将填充汽车速度的转换数据。
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk )
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import ttk
from tkinter.filedialog import askopenfilename
link,warn_list,frame_id, timeStamp=[[] for _ in range(4)]
root= tk.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=tk.Text(frame1,width=45, height=15,bg='lightgray')
text_static.pack(side='left',fill='both')
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()
menu=tk.Menu(root)
root.config(menu=menu)
file=tk.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=NavigationToolbar2Tk(canvas,frame1)
toolbar.update()
canvas._tkcanvas.pack()
root.mainloop()
和
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()
重复的部分是我无法拼凑的部分,因为该代码不是我的,而是由PRMoureu和cbhush制作的。