Python Forum
Update a label text from subprocess
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.
Reply
#2
is esptool.py executable?

try python esptool.py

or import esptool

Using esptool.py from Python
Reply
#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.
Reply
#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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Update label if there are no records in treeview TomasSanchexx 1 907 Aug-20-2023, 04:45 PM
Last Post: menator01
  [Tkinter] Can't update label in new tk window, object has no attribute tompranks 3 3,468 Aug-30-2022, 08:44 AM
Last Post: tompranks
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,674 Jun-26-2022, 06:26 PM
Last Post: menator01
  [Tkinter] Make my button text update? Skata100 1 2,014 Aug-07-2021, 05:37 AM
Last Post: deanhystad
  update text variable on label with keypress knoxvilles_joker 3 4,842 Apr-17-2021, 11:21 PM
Last Post: knoxvilles_joker
  How to read text in kivy textinput or Label jadel440 1 5,184 Dec-29-2020, 10:47 AM
Last Post: joe_momma
  [Kivy] Kivy text label won't shows up! AVD_01 1 2,895 Jun-21-2020, 04:01 PM
Last Post: AVD_01
  [Tkinter] Python 3 change label text gw1500se 6 4,608 May-08-2020, 05:47 PM
Last Post: deanhystad
  [Tkinter] how to update label text from list Roshan 8 5,371 Apr-25-2020, 08:04 AM
Last Post: Roshan
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 13,670 Mar-20-2020, 07:21 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020