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

查看:41
本文介绍了想在 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.

如果您确认在您的情况下程序在调整大小后停止,我猜测将是 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天全站免登陆