diff --git a/pydgets/widgets.py b/pydgets/widgets.py
index 5c47cebe7fd3287bbf722af583fb032994bf8c2c..a1fae846b0452361efe6e59dec65d5b3f57767fa 100644
--- a/pydgets/widgets.py
+++ b/pydgets/widgets.py
@@ -288,24 +288,39 @@ TREE_STYLES = {
 
 #-------------------------------------------------------------------------------
 
-def terminal_size():
+def is_terminal(fdesc):
     """
-    Detect the current size of terminal window as a numer of rows and columns.
+    Check, if we are connected to any terminal-like device.
     """
     try:
-        (rows, columns) = os.popen('stty size', 'r').read().split()
-        rows    = int(rows)
-        columns = int(columns)
-        return (columns, rows)
-
-    # Currently ignore any errors and return some reasonable default values.
-    # Errors may occur, when the library is used in non-terminal application
-    # like daemon.
+        if not fdesc:
+            return False
+        return os.isatty(fdesc.fileno())
     except:
-        pass
+        return False
+
+# Detect the terminal automatically after library initialization.
+IS_TERMINAL = is_terminal(sys.stdout)
+
+def terminal_size():
+    """
+    Detect the current size of terminal window as a numer of rows and columns.
+    """
+    if IS_TERMINAL:
+        try:
+            (rows, columns) = os.popen('stty size', 'r').read().split()
+            rows    = int(rows)
+            columns = int(columns)
+            return (columns, rows)
+
+        # Currently ignore any errors and return some reasonable default values.
+        # Errors may occur, when the library is used in non-terminal application
+        # like daemon.
+        except:
+            pass
     return (80, 24)
 
-# Detect the terminal size automatically after library initialization
+# Detect the terminal size automatically after library initialization.
 (TERMINAL_WIDTH, TERMINAL_HEIGHT) = terminal_size()
 
 #-------------------------------------------------------------------------------
diff --git a/setup.py b/setup.py
index d368ac58a1e062e82844344cf740e3cf05b7dc6d..dddf393a68c1c61b938cda171fe7f442022d17bb 100644
--- a/setup.py
+++ b/setup.py
@@ -23,7 +23,7 @@ with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
 
 setup(
     name = 'pydgets',
-    version = '0.6',
+    version = '0.9',
     description = 'Console widget library for Python 3',
     long_description = long_description,
     classifiers = [