Python Forum
[Tkinter] Show temperature in gui
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Show temperature in gui
#1
Hi, im a total newbie to this but here we go:
Got 6x MAX31855 running with code from https://github.com/Tuckie/max31855
I have managed to make a noob gui and "logging" directly to USB as a text file.
Now I also want to see the current temperatures displayed on the gui...
I've tried with some labels etc but cant get it working.
When looking at my program you see that i dont have a clue :)

#!/usr/bin/python
from tkinter import *
import tkinter as tk
from max31855 import MAX31855, MAX31855Error
running = False
import time
import readline
       

def scanning ():
        f = open('/media/pi/KINGSTON/LOGG', 'a')
        
      
        if running:
              
                cs_pins = [25, 8, 7, 5, 6, 12]
                clock_pin = 11
                data_pin = 9
                units = "c"
                thermocouples = []
                for cs_pin in cs_pins:
                        thermocouples.append(MAX31855(cs_pin, clock_pin, data_pin, units))
                meh = True
                if (meh):
                        try:
                            for thermocouple in thermocouples:
                                rj = thermocouple.get_rj()
                                try:
                                    tc = thermocouple.get()
                                except MAX31855Error as e:
                                    tc = "Error: "+ e.value
                                    meh = False
                                
                                print ("Sensor: {} *C".format(tc))      
                                print (f.write("Sensor: {} *C".format(tc))), (f.write("  kl.  ")), (f.write(time.strftime("%H:%M:%S\n")))
                            time.sleep(1)
                        except (Stopped):
                            meh = False
                for thermocouple in thermocouples:
                    thermocouple.cleanup()    
      
        root.after(1000, scanning)


def start():
        f = open('/media/pi/KINGSTON/LOGG', 'a')
        #enable logging#
        global running
        running = True
        print (f.write("-------------------------------\n"))
        print (f.write("Logging har startet\n"))
        print (f.write("\n"))
        print (f.write(time.strftime("%H:%M:%S\n")))
        print (f.write(time.strftime("%d/%m/%Y\n")))
        print (f.write("-------------------------------\n"))
        start['state'] = DISABLED
        stop['state'] = NORMAL
        
        
def stop():
        f = open('/media/pi/KINGSTON/LOGG', 'a')
        #disable logging#
        global running
        running = False
        print (f.write("-------------------------------\n"))
        print (f.write("Logging avsluttet\n"))
        print (f.write("\n"))
        print (f.write(time.strftime("%H:%M:%S\n")))
        print (f.write(time.strftime("%d/%m/%Y\n")))
        start['state'] = NORMAL
        stop['state'] = DISABLED
       




root = tk.Tk()
root.title("Temperaturlogging")
root.geometry("500x500")







app = Frame(root)
app.grid()

start = Button(app, text="Start Logging", width=40, height=8, background="green", command=start)
stop = Button(app, text="Stopp Logging", width=40, height=8, background="yellow", command=stop)



start.grid(row=1, column=3)
stop.grid(row=2, column=3)



stop['state'] = DISABLED

    


root.after(1000, scanning)
root.mainloop()
Reply
#2
Try this code demonstrating how to update a label in tkinter (found here, credits to woooee)
import tkinter as tk

class UpdateLabel():
    def __init__(self):
        self.master = tk.Tk()
        self.label1_text = tk.StringVar()
        self.label1_text.set("initial value")
        self.label1=tk.Label(self.master, textvariable=self.label1_text,
                            fg='blue', font=("Arial", 36, "bold"),
                            background='#CDC5D9')
        self.label1.grid(row=0,column=0)

        self.master.grid_columnconfigure(1, minsize=100)

        self.label2=tk.Label(self.master,text='Initial value',
                            fg='blue', font=("Arial", 36, "bold"),
                            background='#CDC5D9')
        self.label2.grid(row=0,column=2)

        tk.Button(self.master, text="Quit", command=self.master.quit,
                  bg="red").grid(row=1, column=0)

        ## update the label in two different ways
        self.master.after(2000, self.update_label_1) ## sleep for 2 seconds

        self.master.mainloop()

    def update_label_1(self):
        ## update label1 by changing the StringVar()
        self.label1_text.set("2nd value")
        self.label1.config(bg="yellow")i

        ## update label2 by setting the text
        self.label2["text"] = "third value"
        self.label2["fg"] = "green"
        ## this is the same as the above line
        ##self.label2.config(text="third value")

UpdateLabel()
Reply
#3
Thank you , I understand how that works out now!
But I can't seem to get it working with the code I already made.
And also, tk.StringVar seems to only handle 1 variable at a time?
Can it handle all different inputs in the same string and also showing all six on the same label?
Sorry if my questions are completely weird
Reply
#4
(Jan-17-2018, 11:33 AM)katastroooof Wrote: Can it handle all different inputs in the same string and also showing all six on the same label?
I don't understand the part about different inputs in the same string, but if you want to show six values on the same label, you only need to build a string with the six values and set the StringVar variable with this string.
Reply
#5
self.label1["text"] = (" {} ".format(tc)) #readings from MAX31855, prints 6 lines but only the last line shows in label.
Thats my current issue :)
Reply
#6
You can try to replace newlines with space characters
= " {} ".format(tc.replace('\n', ' '))
If you want more information, print repr(tc) and post the result here.
Reply
#7
Thanks for helping out!
So, when :
self.label1["text"] = " {} ".format(tc.replace('\n', ' '))
I get the following result :
self.label1["text"] = " {} ".format(tc.replace('\n', ' '))
AttributeError: 'float' object has no attribute 'replace'

With:
print(repr(tc))
22.25
22.25
21.75
22.75
22.5
22.25
Reply
#8
It's impossible. The repr of a float is not 6 lines long. Or perhaps you are printing 6 times the repr of a float because of the loop on the theromcouples. Then you need accumulate values somewhere and display it at the end of the loop
                            acc = []
                            for thermocouple in thermocouples:
                                rj = thermocouple.get_rj()
                                try:
                                    tc = thermocouple.get()
                                except MAX31855Error as e:
                                    tc = "Error: "+ e.value
                                    meh = False
                                acc.append(tc)
                            self.label1['text'] = ' {} '.format(' '.join(str(x) for x in acc))
Reply
#9
Not sure what you just put in the code but here's the result:

File "/home/pi/Label2.py", line 101, in updater
acc += tc
TypeError: 'float' object is not iterable
Reply
#10
Yes I wrote += instead of using the append() method. See the updated code above.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding text after temperature reading Tkinter list249 2 4,291 Jun-21-2017, 01:47 PM
Last Post: Barrowman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020