python - 在 tkinter 中嵌入 matplotlib 图的问题
问题描述
我正在尝试将动画图嵌入到我的 GUI 中,但是,每当我尝试根据有关使用画布的教程所看到的内容时,我都会得到一个空白的 tkinter 屏幕。没有错误,什么都没有。
但是,当我使用 plt.show 时,它可以正常工作,但并不局限于 GUI。这是问题所在。
它可能是一个快速修复,但这就是我所拥有的。
任何帮助,将不胜感激!
class popupWindowOscil(tk.Frame):
def __init__(self,master,ser):
OscilTop= self.OscilTop= Toplevel(master)
tk.Frame.__init__(self)
self.ser = ser
self.fig = plt.figure()
self.ax = self.fig.add_subplot(1, 1, 1)
self.xs = []
self.ys = []
self.xval =0
self.OscilLoop()
def OscilLoop(self):
ani = animation.FuncAnimation(self.fig, self.Oscilliscope, fargs=(self.xs, self.ys))
#self.canvas = FigureCanvasTkAgg(self.fig, self)
#self.canvas.draw()
#self.canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
plt.show()
return
# The function that creates the values for the plot.
def Oscilliscope(self,i,xs,ys):
tryagain =1
while tryagain == 1:
try:
reading = self.ser.readline().decode()
tryagain = 0
except UnicodeDecodeError:
pass
Incominglist = str(reading).split(",")
try:
numbers = [float(x) for x in Incominglist]
except ValueError:
print ('Failure during string decode, Restart and Try again')
return
# Add x and y to lists
self.ys.extend(numbers)
for val in range(len(Incominglist)):
if self.xval == 0 and val ==0:
self.xs.append(self.xval) # or any arbitrary update to your figure's data
else:
self.xval += 0.005
self.xs.append(self.xval)
# Draw x and y lists
self.ax.clear()
self.ax.plot(self.xs, self.ys)
# Format plot
self.ax.yaxis.set_ticks(np.arange(0,5,0.25))
plt.subplots_adjust(bottom=0.30)
解决方案
推荐阅读
- java - WebSocketListener OnMessage 被阻止并且其他传入消息被排队
- laravel - 如何删除服务器上的“public/index.php”?
- java - apache-commons-csv println 方法不在输出中打印换行符
- python - 是否有类似于 R 的 brglm 的东西来帮助使用 statsmodels Logit 处理 Python 中的准分离?
- http-headers - 如何在 WSO2 APIM 2.6 中使用运行时值设置标头中介值?
- android - AndoridManifest 权限被忽略
- c# - Unity:附加到 DontDestroyOnLoad 对象的子对象的脚本被调用两次
- spring - 是否有任何来源可以搜索 Spring Boot 启动器依赖项?
- php - 按值对多维数组进行排序,如果优先级值为 0,则不包括排序
- google-maps - 我们可以在 GoogleMap 中初始化CameraPosition - Flutter