【发布时间】:2019-01-06 08:20:15
【问题描述】:
我有一个带有实时图形读取串行数据的 GUI,对我的业余技能来说非常好,但是有人可以向我解释如何摆脱这个错误吗?
File "C:\Users\222\Desktop\V25 Live graph.py", line 200, in measure
cnt=cnt+1
UnboundLocalError: local variable 'cnt' referenced before assignment
我尝试的每件事都会弄乱我的整个项目。谢谢大家的建议,代码如下。
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
import tkinter as tk
import time
import serial
from tkinter import *
from tkinter import ttk
import numpy # Import numpy
import pandas as pd
import numpy as np
import sys
import matplotlib.pyplot as plt #import matplotlib library
from drawnow import *
arduinoData = serial.Serial('com15', 115200) #Creating our serial object named arduinoData
plt.ion() #Tell matplotlib you want interactive mode to plot live data
cnt=0
style.use("ggplot") #ggplot...dark_background
tempF= []
# Main Tkinter application
class Application(Frame):
# Init the variables & start measurements
def __init__(self, master=None):
Frame.__init__(self, master)
root.title( "Stylibleue Dashboard")
root.state('zoomed') # This makes full screen on a windows machines
self.grid(row=0, column=0, sticky="nsew")
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=3)
self.columnconfigure(1, weight=3)
self.columnconfigure(2, weight=3)
self.columnconfigure(3, weight=3)
self.columnconfigure(4, weight=3)
self.B11 = StringVar()
self.B2 = StringVar()
self.createWidgets()
self.pack()
self.measure()
# Create display elements
def createWidgets(self):
self.temperature = Label(self, text="", font=('Verdana', 20)).grid(row=5, column=0,padx=100,pady=200)
self.temperature = Label(self, text="", font=('Verdana', 20)).grid(row=5, column=1,padx=100)
self.temperature = Label(self, text="", font=('Verdana', 20)).grid(row=5, column=2,padx=100)
self.temperature = Label(self, text="", font=('Verdana', 20)).grid(row=5, column=3,padx=100)
self.temperature = Label(self, text="", font=('Verdana', 20)).grid(row=5, column=4,padx=100)
self.temperature = Label(self, text="", font=('Verdana', 20)).grid(row=5, column=5,padx=100)
ttk.Button(self, text='Graph', command=self.makeFig).grid(row=2, column=5, sticky=E, pady=50)
ttk.Separator(self,orient=HORIZONTAL).grid(row=1, columnspan=6, sticky="ew")
self.temperature = Label(self, text="Bassin 2 :", font=('Verdana', 20)).grid(row=2, column=0, sticky= W ,columnspan=2)
self.temperature = Label(self, text="DO :", font=('Verdana', 20)).grid(row=2, column=1, sticky="nsew")
self.temperature = Label(self, text="TEMP :", font=('Verdana', 20)).grid(row=2, column=3, sticky="nsew")
def makeFig(self):
plt.title('My Live Streaming Sensor Data') #Plot the title
plt.grid(True) #Turn the grid on
plt.ylabel('Temp F') #Set ylabels
plt.plot(tempF, label='Degrees F') #plot the temperature
plt.legend(loc='upper left') #plot the legend
# Read the incoming serial data and display it
def measure(self):
if(arduinoData.inWaiting()>0):
data = arduinoData.readline()
data =str(data,'utf-8') #removes the surrounding rubbish
self.B11.set(str(data))
self.temperature = Label(self, textvariable=self.B11, font=('Verdana', 20)).grid(row=2, column=4, sticky="nsew")
self.B2.set(str(data))
self.temperature = Label(self, textvariable=self.B2, font=('Verdana', 20)).grid(row=2, column=2, sticky="nsew")
tempF.append(data) #Build our tempF array by appending temp readings
print(data)
# Wait 1 second between each measurement
self.after(1000,self.measure)
arduinoData.flushOutput()
arduinoData.flushInput()
drawnow(self.makeFig) #Call drawnow to update our live graph
plt.pause(.000001) #Pause Briefly. Important to keep drawnow from crashing
cnt=cnt+1
if(cnt>5000): #If you have 50 or more points, delete the first one from the array
tempF.pop(0) #This allows us to just see the last 50 data points
# Create and run the GUI
root = Tk()
app = Application(master=root)
app.mainloop()
【问题讨论】:
标签: python python-3.x matplotlib tkinter