I have a python script which I have written for a Raspberry Pi project, the script reads a value from a micrometer every second, and stores this value as a TkInter StringVar (http://effbot.org/tkinterbook/variable.htm)
What I want to happen is that that string is displayed on the UI as a label, and updated on the UI when the value changes.
I chose to use a Tkinter stringvar instead of a standard string because I thought that Tkinter would then automatically update on the UI, but it doesn't seem to work. Currently the script displays nothing for the label, and does not update when show sets virtual_reading.
From other threads I have read, the Tkinter method update_idletasks() should force the window to update, but I haven't been able to get this to work.
Can anyone give any guidance on where I'm going wrong? This is my first go at Python so sorry if this is simple.
import datetime
import csv
from tkinter import *
from tkinter import messagebox
import time
import pigpio
import osos.system("sudo pigpiod")root = Tk()winx = 480
winy = 320 CLOCK=21
DATA=20g_level=0
g_reading=0
g_bits=0pi = pigpio.pi()virtual_reading = StringVar()def go():global cb1global cb2cb1 = pi.callback(DATA, pigpio.EITHER_EDGE, cbf)cb2 = pi.callback(CLOCK, pigpio.EITHER_EDGE, cbf)root.after(1000 ,go)def show(bits, value):inch = value & (1<<23)minus = value & (1<<20)value = value & 0xfffffif inch:reading = value / 2000.0units = "in"else:reading = value / 100.0units = "mm"if minus:sign = "-"else:sign = ""global virtual_readingvirtual_reading = StringVar()virtual_reading.set("{} {:.3f} {}".format(sign, reading, units))print(virtual_reading.get())cb2.cancel()cb1.cancel()def measure(event=None):todays_date = datetime.date.today() try:get_tool_no = int(tool_no_entry.get())if get_tool_no <= 0:messagebox.showerror("Try Again","Please Enter A Number")else:with open("thickness records.csv", "a") as thicknessdb: thicknessdbWriter = csv.writer(thicknessdb, dialect='excel', lineterminator='\r')thicknessdbWriter.writerow([get_tool_no] + [todays_date] + [virtual_reading.get()])thicknessdb.close()except:messagebox.showerror("Try Again","Please Enter A Number")tool_no_entry.delete(0, END)def cbf(g, l, t):global g_level, g_reading, g_bitsif g == DATA:if l == 0:g_level = 1else:g_level = 0elif g == CLOCK:if l == pigpio.TIMEOUT:if g_bits > 10:show(g_bits, g_reading)g_reading=0g_bits=0elif l == 0:g_reading = g_reading | (g_level<<g_bits)g_bits += 1go()record_button = Button(root,width = 30,height = 8,text='Measure',fg='black',bg="light grey", command = measure)tool_no_entry = Entry(root)reading_display = Label(root, font=("Helvetica", 22), text = virtual_reading.get())reading_display.place(x = 50, y =80)root.resizable(width=FALSE, height=FALSE)
root.geometry('%dx%d' % (winx,winy))
root.title("Micrometer Reader V1.0")record_button.place(x = 340, y = 100, anchor = CENTER)tool_no_entry.place(x = 120, y = 250, anchor=CENTER)
tool_no_entry.focus_set()root.bind("<Return>", measure)root.mainloop()cb2.cancel()
cb1.cancel()
pi.stop()