Python Forum
Update a label text from subprocess - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Update a label text from subprocess (/thread-20551.html)



Update a label text from subprocess - jim53 - Aug-18-2019

Hello,

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.parent=parent
        self.but=Button(text="Start", command=self.action)
        self.but.pack()
        self.lab=Label(parent, text='Text to display')
        self.lab.pack()
        self.lab.configure(width=50,height=100)
        self.result=''
    
    def action(self):
        self.result=''
        self.run('esptool.py --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 = reader.read(1)
            self.result=self.result+char
            print(self.result)
            self.lab.configure(text=self.result)

if __name__ == "__main__":
    Test=App()
I think I need to use Thread but even if I have read a lot of article. I am block.
Could someone help me.


RE: Update a label text from subprocess - Axel_Erfurt - Aug-18-2019

is esptool.py executable?

try python esptool.py

or import esptool

Using esptool.py from Python


RE: Update a label text from subprocess - jim53 - Aug-18-2019

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
self.update()
after
self.lab.configure(text=self.result)
and now the text of the label is dynamicaly updated.


RE: Update a label text from subprocess - Denni - Aug-19-2019

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 ( https://docs.python.org/2/library/subprocess.html ) 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