- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Arduino 通过 I2C 与传感器进行通信。从传感器获取数据后,Arduino 通过串行通信将其以字符串形式输出。该字符串看起来像这样:
"-3.46,-8.4,4.64,7.5,35.3,4.32,-9.0,\r\n"
然后,我使用 Python 读取传入数据,将其分段(丢弃 \r\n
),然后通过 实时显示用户选择的数据集matplotlib.animation
。在我的计算机上使用该脚本时,它工作正常,但是当尝试在另一台计算机上使用它时,我收到 Tkinter 回调异常错误。
Python 脚本:
port = 'com4' # Configure which port the Arduino is connected to.
baud = 19200 # Configure baud rate
ind = 0 # Determine which data point you want displayed on the graph. [0-6]
import numpy as np
from matplotlib.lines import Line2D
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import serial
import time
import re
import xlwt
if serial.Serial(port,baud).is_open:
serial.Serial(port,baud).close()
ser = serial.Serial(port,baud,timeout=1)
ser.reset_input_buffer()
ser.readline()
class Scope(object):
def __init__(self, ax, maxt=4, dt=0.04):
self.ax = ax
self.dt = dt
self.maxt = maxt
self.tdata = [-dt]
self.ydata = [0]
self.line = Line2D(self.tdata, self.ydata)
self.ax.add_line(self.line)
self.ax.set_ylim(yMin, yMax)
self.ax.set_xlim(0, self.maxt)
def update(self, y):
lastt = self.tdata[-1]
if lastt > self.tdata[0] + self.maxt: # reset the arrays
self.tdata = [self.tdata[-1]]
self.ydata = [self.ydata[-1]]
self.ax.set_xlim(self.tdata[0], self.tdata[0] + self.maxt)
self.ax.figure.canvas.draw()
t = self.tdata[-1] + self.dt
self.tdata.append(t)
tArray.append(t)
self.ydata.append(y)
self.line.set_data(self.tdata, self.ydata)
return self.line,
def emitter():
while True:
val = ser.readline()
allData = val.split(",")
yArray.append(allData[:-1]) # discarding return line characters from the serial read.
yield allData[ind]
fig, ax = plt.subplots()
scope = Scope(ax)
# pass a generator in "emitter" to produce data for the update func
ani = animation.FuncAnimation(fig, scope.update, emitter, interval=10,blit=True)
plt.show()
print yArray
错误消息:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1542, in __call__
return self.func(*args)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 592, in callit
func(*args)
File "C:\Python27\lib\site-packages\matplotlib\backends\_backend_tk.py", line 88, in _on_timer
TimerBase._on_timer(self)
File "C:\Python27\lib\site-packages\matplotlib\backend_bases.py", line 1373, in _on_timer
ret = func(*args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 1481, in _step
still_going = Animation._step(self, *args)
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 1217, in _step
self._draw_next_frame(framedata, self._blit)
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 1237, in _draw_next_frame
self._post_draw(framedata, blit)
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 1260, in _post_draw
self._blit_draw(self._drawn_artists, self._blit_cache)
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 1275, in _blit_draw
a.axes.draw_artist(a)
File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 2622, in draw_artist
a.draw(self._cachedRenderer)
File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\lines.py", line 738, in draw
self.recache()
File "C:\Python27\lib\site-packages\matplotlib\lines.py", line 656, in recache
yconv = self.convert_yunits(self._yorig)
File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 200, in convert_yunits
return ax.yaxis.convert_units(y)
File "C:\Python27\lib\site-packages\matplotlib\axis.py", line 1526, in convert_units
ret = self.converter.convert(x, self.units, self)
File "C:\Python27\lib\site-packages\matplotlib\category.py", line 65, in convert
unit.update(values)
AttributeError: 'NoneType' object has no attribute 'update'
我已将其精确定位到 scope.update
中的 if
语句,具体来说:
self.tdata = [self.tdata[-1]]
self.ydata = [self.ydata[-1]]
因为当注释掉这些行时,程序会继续运行(尽管毫无意义,因为它不会更新图表)。
在工作系统上运行python --version && pip freeze
会导致:
Python 2.7.13
appdirs==1.4.3
certifi==2018.1.18
chardet==3.0.4
configobj==5.0.6
cycler==0.10.0
Cython==0.28
decorator==4.0.11
enum34==1.1.6
et-xmlfile==1.0.1
functools32==3.2.3.post2
future==0.16.0
idna==2.6
ipython-genutils==0.2.0
iso8601==0.1.12
jdcal==1.3
matplotlib==2.0.2
mpltools==0.2.0
mpmath==0.19
nose==1.3.7
numpy==1.13.0rc1+mkl
openpyxl==2.4.7
packaging==16.8
panda==0.3.1
pandas==0.22.0
patsy==0.5.0
pyparsing==2.2.0
pyperclip==1.5.27
pyserial==3.4
python-dateutil==2.6.0
pytz==2017.2
PyVISA==1.8
pywin32==221
PyYAML==3.12
requests==2.18.4
scikit-learn==0.19.1
scipy==0.19.0
seaborn==0.8.1
six==1.10.0
sklearn==0.0
sympy==1.0
traitlets==4.3.2
urllib3==1.22
vispy==0.5.3
xlwt==1.3.0
...在非工作系统上:
Python 2.7.13
backports.functools-lru-cache==1.5
cycler==0.10.0
et-xmlfile==1.0.1
jdcal==1.3
kiwisolver==1.0.1
matplotlib==2.2.2
numpy==1.13.1
openpyxl==2.4.8
pyparsing==2.2.0
pyserial==3.4
python-dateutil==2.7.2
pytz==2018.4
six==1.11.0
xlwt==1.3.0
最佳答案
解决了!
通过将不工作计算机的 matplotlib
版本从 2.2.2
恢复到 2.0.2
问题可以得到缓解>。虽然这解决了问题,但我不确定问题背后的根本原因是什么。
关于python - Tkinter回调异常: matplotlib animation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49944878/
在 How to make a Tkinter window jump to the front? 中提出的问题之后的一个问题 我希望有一个顶层窗口(我用它来导航我的其他主窗口)总是在前面。但我希望它
有没有办法在 Tkinter 中保持小部件(特别是图像)的堆叠顺序一致?例如,我可能在 Canvas 上的同一位置有两个矩形、两个三角形和一个圆。圆圈移动到最后一次点击鼠标的地方,但我总是希望它被绘制
这是一个简单的 GUI 程序,用于创建 5x16 按钮矩阵。 from tkinter import * root = Tk() button = [[0 for x in range(16)] fo
有一个错误:“AttributeError: module 'tkinter' has no attribute 'messagebox'” 即使 import tkinter 一开始就已经给出了,为
我知道 menu.tk_popup() 可用于在特定坐标处打开上下文菜单,但也不知道如何从中打开子菜单,如果这有意义的话。这是我编写的代码: import tkinter as tk root = t
我正在尝试在禁用自动换行和水平滚动条的文本窗口中书写,如下所示: root = Toplevel() root.geometry("%dx%d+0+0" % (350,400)) af=Frame(r
已经将文本变量分配给小部件后,如何将其删除? widget.config(textvariable=None)只是不工作。在谷歌或这里找不到任何东西。 最佳答案 将您的变量分配给一个空字符串以实现此目
Jython 支持 Tkinter 吗?如果我用 Python 编写一个程序并放一个 使用 Tkinter 的 GUI 前端,做同样的事情有多难 Jython 中的程序?或者对于 Jython GUI
因此,我尝试创建一个 tkinter 窗口,显示当前时间和日期以及自定义短语。不过,我遇到的问题是,我似乎无法在第二天刷新日期。 我可以传递截至运行代码时的当前日期,但之后它变为静态。 这是我目前的程
我的理解是在初始化 __init__ 中的所有框架和小部件之后方法,tkinter 窗口会调整大小以适合所有这些组件。 我想将窗口的初始化大小设置为其最小大小。我希望能够最大化并放大窗口,但我从不希望
此代码仅水平居中,如何使进度条也垂直居中? import Tkinter import ttk root = Tkinter.Tk() root.geometry("=500x500") root.p
使用 Python 2.7 和 Tkinter 模块,我创建了一个菜单按钮并为其分配了一个菜单。现在每次我在特定位置发布菜单时,菜单的宽度都会根据字符数自动设置。有没有办法在菜单小部件中设置静态宽度?
我想将我的 tkinter 应用程序的主题更改为 clam。 代码是什么,我把它放在哪里?我试过了: from tkinter import * from tkinter.ttk import * s
我有以下代码: from Tkinter import * from urllib import urlretrieve import webbrowser import ttk def get_la
我知道,如果我将滚动条控制的框架绑定(bind)到函数 ( onFrameConfigure ),您可以获得滚动条位置,如下所示:self.calendar_frame.bind("", self.o
许多网站都说菜单小部件有一个选项“字体”,但我一直无法设置它。系统是在 Windows 8.1 中运行的 Python 3.5。脚本开始: 从 tkinter 导入 * 根 = Tk() root.g
我正在阅读本教程,它帮助我同时学习 tkinter 和 wxWidgets,但我想深入挖掘,所以想知道哪个 GUI 工具更适合深入学习,为什么? 最佳答案 不可能说哪个“更好”。两者均可用于最常见的用
看书学python,tkinter.END用在一段代码里不用解释 import tkinter def count(text, out_data): """ Update out_data w
我正在尝试使用 Python 2.7 将 Tkinter 导入到我的项目中,但我收到了错误: ImportError: No module named tkinter 在有人说之前,我已经尝试了“Tk
当我回答 Tkinter 问题时,我通常会尝试自己运行代码,但有时我会收到此错误: Traceback (most recent call last): File "C:\Python27\pyg
我是一名优秀的程序员,十分优秀!