我的代码存在问题。我正在尝试从ADC读取电压测量结果并获得每100个读数的平均值,然后在我的默认屏幕上显示平均值。该过程应该重复,标签应该每0.5秒更新一次。但是,我的标签甚至没有显示在屏幕上。
这些是我的代码的一些片段
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
import tkinter as tk
from tkinter import ttk
from matplotlib import pyplot as plt
import time
import Adafruit_ADS1x15
adc = Adafruit_ADS1x15.ADS1115()
GAIN = 1
LARGE_FONT = ('Verdana', 18)
NORM_FONT = ('Verdana', 14)
SMALL_FONT = ('Verdana' , 11)
MENU_FONT= ('Arial', 12, "bold")
def update_label(label):
def getvoltage():
readings = [0]*100
maxsample = 100
counter = 0
while (counter < maxsample):
reading = adc.read_adc(0, gain=GAIN)
readings.append(reading)
counter += 1
avg = sum(readings)/100
voltage = (avg * 0.1259)/100
readings.clear()
label.config(text=str('Voltage: {0:.2f}'.format(voltage)))
label.after(500, getvoltage)
class Controller(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Controller")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0,weight=1)
self.show_frame(DefaultScreen)
class DefaultScreen(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
self.configure(bg="white")
f = Figure(figsize = (10,3), dpi=100)
a = f.add_subplot(111)
a.plot([1,2,3,4,5,6,7,8],[2,4,6,8,10,12,14,16])
button1 = ttk.Button(self,text="Contact Page",
command=lambda:controller.show_frame(ContactPage))
button1.pack(anchor="se", pady=10, padx=10)
ventilatorstatus="Ventilator Status: Closed"
ventstatus= tk.Label(self, text=ventilatorstatus, font=LARGE_FONT, bg="white")
ventstatus.place(relx=0.5, rely=0.15, anchor="center")
voltage_label = tk.Label(self, font=LARGE_FONT, bg="white")
voltage_label.place(relx=0.5, rely= 0.22, anchor="center")
update_label(voltage_label)
canvas= FigureCanvasTkAgg(f, self)
canvas.show()
canvas.get_tk_widget().pack(anchor="s", fill=tk.X, expand=True, pady=20)
app = Controller()
app.attributes('-fullscreen', True)
app.mainloop()
如果有人可以告诉我我做错了什么,那就太棒了.. 谢谢:))
答案 0 :(得分:0)
对于那些可能认为答案有用的人:
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
import tkinter as tk
from tkinter import ttk
from matplotlib import pyplot as plt
import time
import Adafruit_ADS1x15
adc = Adafruit_ADS1x15.ADS1115()
GAIN = 1
LARGE_FONT = ('Verdana', 18)
NORM_FONT = ('Verdana', 14)
SMALL_FONT = ('Verdana' , 11)
MENU_FONT= ('Arial', 12, "bold")
def update_label(label):
def getvoltage():
readings = [0]*100
maxsample = 100
counter = 0
while (counter < maxsample):
reading = adc.read_adc(0, gain=GAIN)
readings.append(reading)
counter += 1
avg = sum(readings)/100
voltage = (avg * 0.1259)/100
readings.clear()
label.config(text=str('Voltage: {0:.2f}'.format(voltage)))
label.after(500, getvoltage)
getvoltage()
class Controller(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Controller")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0,weight=1)
self.show_frame(DefaultScreen)
class DefaultScreen(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
self.configure(bg="white")
f = Figure(figsize = (10,3), dpi=100)
a = f.add_subplot(111)
a.plot([1,2,3,4,5,6,7,8],[2,4,6,8,10,12,14,16])
button1 = ttk.Button(self,text="Contact Page",
command=lambda:controller.show_frame(ContactPage))
button1.pack(anchor="se", pady=10, padx=10)
ventilatorstatus="Ventilator Status: Closed"
ventstatus= tk.Label(self, text=ventilatorstatus, font=LARGE_FONT, bg="white")
ventstatus.place(relx=0.5, rely=0.15, anchor="center")
voltage_label = tk.Label(self, font=LARGE_FONT, bg="white")
voltage_label.place(relx=0.5, rely= 0.22, anchor="center")
update_label(voltage_label)
canvas= FigureCanvasTkAgg(f, self)
canvas.show()
canvas.get_tk_widget().pack(anchor="s", fill=tk.X, expand=True, pady=20)
app = Controller()
app.attributes('-fullscreen', True)
app.mainloop()