somewhereinusa
Posts: 10
Joined: Thu Dec 10, 2015 1:59 pm
Location: Andrews,IN
Contact: Website

frustrated

Thu Dec 31, 2015 9:12 pm

I'm getting frustrated. I have very little coding knowledge, spent all of my life until now (retired) building,repairing and driving things. I'm working on a project to display vehicle temps and pressures (no OBD) on a screen. I have no problem building and testing various sensors and getting digital data. I don't want to log or keep track of anything I just want the current results to show on a screen and keep updating as conditions change. I have spent days combing the web and have found projects that read OBD, log data, put beautiful dashboards on the web, I want to use wires.
I have found many, many tutorial sites with code that doesn't work, ie: wrong syntax, missing parentheses, etc. I try them all in Python 2 and 3 just to see if that is the problem.

So far all I have managed to do is get a very crude GUI that has labels and a 24 hour clock (can't seem to get it to show 12 hour clock) I have found nothing that really has any info on how to take the results I get from a sensor and have it show on a GUI using tkinter.

Can someone point me in the right general direction?

Thanks,
Dick

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: frustrated

Thu Dec 31, 2015 10:57 pm

Happy New Year

Code: Select all

#!/usr/bin/env python
import Tkinter as tk
import thread
import random
import time

data = {}
run = True

def thread_func(a,b):
	""" This is the function that reads in all the data.
		All it does right now is to set them all to random numbers
		twice a second.
	"""
	while run:
		data["speed"].set(random.uniform(1000, 3000))
		data["throttle"].set(random.uniform(0,100))
		time.sleep(0.5)

class Application(tk.Frame):
	def __init__(self, master=None):
		tk.Frame.__init__(self, master)
		self.control = {}
		self.grid()
		self.createWidgets()

	def createWidgets(self):
		# self.control is a dictionary of tuples.
		# The key is the name of the data.
		# The tuple is a pair of controls. The first is the label and the
		#	second is the value.
		# n is the current row number, incremented each time and then used
		# for the quit button.
		n = 0
		for datum in data:
			self.control[datum] = (tk.Label(self, text=datum), tk.Label(self, textvar=data[datum]))
			self.control[datum][0].grid(column=0, row=n)
			self.control[datum][1].grid(column=1, row=n)
			n = n + 1

		self.quitButton = tk.Button(self, text='Quit', command=self.quit)
		self.quitButton.grid(column=0, rowspan=2, row=n)
		
# Need to do this before we create any control variables.
# Normally this is done by tk.Frame, ie Application
tk.Tk()

# Set each datum up here. You can add as many as you want,
# screen-space not withstanding. 
data["speed"] = tk.StringVar()
data["throttle"] = tk.StringVar()

# We're using this for demo purposes.
random.seed()

# Start the thread that will update the values of all the data.
thread.start_new_thread(thread_func, (1,2))

# Standard tkinter stuff
app = Application()
app.master.title('Sample application')
app.mainloop()

# Tell the thread to stop and wait for it to do so.
run = False
time.sleep(1)

somewhereinusa
Posts: 10
Joined: Thu Dec 10, 2015 1:59 pm
Location: Andrews,IN
Contact: Website

Re: frustrated

Fri Jan 01, 2016 9:09 pm

Thanks, I'll see what I can do with that.

Return to “Beginners”