我正在做一个很容易的项目,许多人以前做过。这是我的硬件设置:
Raspberry Pi 3 w / 16GB SD卡
DHT11温湿度传感器
Raspberry Pi 7“显示器
我正在使用Tkinter和Python 2.7以及Adafruit_DHT库。
代码非常基本
from Tkinter import *
import tkFont
import Adafruit_DHT
import sys
import time
temp = 0
win = Tk()
win.title("Temperature")
sans = tkFont.Font(family='FreeSansBold', size=28, weight=tkFont.BOLD)
Label(win, text="Temperature", relief=SUNKEN, width=15, font=sans).grid(row=0, column=0)
Label(win, text="Humidity", relief=SUNKEN, width=15, font=sans).grid(row=1, column=0)
Label(win, text="Date/Time", relief=SUNKEN, width=15, font=sans).grid(row=2, column=0)
def READ():
global temp
humidity, temperature = Adafruit_DHT.read_retry(11, 4)
temp = temperature*9/5.0 + 32
Label(win, text=temp, relief=RIDGE, width=15, fg="black", bg="white", font=sans).grid(row=0, column=1)
Label(win, text=humidity, relief=RIDGE, width=15, fg="black", bg="white", font=sans).grid(row=1, column=1)
Label(win, text=time.strftime("%b %d %I:%M"), relief=RIDGE, width=15, fg="black", bg="white", font=sans).grid(row=2, column=1)
def read_every_second():
READ()
win.after(1000, read_every_second)
win.after(1000, read_every_second)
mainloop()
除了导致内存泄漏外,它的工作原理很好。我确信它正在用“win.after(1000,read_every_second)”调用强制无限循环但是,我不知道如何修复它。
任何帮助都将不胜感激。
答案 0 :(得分:0)
感谢发布的那些......
我完全重写了我的代码并消除了泄漏。
我不是一个有成就的程序员,但是,我发帖,所以其他人可以知道我做了什么(与其他人的帮助),使这个项目工作。
from Tkinter import *
import Adafruit_DHT
import time
import tkFont
from PIL import ImageTk, Image
master = Tk()
sans = tkFont.Font(family='FreeSansBold', size=52) #weight=tkFont.BOLD)
#sans = tkFont.Font(family='Roboto', size=52)
dkblue = '#0b1c3c'
master.geometry("700x400")
master.configure(background=dkblue)
temperature = 0
humidity = 0
original = Image.open("./Downloads/fish.png")
resized = original.resize((100, 100), Image.ANTIALIAS)
image = ImageTk.PhotoImage(resized)
m1 = Label(master, text = 'Temp ', bg = dkblue, fg = 'yellow', font = sans)#, relief = RIDGE, borderwidth = 5)
m1.grid(row = 0, column = 0, sticky = E)
w1 = Label(master, text= temperature, bg = dkblue, fg = 'white', font = sans)
w1.grid(row = 0, column = 1, sticky = W)
m2 = Label(master, text = 'Humid ', bg = dkblue, fg = 'yellow', font = sans)#, relief = RIDGE, borderwidth = 5)
m2.grid(row = 1, column = 0, sticky = E)
w2 = Label(master, text = humidity, bg = dkblue, fg = 'white', font = sans)
w2.grid(row = 1, column = 1, sticky = W)
t1 = Label(master, text = 'Date ', bg = dkblue, fg = 'yellow', font = sans)
t1.grid(row = 2, column = 0, sticky = E)
t2 = Label(master, text = time.strftime('%b %d'), bg = dkblue, fg = 'white', font = sans)
t2.grid(row = 2, column = 1, sticky = W)
t3 = Label(master, text = 'Time ', bg = dkblue, fg = 'yellow', font = sans)
t3.grid(row = 3, column = 0, sticky = E)
t3 = Label(master, text = time.strftime('%I:%M:%S'), bg = dkblue, fg = 'white', font = sans)
t3.grid(row = 3, column = 1, sticky = W)
imglabel = Label(master, image=image, bg = dkblue)
imglabel.grid(row = 0, column = 2, sticky = E)
def CHANGE():
global temperature
global humidity
humidity, temperature = Adafruit_DHT.read_retry(11, 4)
temperature = (temperature * 9/5.0 + 32)
w1.config(text = temperature)
w2.config(text = humidity)
t2.config(text = time.strftime('%b %d'))
t3.config(text = time.strftime('%I:%M:%S'))
master.after(2000, CHANGE)
master.after(2000, CHANGE)
mainloop()