Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Update a label text from subprocess

I try to code a graphical interface to run terminal command to show the result in a label.
My problem is that the text of the label is only update when at the end of the subprocess.popen while I would like to see that the realtime updating of the label text accorindly to the stdout.
Please find below my code :
from tkinter import *
from subprocess import Popen, PIPE
import io

class App(Frame):
    def __init__(self, parent=None):
        Frame.__init__(self, parent)
        self.but=Button(text="Start", command=self.action)
        self.lab=Label(parent, text='Text to display')
    def action(self):
        self.result=''' --port /dev/ttyUSB0 flash_id')

    def run(self,command):
        process = Popen(command, stdout=PIPE, shell=True)
        reader = io.TextIOWrapper(process.stdout, encoding='utf8')
        while process.poll()==None:
            char =

if __name__ == "__main__":
I think I need to use Thread but even if I have read a lot of article. I am block.
Could someone help me.
is executable?

try python

or import esptool

Using from Python
Thank you for the reply but I not problem to execute esptool with program.
The problem that I have is that the output is redirected to a PIPE but the text of label is only updated when the subprocess.popen is completed. I would like to update the text of the label each time a new character arrived in stdout to have a dynamic update.
I hope you understand my problem. It is "just" a display problem.

I have found a solution.
I have just add
and now the text of the label is dynamicaly updated.
Okay but you are executing a linear thread which means nothing after this

process = Popen(command, stdout=PIPE, shell=True)
gets executed until it completes as per the documentation on subprocess:
  • Run the command described by args. Wait for command to complete, then return the returncode attribute.
found here ( ) and I highly doubt that Wait is a Sleeping Wait which means it blocks all other execution until it is done
So in order for you to have dual processing as you seem to be wanting you will have to fire off a second process using Multiprocessing because a Thread, due to GIL, does basically the same thing as Subprocess -- although there are tricks to get a Thread to allow the OS to do something else prior to completing and perhaps the Subprocess can as well but I without looking more closely into this I cannot say for sure. Note I did look into this previously (a while back) for what I am doing and ended up using a combination of Threads and Multiprocessing as if I recall correctly the Subprocess basically works sort of like a Thread due to python's GIL restrictions but did not have the flexibility that a Thread provided me

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 119 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  [Tkinter] Label, align imported text from pandas kundrius 2 327 Dec-11-2019, 08:26 AM
Last Post: kundrius
  Make Label Text background (default color) transparent using tkinter in python barry76 1 3,428 Nov-28-2019, 10:19 AM
Last Post: Larz60+
  Unable to update or refresh label text in tkinter jenkins43 3 1,548 Jul-24-2019, 02:09 PM
Last Post: Friend
  Update value selected in option menu when select an item from text box klllmmm 2 1,144 Jun-06-2019, 04:51 AM
Last Post: klllmmm
  [Tkinter] Having trouble updating the text in a label microphone_head 3 634 May-02-2019, 08:44 AM
Last Post: microphone_head
  Display and update the label text which display the serial value jenkins43 5 2,292 Feb-04-2019, 04:36 AM
Last Post: Larz60+
  [Tkinter] update the content of Text widget atlass218 10 6,724 Dec-15-2018, 11:51 AM
Last Post: atlass218
  [Tkinter] can't update label to include variable foxtreat 2 895 Dec-01-2018, 07:16 AM
Last Post: jfong
  [WxPython] bind label and entry text with return key metulburr 1 973 Aug-14-2018, 10:02 PM
Last Post: metulburr

Forum Jump:

Users browsing this thread: 1 Guest(s)