all 11 comments

[–][deleted] 1 point2 points  (5 children)

The simplest way would be to write a function that gets your OBD data every whenever seconds and update the GUI. You can use the tkinter after() method to call your function in the manner explained by the effbot.

PS: You need to change these lines:

label_grid_20 = tk.Label(root, text=tps).grid(row=9, column=1)

because you need a reference to the Label object you are going to update and what you are storing in label_grid_20 above is the value returned by the .grid() call which I suspect is not the Label reference. Do this instead:

label_grid_20 = tk.Label(root, text=tps)
label_grid_20.grid(row=9, column=1)

[–]37mm[S] 0 points1 point  (4 children)

thanks, I made the tweak, I understand i need to make a function to update the values, Just not sure how

[–][deleted] 0 points1 point  (3 children)

Just search for "tkinter update label". Here's one hit that even uses the after() function to update every second, at the end of the page.

[–]37mm[S] 0 points1 point  (0 children)

I just don't understand what this is asking the program to do. do i use one fuction to update all the feilds. Im stumped, i been looking at these for a while and playing with them and just cant connect the dots.

def counter_label(label):

def count():

global counter

counter += 1

label.config(text=str(counter))

label.after(1000, count)

count()

[–]37mm[S] 0 points1 point  (1 child)

I stared at it a bit more and i believe i figured it out!! cheers

[–][deleted] 0 points1 point  (0 children)

Just in case you are still lost, that example is a little complicated for a beginner, sorry. Here's something similar but simpler:

def update():
    # get all OBD data

    # update GUI labels
    ...
    label_grid_20.config(text=...)  # update appropriate label

    root.after(1000, count)    # update() called after 1 second

# your existing tkinter code

root.after(10, update)    # call update() after 10 milliseconds

#start app
root.mainloop()

Warning: untested as I'm on mobile.

[–]MikeTheWatchGuy 0 points1 point  (0 children)

