Search code examples
tkinterrefresh

Unable to update value in Tkinter


I have been working on my final year project for a while now but have become a little stuck for the past week now

I am designing a Temperature controller and so far have the temperature displayed on the main screen with a few other elements to it

But the problem I have is the temperature only stays at the value it was when the program is first run (it doesn't change value)

Below is part of the code I'm struggling with:

from gpiozero import MCP3008

adc = MCP3008(channel =0, device =0)

voltage = 3.3 * adc.value

ActualTemp = voltage * 100

ACTEMP ='{:.2f}'.format(ActualTemp)


ttk.Label(mainframe, text= ACTEMP).grid(column=2, row=2, sticky=(W, E))

I have tried searching and guessing for days but unable to find anything

Any help would be gratefully appreciated

Thanks

Here is the complete code:

from tkinter import *
from tkinter import ttk
import RPi.GPIO as GPIO
from gpiozero import MCP3008

adc = MCP3008(channel =0, device =0)
voltage = 3.3 * adc.value
ActualTemp = voltage * 100
ACTEMP ='{:.2f}'.format(ActualTemp)

FanSpeedDigital = MCP3008(channel =1, device =0)
FanSpeedAnalogue1 = 3.3 * 30.3030303 * FanSpeedDigital.value
FanSpeedAnalogue ='{:.2f}'.format(FanSpeedAnalogue1)


root = Tk()
mainframe = ttk.Frame(root, padding="3 6")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))

mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

root.title("Circulation Water Temperature Regulator")


ttk.Label(mainframe, text="Circulation Water Temperature").grid(column=1, row=1, sticky=(W, E))


ttk.Label(mainframe, text="Actual Temp").grid(column=1, row=2, sticky=(W, E))

temp_label = ttk.Label(mainframe);
temp_label.grid(column=2, row=2, sticky=(W, E));
temp_label.configure(text = ACTEMP)


ttk.Label(mainframe, text="°C ").grid(column=3, row=2, sticky=W)


AutoContol = Button(mainframe, text="Auto Control").grid(column=4, row=2, sticky=(W, E))


ttk.Label(mainframe, text="Set Temperature").grid(column=1, row=3, sticky=(W, E))


SetTemp = StringVar()
SetTemp_entry = ttk.Entry(mainframe, width=17,textvariable=SetTemp)
SetTemp_entry.grid(column=2, row=3, sticky=(W, E))


ttk.Label(mainframe, text="°C ").grid(column=3, row=3, sticky=W)


ManFullSpeed = Button(mainframe, text="Manual Full Speed").grid(column=4, row=3, sticky=(W, E))
Entr = Button(mainframe, text="Enter").grid(column=2, row=4, sticky=(W, E))


ttk.Label(mainframe, text='Fan Speed').grid(column=1, row=6, sticky=(W, E))


FanSpeed = ttk.Label(mainframe, width=17,text = FanSpeedAnalogue)
FanSpeed.grid(column=2, row=6, sticky=(W, E))


ttk.Label(mainframe, text="%").grid(column=3, row=6, sticky=W)


for child in mainframe.winfo_children(): child.grid_configure(padx=15, pady=15)


root.update_idletasks

You may not be able to run this code, as I have an analogue to digital converter attached to my Raspberry Pi, which is where im getting my Temperature reading from

Thanks


Solution

  • Try putting that in your code just before calling root.mainloop() :

    def get_temperature():
        """returns the current temperature"""
    
        voltage = 3.3 * adc.value
        ActualTemp = voltage * 100
        ACTEMP ='{:.2f}'.format(ActualTemp)
        return ACTEMP
    
    
    def update_temperature():
        """updates the text"""
    
        temp_label.configure(text = get_temperature())
        root.after(100, update_temperature)
    
    root.after(100, update_temperature)
    

    And this before your mainloop :

    root.after(100, update_temperature)

    The after method will call your function every 100 ms, you can change this value according to your needs.