Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Update a label text from subprocess
#1
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.
Quote
#2
is esptool.py executable?

try python esptool.py

or import esptool

Using esptool.py from Python
Quote
#3
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.
Quote
#4
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
silly American kah-nig-hit
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Unable to update or refresh label text in tkinter jenkins43 3 292 Jul-24-2019, 02:09 PM
Last Post: Friend
  Update value selected in option menu when select an item from text box klllmmm 2 464 Jun-06-2019, 04:51 AM
Last Post: klllmmm
  [Tkinter] Having trouble updating the text in a label microphone_head 3 399 May-02-2019, 08:44 AM
Last Post: microphone_head
  Display and update the label text which display the serial value jenkins43 5 1,006 Feb-04-2019, 04:36 AM
Last Post: Larz60+
  [Tkinter] update the content of Text widget atlass218 10 4,866 Dec-15-2018, 11:51 AM
Last Post: atlass218
  [Tkinter] can't update label to include variable foxtreat 2 628 Dec-01-2018, 07:16 AM
Last Post: jfong
  [WxPython] bind label and entry text with return key metulburr 1 701 Aug-14-2018, 10:02 PM
Last Post: metulburr
  How to change the text in label? ;/ Dzonzi 3 7,963 Jul-18-2018, 07:26 PM
Last Post: nilamo
  [PyQt] Assign Label Text as number in Hz mekha 17 2,343 Jul-18-2018, 12:47 AM
Last Post: mekha
  Update Gtk.Label in for-loop TimeMen 3 1,333 Jun-07-2018, 10:33 PM
Last Post: killerrex

Forum Jump:


Users browsing this thread: 1 Guest(s)