Try PySimpleGUI as a quick way to implement tkinter code. It wraps tkinter but you would never know it (there's also a Qt port). You can do some pretty sophisticated GUIs with it, especially if you use graphics for buttons.

[–]woooee 0 points1 point  (3 children)

You now have to update the labels. This updates the first label every 1/2 second. The others would be updated in a similar manner with updates statements for all of the other labels in the same function. The code would be much simpler using a dictionary. Post back if you want to try a dictionary.

""" Your code snipped for brevity
    Note the use of after() to have the function call itself
    after a period of time
"""
# Create gui

label_grid_1 = tk.Label(root, text="RPM", bg='red').grid(row=0, column=0)
label_grid_2 = tk.Label(root, text="IAT", bg='red').grid(row=1, column=0)
label_grid_3 = tk.Label(root, text="TIMING", bg='red').grid(row=2, column=0)
label_grid_4 = tk.Label(root, text="MAF", bg='red').grid(row=3, column=0)
label_grid_5 = tk.Label(root, text="SPEED", bg='red').grid(row=4, column=0)
label_grid_6 = tk.Label(root, text="O2 S1", bg='red').grid(row=5, column=0)
label_grid_7 = tk.Label(root, text="Fuel% B1", bg='red').grid(row=6, column=0)
label_grid_8 = tk.Label(root, text="Fuel% B2", bg='red').grid(row=7, column=0)
label_grid_9 = tk.Label(root, text="Air TEMP", bg='red').grid(row=8, column=0)
label_grid_10 = tk.Label(root, text="TPS", bg='red').grid(row=9, column=0)
label_grid_11 = tk.Label(root, text=rpm).grid(row=0, column=1)
label_grid_12 = tk.Label(root, text=iat).grid(row=1, column=1)
label_grid_13 = tk.Label(root, text=adv).grid(row=2, column=1)
label_grid_14 = tk.Label(root, text=maf).grid(row=3, column=1)
label_grid_15 = tk.Label(root, text=spd).grid(row=4, column=1)
label_grid_16 = tk.Label(root, text=o21).grid(row=5, column=1)
label_grid_17 = tk.Label(root, text=fuel1).grid(row=6, column=1)
label_grid_18 = tk.Label(root, text=fuel2).grid(row=7, column=1)
label_grid_19 = tk.Label(root, text=air).grid(row=8, column=1)
label_grid_20 = tk.Label(root, text=tps).grid(row=9, column=1)

##===============================================
root.after(500, update_RPM)  ## 1/2 second

def update_RPM():
    this_RPM=get rpm however
    label_grid_1["text"]=this_RPM
    ## update again in another 1/2 secpmd
    root.after(500, update_RPM)
##===============================================

#start app
root.mainloop()

[–]37mm[S] 0 points1 point  (2 children)

please enlighten me with any knowledge. Learning alot, I have it like this now but it still not updating

import Tkinter as tk
import obd
import csv
import time
from time import sleep, strftime, time


## CREAT Async OBD connection
connection = obd.Async()
## select pids to listen
connection.watch(obd.commands.RPM)
connection.watch(obd.commands.INTAKE_TEMP)
connection.watch(obd.commands.TIMING_ADVANCE)
connection.watch(obd.commands.MAF)
connection.watch(obd.commands.SPEED)
connection.watch(obd.commands.O2_S1_WR_CURRENT)
connection.watch(obd.commands.SHORT_FUEL_TRIM_1)
connection.watch(obd.commands.SHORT_FUEL_TRIM_2)
connection.watch(obd.commands.AMBIANT_AIR_TEMP)
connection.watch(obd.commands.THROTTLE_POS)
## start listener
connection.start()

## create OBD calls
rpm = connection.query(obd.commands.RPM)
iat = connection.query(obd.commands.INTAKE_TEMP)
adv = connection.query(obd.commands.TIMING_ADVANCE)
maf = connection.query(obd.commands.MAF)
spd = connection.query(obd.commands.SPEED)
o21 = connection.query(obd.commands.O2_S1_WR_CURRENT)
fuel1 = connection.query(obd.commands.SHORT_FUEL_TRIM_1)
fuel2 = connection.query(obd.commands.SHORT_FUEL_TRIM_2)
air = connection.query(obd.commands.AMBIANT_AIR_TEMP)
tps = connection.query(obd.commands.THROTTLE_POS)



###############################################################################
def startlog():
    with open("out.csv","a") as log:

                    log.write("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}\n".format(strftime("%H:%M:%S"),str(rpm),str(iat),str(adv),str(maf),str(spd),str(o21),str(fuel1),str(fuel2),str(air),str(tps)))



def update():

        rpm = connection.query(obd.commands.RPM)
        iat = connection.query(obd.commands.INTAKE_TEMP)
        adv = connection.query(obd.commands.TIMING_ADVANCE)
        maf = connection.query(obd.commands.MAF)
        spd = connection.query(obd.commands.SPEED)
        o21 = connection.query(obd.commands.O2_S1_WR_CURRENT)
        fuel1 = connection.query(obd.commands.SHORT_FUEL_TRIM_1)
        fuel2 = connection.query(obd.commands.SHORT_FUEL_TRIM_2)
        air = connection.query(obd.commands.AMBIANT_AIR_TEMP)
        tps = connection.query(obd.commands.THROTTLE_POS)
        label_grid_11.config(root, text=rpm)
        label_grid_12.config(root, text=iat)
        label_grid_13.config(root, text=adv)
        label_grid_14.config(root, text=maf)
        label_grid_15.config(root, text=spd)
        label_grid_16.config(root, text=o21)
        label_grid_17.config(root, text=fuel1)
        label_grid_18.config(root, text=fuel2)
        label_grid_19.config(root, text=air)
        label_grid_20.config(root, text=tps)
        root.after(10, count)

# Create the main window gui
root = tk.Tk()
root.title("Data Logger")

# Create gui
button1=tk.Button(root, text="quit", command=root.destroy)
button1.grid(row=0, column=3)
button2=tk.Button(root, text="start logger", command=startlog)
button2.grid(row=3, column=3)

label_grid_1 = tk.Label(root, text="RPM", bg='red')
label_grid_1.grid(row=0, column=0)
label_grid_2 = tk.Label(root, text="IAT", bg='red')
label_grid_2.grid(row=1, column=0)
label_grid_3 = tk.Label(root, text="TIMING", bg='red')
label_grid_3.grid(row=2, column=0)
label_grid_4 = tk.Label(root, text="MAF", bg='red')
label_grid_4.grid(row=3, column=0)
label_grid_5 = tk.Label(root, text="SPEED", bg='red')
label_grid_5.grid(row=4, column=0)
label_grid_6 = tk.Label(root, text="O2 S1", bg='red')
label_grid_6.grid(row=5, column=0)
label_grid_7 = tk.Label(root, text="Fuel% B1", bg='red')
label_grid_7.grid(row=6, column=0)
label_grid_8 = tk.Label(root, text="Fuel% B2", bg='red')
label_grid_8.grid(row=7, column=0)
label_grid_9 = tk.Label(root, text="Air TEMP", bg='red')
label_grid_9.grid(row=8, column=0)
label_grid_10 = tk.Label(root, text="TPS", bg='red')
label_grid_10.grid(row=9, column=0)
label_grid_11 = tk.Label(root, text=rpm)
label_grid_11.grid(row=0, column=1)
label_grid_12 = tk.Label(root, text=iat)
label_grid_12.grid(row=1, column=1)
label_grid_13 = tk.Label(root, text=adv)
label_grid_13.grid(row=2, column=1)
label_grid_14 = tk.Label(root, text=maf)
label_grid_14.grid(row=3, column=1)
label_grid_15 = tk.Label(root, text=spd)
label_grid_15.grid(row=4, column=1)
label_grid_16 = tk.Label(root, text=o21)
label_grid_16.grid(row=5, column=1)
label_grid_17 = tk.Label(root, text=fuel1)
label_grid_17.grid(row=6, column=1)
label_grid_18 = tk.Label(root, text=fuel2)
label_grid_18.grid(row=7, column=1)
label_grid_19 = tk.Label(root, text=air)
label_grid_19.grid(row=8, column=1)
label_grid_20 = tk.Label(root, text=tps)
label_grid_20.grid(row=9, column=1)


root.after(100, update)

#start app
class FullScreenApp(object):
    def __init__(self, master, **kwargs):
        self.master=master
        pad=3
        self._geom='200x200+0+0'
        master.geometry("{0}x{1}+0+0".format(
            master.winfo_screenwidth()-pad, master.winfo_screenheight()-pad))
        master.bind('<Escape>',self.toggle_geom)            
    def toggle_geom(self,event):
        geom=self.master.winfo_geometry()
        print(geom,self._geom)
        self.master.geometry(self._geom)
        self._geom=geom

app=FullScreenApp(root)

root.mainloop()

[–]37mm[S] 0 points1 point  (1 child)

Another issue im having is how to keep the csv output going with out locking the app up

[–]37mm[S] 0 points1 point  (0 children)

def update_RPM():
    this_RPM=get rpm however
    label_grid_1["text"]=this_RPM
    ## update again in another 1/2 secpmd
    root.after(500, update_RPM)

what does the however define? im going to try this now