Hi Guys,

I've been asked to create a GUI to output all the information gather by the program I currently have, however I'm new to using Tkinter and was wondering if someone could possible help...

#!/usr/bin/python

"""  header comment section     ******

file:  M:\testPCAN\miketst.py

  *****  END of header comment section    """

#-------------------------------------------------------------------
#     Libraries and Modules

from time            import clock, sleep
from os              import system
from collections     import defaultdict
from subprocess      import PIPE, Popen
from threading       import Thread, Lock

import Tkinter as tk


# -------------------------------------------------------------------
#     Global Definitions

mydict ={}   # define the dictionary
dict_lock = Lock()
guiMain = tk.Tk() #GUI Definition

# ***************************************************************
#
# ************************   PCAM Msg    ************************ 
#
# ***************************************************************
class PCANmsg(object):

  def __init__(self):
    self.dlc        = 0
    self.CANtime    = 0
    self.PCANperiod = 0
    self.COUNT      = 0
    self.hdata0     = 0
    self.hdata1     = 0
    self.hdata2     = 0
    self.hdata3     = 0
    self.hdata4     = 0
    self.hdata5     = 0
    self.hdata6     = 0
    self.hdata7     = 0
    self.timing     = 0


# ***************************************************************
#
# ***********************   SELECTBAUD   ************************ 
#
# ***************************************************************

def selectBaud():

  while True:
    baudRate = raw_input("Please Enter Baud Rate: ")
    if(baudRate == '1M'):
      system('echo "i 0x0014 e" >/dev/pcan33')
      print "PCAN SET TO 1M"
      break
    if(baudRate == '500K'):
      system('echo "i 0x001C e" >/dev/pcan33')
      print "PCAN SET TO 500K"
      break
    if(baudRate == '250K'):
      system('echo "i 0x011C e" >/dev/pcan33')
      print "PCAN SET TO 250K"
      break
    if(baudRate == '125K'):
      system('echo "i 0x031C e" >/dev/pcan33')
      print "PCAN SET TO 125K"
      break

  return



# ***************************************************************
#
# ***********************  RECIEVETTEST  ************************ 
#
# ***************************************************************
def beginRecvTest(locDicLckFLG):

    receiving = Popen("receivetest -f=/dev/pcan33".split(), stdout = PIPE)
    payload = iter(receiving.stdout.readline, "")
    #with open('testdata.txt') as File:
    #    lines = File.readlines()

    for line in payload:

      if(line[0].isdigit()):
          with locDicLckFLG:
            splitline = line.split()
            dictAdd(splitline, locDicLckFLG)

# ***************************************************************
#
# ************************    Dict Add   ************************ 
#
# ***************************************************************

def dictAdd(info, l):

    global mydict

    can_ID = info[4]

    p = PCANmsg()

    p.dlc        = int(info[5])
    p.CANtime    = float(info[0])
    p.hdata0     = info[6]  
    p.hdata1     = info[7]
    p.hdata2     = info[8]
    p.hdata3     = info[9]
    p.hdata4     = info[10]
    p.hdata5     = info[11]
    p.hdata6     = info[12]
    p.hdata7     = info[13]
    p.timing     = 1

    if can_ID in mydict.keys():
        q = mydict[can_ID]
        p.COUNT = q.COUNT + 1
        p.PCANperiod = p.CANtime - q.CANtime
    else:
        p.COUNT = 1
        p.PCANperiod = 0.0

    mydict[can_ID] = p

# ***************************************************************
#
# ************************   Print Msg   ************************ 
#
# ***************************************************************
def printMsg(_dict, _id):

  print('%06X: %3d   %02X %02X %02X %02X %02X %02X %02X %02X\t %8.2F %8d ' %
     (int(_id,16),
      _dict[_id].dlc,
      int(_dict[_id].hdata0, 16),
      int(_dict[_id].hdata1, 16),
      int(_dict[_id].hdata2, 16),
      int(_dict[_id].hdata3, 16),
      int(_dict[_id].hdata4, 16),
      int(_dict[_id].hdata5, 16),
      int(_dict[_id].hdata6, 16),
      int(_dict[_id].hdata7, 16),
      _dict[_id].PCANperiod,
      _dict[_id].COUNT)
      )

# ***************************************************************
#
# ************************ Print Function *********************** 
#
# ***************************************************************
def PRTdictLoop(locDicLckFLG):

  global mydict

  try:
    count = 0
    while True:
      system("clear")
      print ; print "-" *80  
      with locDicLckFLG:
        for _i in mydict.keys():
          printMsg(mydict, _i)
      print "Keys: ", ;  print mydict.keys()
      print ; print "-" *80  
      count += 1
      print count
      print len(mydict)
      sleep(0.1)
  except KeyboardInterrupt:
    pass


def Tranny():

      system('echo "m s 0xFF 8 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF" >/dev/pcan33')
      sleep(1)

# ***************************************************************
#
# ************************ Script Start  ************************ 
#
# ***************************************************************
if __name__ == "__main__":

    selectBaud()

    worker1 = Thread(target=beginRecvTest, args=(dict_lock,))
    worker2 = Thread(target=PRTdictLoop, args=(dict_lock,))

    recvButton = tk.Button(guiMain, text="Start Receive", command =worker1.start())
    worker2.start()

    recvButton.pack()
    guiMain.mainloop()

# --------------------------------------------------------------

I need to output the dictionary entries to the GUI in a way that when an update for that key is received it changes on the GUI. Any help/advise would be greatly appreciated

Tkinter variables are different (classes) from Python variables so you always have to convert one to the other. In Tkinter a StringVar shows any changes as they occur (to the StringVar). See the simple example at effbot http://effbot.org/tkinterbook/entry.htm showing set() --> Python to Tkinter, and get() --> Tkinter to Python

This article has been dead for over six months. Start a new discussion instead.