How to Show With Tkinter The Result of Subprocess.call?

0

Good morning I would like to know how to use Tkinter , I can show on the screen the result of the module subprocess.call(["free","-m"]) .

Thank you very much for your attention.

    
asked by Freder Hernandez 09.04.2017 в 22:19
source

1 answer

0

The simplest way is to redirect standard output using pipelines using subprocess.PIPE . The problem is that it is not safe to do this with the call method. Instead we can use the class Popen and its method comunicate . A very simple example using a Text widget to display the information would be:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import subprocess as sub
import Tkinter as tk

p = sub.Popen(["free","-m"],stdout=sub.PIPE,stderr=sub.PIPE)
output, errors = p.communicate()

root = tk.Tk()
text = tk.Text(root)
text.pack()
text.insert(tk.END, output)
root.mainloop()

Getting:

You can pause the information as you want and use other widgets to display it.

If you need to use this idea to make calls to processes that are slow to respond or need to update in real time, you will need other approaches such as redirecting the stdout to a queue to prevent the GUI from blocking waiting for the thread.

P.D: You do not say what version of Python you use, the code is for Python 2.

    
answered by 10.04.2017 / 00:24
source