Python Tkinter, Update every second

Question:

So I am creating a Python Tkinter application, which has a measured value I need displayed to the screen every second. When the program runs, the value show, but it isn’t updating (I am externally manipulating the value so I know the display should be changing). How do I get the display to dynamically update every second? I was made to understand that I didn’t need to create an Update() method or anything for Tkinter applications because mainloop() takes care of that. Here is my code:

Main.py:

from SimpleInterface import SimpleInterface
from ADC import Converter, Differential

adc = Converter(channelNums = [2])

root = SimpleInterface(adc)
root.title = ("Test Interface")

root.mainloop()

SimpleInterface.py:

import tkinter as tk
from tkinter import ttk

class SimpleInterface(tk.Tk):

    def __init__(self, ADC, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self, *args, **kwargs)
        container.grid(column = 0, row = 0, sticky = "nwes")
        container.grid_rowconfigure(0, weight = 1)
        container.grid_columnconfigure(0, weight = 1)

        frame = Screen(ADC, container, self)
        frame.grid(row = 0, column = 0, sticky = "nsew")
        frame.tkraise()

class Screen(tk.Frame):

    def __init__(self, ADC, parent, controller):
        tk.Frame.__init__(self, parent)

        displayText = ADC.ReadValues() #this method returns a list of values
        for i in range(len(displayText)):
            displayText[i] = round(displayText[i], 2)
        ttk.Label(self, text = "Test Display", background = "grey").grid(column = 7, row = 8)
        lblTestDisplay = ttk.Label(self, text = displayText, foreground = "lime", background = "black").grid(column = 7, row = 9, sticky = "ew")

So the display properly shows the displayText when the code is initially run, but again nothing changes as I manually manipulate the value being input. Do I need to create an Update() method? If so where would I call said method?

Asked By: Skitzafreak

||

Answers:

Yes, you will need to create a method to update the values, and add that method to the tkinter mainloop with after. I’d recommend naming it something else, because update is already a tkinter method.

As a totally untested guess:

class Screen(tk.Frame):
    def __init__(self, ADC, parent, controller):
        tk.Frame.__init__(self, parent)
        self.ADC = ADC
        lbl = ttk.Label(self, text = "Test Display", background = "grey")
        lbl.grid(column = 7, row = 8)
        self.lblTestDisplay = ttk.Label(self, foreground = "lime", background = "black")
        self.lblTestDisplay.grid(column = 7, row = 9, sticky = "ew")

        self.adc_update() # start the adc loop

    def adc_update(self):
        displayText = self.ADC.ReadValues() #this method returns a list of values
        for i in range(len(displayText)):
            displayText[i] = round(displayText[i], 2)
        self.lblTestDisplay.config(text = str(displayText)) # update the display
        self.after(1000, self.adc_update) # ask the mainloop to call this method again in 1,000 milliseconds

Note I also split your label creation into 2 lines, one to initialize and one to layout. The way you had it is very bad; it leads to the variable assigned to None which leads to bugs.

Answered By: Novel

#é possível utilizar um meio para executar o código que atualizar em um while loop

detalhe, a tela só atualiza quando o tempo de delay acaba

from tkinter import *
import time

class App():
def init(self):
self.window = Tk()
self.window.protocol(‘WM_DELETE_WINDOW’, self.done)
self.window.geometry(‘500×500′)
self.outlabel = Label(self.window, text=’0’)
self.outlabel.grid(column=0, row=0) # this label will be used soon
self.controller = True
self.conter = 0
while self.controller:
self.update()
def update(self):
# the code you want to use
self.conter += 1
self.outlabel.config(text=str(self.conter))
self.window.update()
time.sleep(1)
def done(self):
self.controller = False
self.window.destroy()

App()

Answered By: Elias Gabriel
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.