用 Python 调整终端的大小? [英] Resize the terminal with Python?

查看:37
本文介绍了用 Python 调整终端的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过快速的 Google 搜索找到任何内容,这里也没有任何内容,只为 这个.但是,它并不能解决问题.那么,您究竟如何使用 Python 调整终端的大小?

I couldn't find anything with a quick Google search, nor anything on here, saving for this. However, it doesn't do the trick. So how exactly do you resize the terminal using Python?

推荐答案

要更改 tty/pty 设置,您必须在 stdin 文件描述符上使用 ioctl.

To change the tty/pty setting you have to use an ioctl on the stdin file descriptor.

import termios
import struct
import fcntl

def set_winsize(fd, row, col, xpix=0, ypix=0):
    winsize = struct.pack("HHHH", row, col, xpix, ypix)
    fcntl.ioctl(fd, termios.TIOCSWINSZ, winsize)

但是要更改实际窗口大小,您可以使用终端转义序列,但并非所有终端都支持或启用该功能.如果您使用的是 urxvt,您可以这样做:

But to change the actual window size you can use terminal escape sequences, but not all terminals support or enable that feature. If you're using urxvt you can do this:

import sys
sys.stdout.write("\x1b[8;{rows};{cols}t".format(rows=32, cols=100))

但这可能不适用于所有终端.

But that may not work on all terminals.

这篇关于用 Python 调整终端的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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