-1

So I'm making a script for a raspberry pi 3-b. The idea is to take sensor readings from 4 sensors, and display each reading, updating over time, in each corner overtop of a background image. I am mostly unfamiliar with python, and new to tkinter.

The following is my current code.

from tkinter import *
from PIL import ImageTk
import Adafruit_DHT
import tkinter as tk
from tkinter import font as tkfont

window = Tk()

def main():
    
    c = Canvas(window,height=480,width=800)
    image = ImageTk.PhotoImage(file="/home/pi/Desktop/AppFiles/AppBackround.jpg")
    c.create_image(0,0,image=image,anchor=NW)
    text1 = tk.StringVar()
    text2 = tk.StringVar()
    text3 = tk.StringVar()
    text4 = tk.StringVar()
    myFont = tkfont.Font(family="Titillium Web", size=32)
    c.create_text(10,10,text=text1,fill="yellow",font=myFont)
    c.create_text(10,450,text=text2,fill="yellow",font=myFont)
    c.create_text(750,10,text=text3,fill="yellow",font=myFont)
    c.create_text(750,450,text=text4,fill="yellow",font=myFont)
    c.pack()

def update():
    sensor = Adafruit_DHT.DHT22
    DHT22_pin1 = 4
    DHT22_pin2 = 17
    DHT22_pin3 = 27
    DHT22_pin4 = 22
    humidity1, temperature1 = Adafruit_DHT.read_retry(sensor, DHT22_pin1)
    humidity2, temperature2 = Adafruit_DHT.read_retry(sensor, DHT22_pin2)
    humidity3, temperature3 = Adafruit_DHT.read_retry(sensor, DHT22_pin3)
    humidity4, temperature4 = Adafruit_DHT.read_retry(sensor, DHT22_pin4)
    text="Temperature1={0:0.1f}*C  Humidity1={1:0.1f}%".format(temperature1, humidity1)
    text="Temperature2={0:0.1f}*C  Humidity2={1:0.1f}%".format(temperature2, humidity2)
    text="Temperature3={0:0.1f}*C  Humidity3={1:0.1f}%".format(temperature3, humidity3)
    text="Temperature4={0:0.1f}*C  Humidity4={1:0.1f}%".format(temperature4, humidity4)
    window.after(100, update)

main()
update()
window.mainloop()

For some reason, my image is displaying as a blank gray background and my text is not displaying the readings or updating. instead I get this

enter image description here

What am I doing wrong, can anyone please explain what is going wrong here and how I might fix it?

  • Does this answer your question about the image? https://stackoverflow.com/questions/16424091 Also, you're displaying an object in the labels, rather than the text stored in the object. A search for `PY_VAR0` will result in more than 50 similar questions. – Bryan Oakley Apr 08 '21 at 00:51
  • thank you. that helped – Natan Andrews Apr 08 '21 at 02:29

1 Answers1

1

With advice from some of the comments, I produced this, It works well. Thanks guys

from tkinter import *
from PIL import ImageTk
import Adafruit_DHT
import tkinter as tk
from tkinter import font as tkfont

sensor = Adafruit_DHT.DHT22
DHT22_pin1 = 4
DHT22_pin2 = 17
DHT22_pin3 = 27
DHT22_pin4 = 22

window = Tk()


    
c = Canvas(window,height=480,width=800)
image = ImageTk.PhotoImage(file="/home/pi/Desktop/AppFiles/AppBackround.jpg")
c.create_image(0,0,image=image,anchor=NW)

myFont = tkfont.Font(family="Titillium Web", size=32)


textVar1 = tk.StringVar()
textVar2 = tk.StringVar()
textVar3 = tk.StringVar()
textVar4 = tk.StringVar()

humidity1, temperature1 = Adafruit_DHT.read_retry(sensor, DHT22_pin1)
humidity2, temperature2 = Adafruit_DHT.read_retry(sensor, DHT22_pin2)
humidity3, temperature3 = Adafruit_DHT.read_retry(sensor, DHT22_pin3)
humidity4, temperature4 = Adafruit_DHT.read_retry(sensor, DHT22_pin4)

textVar1.set("Temperature1={0:0.1f}*C  Humidity1={1:0.1f}%".format(temperature1, humidity1))
textVar2.set("Temperature2={0:0.1f}*C  Humidity2={1:0.1f}%".format(temperature2, humidity2))
textVar3.set("Temperature3={0:0.1f}*C  Humidity2={1:0.1f}%".format(temperature3, humidity3))
textVar4.set("Temperature4={0:0.1f}*C  Humidity2={1:0.1f}%".format(temperature4, humidity4))

text1 = c.create_text(10,10,text=textVar1.get(),fill="yellow",font=myFont)
text2 = c.create_text(10,450,text=textVar2.get(),fill="yellow",font=myFont)
text3 = c.create_text(750,10,text=textVar3.get(),fill="yellow",font=myFont)
text4 = c.create_text(750,450,text=textVar4.get(),fill="yellow",font=myFont)
c.pack()

def update():
    
    humidity1, temperature1 = Adafruit_DHT.read_retry(sensor, DHT22_pin1)
    humidity2, temperature2 = Adafruit_DHT.read_retry(sensor, DHT22_pin2)
    humidity3, temperature3 = Adafruit_DHT.read_retry(sensor, DHT22_pin3)
    humidity4, temperature4 = Adafruit_DHT.read_retry(sensor, DHT22_pin4)
    
    textVar1.set("Temperature1={0:0.1f}*C  Humidity1={1:0.1f}%".format(temperature1, humidity1))
    textVar2.set("Temperature2={0:0.1f}*C  Humidity2={1:0.1f}%".format(temperature2, humidity2))
    textVar3.set("Temperature3={0:0.1f}*C  Humidity2={1:0.1f}%".format(temperature3, humidity3))
    textVar4.set("Temperature4={0:0.1f}*C  Humidity2={1:0.1f}%".format(temperature4, humidity4))
    
    c.itemconfigure(text1, text=textVar1.get())
    c.itemconfigure(text2, text=textVar2.get())
    c.itemconfigure(text3, text=textVar3.get())
    c.itemconfigure(text4, text=textVar4.get())
    window.after(100, update)
update()
window.mainloop()