Skip to content Skip to sidebar Skip to footer

How To Display Some Measured Result In Python Tkinter Gui- New Values Every Seconds

I am trying to measure some value with sensors and display it using python tkinter GUI. I managed to create a GUI and the program to measure. Now I want to display the data in the

Solution 1:

You need to update the variables set as textvariables with the sensor values last read:

Something like this - the sensor readings were replaced with a randomly chosen value to simulate new data readings:

import tkinter as tk
import random


def readSensors():
    output_1.set(random.choice([0, 1, 2, 3, 4, 5]))
    output_2.set(random.choice([0, 1, 2, 3, 4, 5]))
    output_3.set(random.choice([0, 1, 2, 3, 4, 5]))
    output_4.set(random.choice([0, 1, 2, 3, 4, 5]))
    win.after(1000, readSensors)


win = tk.Tk()

win.geometry('800x480')
win.configure(background='#CD5C5C')

output_1 = tk.StringVar()
output_2 = tk.StringVar()
output_3 = tk.StringVar()
output_4 = tk.StringVar()

measuredValues = [0, 1, 2, 3, 4, 5]
value0 = str(measuredValues[0])
value1 = str(measuredValues[1])
value2 = str(measuredValues[2])
value3 = str(measuredValues[3])

output_1.set(value0)
output_2.set(value1)
output_3.set(value2)
output_4.set(value3)

output_1_label = tk.Label(win, textvariable=output_1, height=2, width=12)
output_1_label.place(x=200, y=100)

output_2_label = tk.Label(win, textvariable=output_2, height=2, width=12)
output_2_label.place(x=200, y=200)

output_3_label = tk.Label(win, textvariable=output_3, height=2, width=12)
output_3_label.place(x=200, y=300)

output_4_label = tk.Label(win, textvariable=output_4, height=2, width=12)
output_4_label.place(x=200, y=400)

win.after(1000, readSensors)
win.mainloop()

Solution 2:

You can use Classes to reach all the data from parent structure. You need to initilize a function to call another function with after tribute inside it first. It simply configures the data again and again after counting firstly 1000 ms and then 20 ms(adjust it to read data smoothly). You can add more dynamic data/text and use "after" tribute once, it will still be refreshing all of them. It is an event dependent solution which is significantly better than time dependent or loop based refreshing algorithms for data reading.

import time
from random import random
from random import seed
from tkinter import StringVar,Entry
import tkinter as tk

classGUI:

    #------------------INITIAL---------------------------------------------------------------------------------------------------------def__init__(self, parent):

        self.labelBackground = tk.Label(parent, text="",bg="white",width=1920,height=1080)
        self.labelBackground.place(x=0,y=0)
        self.labelDate = tk.Label(parent, text="Date",bg="white", font="Arial 20", width=100)
        self.labelDate.pack()
        self.labelDate.after(1000, self.refresh_label)

    defdateData(self):
        year,mon,day,hour,min,sec,a,b,c = time.localtime()
        infoDate = f"Date/Time: {year}{mon}{day} h:{hour} m:{min} s:{sec} "returnf"Clock: {hour}:{min}:{sec} "defrefresh_label(self):
        self.seconds = self.dateData()
        self.labelDate.configure(text=self.seconds)
        self.labelDate.after(20, self.refresh_label)



if __name__ == "__main__":
    root = tk.Tk()
    root.geometry("1920x1080") #Window Resolution
    root.title("Insert_Window_Title")
    timer = GUI(root)
    root.mainloop()

Post a Comment for "How To Display Some Measured Result In Python Tkinter Gui- New Values Every Seconds"