3

I'm attempted to resize the terminal window on launch of a python script to ensure the display will be static size. It's working but not quite what I expected. I've tried a few methods:

import sys
sys.stdout.write("\x1b[8;40;120t")

and

import subprocess
subprocess.call(["echo","-e","\x1b[8;40;120t"])

and even just

print "\x1b[8;40;80t"

They all work and resize the real terminal. However, if my terminal is, let's say 25x80 to start, the script starts, it resizes, then exits. It will not execute the rest of the script. I wrapped it in a try/Except and nothing is thrown. Nothing in the syslogs and python -v script.py shows nothing odd. If I execute the script again or at a term size of 40x120 (my target size)..the script runs just fine. Why is exeecuting the ANSI escape exiting python? Furthermore if I run this interactively it works with no issues. Using Python 2.6.6.

2
  • I've added code tags to your source code, please remember to use the markdown code function to make your question more readable.
    – Femaref
    Jun 5, 2013 at 13:57
  • 1
    Thank you!, sorry, first time posting.
    – Ian
    Jun 5, 2013 at 13:58

2 Answers 2

2

I tried to run the following script, and it "works" (Linux Debian / Python 2.6 / gnome-terminal):

print "\x1b[8;40;80t"

print "ok"

The window is resized and the script execution continue.


If you confirm in your case the program stops after resizing, my guess would be Python received a signal SIGWINCH when the window is resized.

You should try to add a specific signal handler. Something like that:

def resizeHandler(signum, frame):
    print "resize-window signal caught"

signal.signal(signal.SIGWINCH, resizeHandler)
3
  • This seems to work, I'm not sure why :) but I'll read up on the signal handlers. Thank you!
    – Ian
    Jun 5, 2013 at 14:41
  • So now that I'm "overloading" the SIGWINCH, to do very little, when I start building a curses screen it fails to get the new cols/rows (which I'm presuming is caught on resize with SIGWINCH). I shot myself in the foot. Can I restore the default SIGWINCH handler before calling curses?
    – Ian
    Jun 5, 2013 at 14:54
  • 1
    @Ian: To obtain the current signal handler you could use signal.getsignal(signal.SIGWINCH). On the other hand, when you change a signal handler with signal.signal(signalnum, handler) the previous signal handler is returned Jun 5, 2013 at 15:03
0

You need to put the terminal in cbreak mode for this. Using the term package (easy_install term) this could look like this:

from term import opentty, cbreakmode

with opentty() as tty:
    if tty is not None:
        with cbreakmode(tty, min=0):
            tty.write('\033[8;26;81t');

print 'terminal resized'
1
  • Would calling curses.cbreak() do the same?
    – Ian
    Jun 5, 2013 at 14:55

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.