想要在python中调整终端窗口的大小,可以运行,但不太正确 [英] Want to resize terminal windows in python, working but not quite right

查看:575
本文介绍了想要在python中调整终端窗口的大小,可以运行,但不太正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在启动python脚本时调整终端窗口的大小,以确保显示为静态大小。它正在工作,但与我的预期不符。我尝试了几种方法:

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")

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

甚至只是

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

它们都可以工作并调整实际终端的大小。但是,如果我的终端是25x80(例如,要启动),则脚本将启动,调整大小,然后退出。它不会执行脚本的其余部分。我将其包装在try / Except中,没有抛出任何异常。 syslogs和python -v script.py中没有任何内容显示任何异常。如果我再次执行脚本或以40x120(我的目标尺寸)的字词大小执行脚本,脚本就可以正常运行。为什么执行ANSI转义退出python?此外,如果我以交互方式运行此程序,则不会出现任何问题。
使用Python 2.6.6。

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.

推荐答案

我尝试运行以下脚本,并且该脚本有效(Linux Debian / Python 2.6 / gnome-terminal):

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.

如果您确认调整大小后程序停止运行,我的 guess 将是Python收到的信号 SIGWINCH 调整窗口大小后。

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)

这篇关于想要在python中调整终端窗口的大小,可以运行,但不太正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