2017-06-14 63 views
0

我正在做一個相當簡單的項目,許多人以前做過。這是我的硬件設置:Tkinter內存泄漏

樹莓裨3瓦特/ 16GB SD卡

DHT11溫度溼度傳感器

樹莓裨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),但是打電話,我不知道如何解決它。

任何幫助,將不勝感激。

+2

您正在爲每次閱讀創建三個新標籤。爲什麼不改變舊的? –

+0

要更改標籤的文本,請參閱:[更改標籤上的文本](https://stackoverflow.com/questions/17125842/changing-the-text-on-a-label)或[如何更改Tkinter標籤文本按下按鈕](https://stackoverflow.com/questions/29828477/how-to-change-tkinter-label-text-on-button-press) – Lafexlos

+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()