1

I am doing a rather easy project that many have done before. Here is my hardware setup:

Raspberry Pi 3 w/16GB SD card

DHT11 Temperature Humidity sensor

Raspberry Pi 7" display

I am using Tkinter with Python 2.7 and Adafruit_DHT library.

The code is very basic

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

It works great except that it causes a memory leak. I am sure that it is forcing endless loops with the "win.after(1000, read_every_second) call however, I do not know how to fix it.

Any help would be appreciated.

  • 2
    You are creating three new labels for every read. Why not change the old ones? – Klaus D. Jun 14 '17 at 05:39
  • To change label's text, see: [Changing the text on a label](https://stackoverflow.com/questions/17125842/changing-the-text-on-a-label) or [How to change Tkinter label text on button press](https://stackoverflow.com/questions/29828477/how-to-change-tkinter-label-text-on-button-press) – Lafexlos Jun 14 '17 at 06:14
  • can you give an example? – Paul Thomas Jun 14 '17 at 06:15
  • Thanks for the info but, is that the source of the memory link? How? – Paul Thomas Jun 14 '17 at 06:19
  • Yes, creating any new widget consumes memory. What Klaus D and Lafexlos stated is that you should try changing label text instead of creating new widgets. After that, run your program again to verify that the rate of increased memory consumption decreases. – Ron Norris Jun 14 '17 at 14:18
  • OK... thanks for the explanation. I will work on that and will update this thread with the results – Paul Thomas Jun 14 '17 at 15:29

1 Answers1

0

Thanks to those that posted...

I have completely reworked my code and eliminated the leak.

I am not an accomplished programmer but, I am posting so others can get a idea of what I did (with others help) to make this project work.

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