如何为Python控制台应用程序启用滚动 [英] How to Enable Scrolling for Python Console Application

查看:105
本文介绍了如何为Python控制台应用程序启用滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Windows(8.1)上编写的Python(2.7)控制台应用程序。我已经使用 argparse.ArgumentParser()在执行程序时处理参数。

I have a Python (2.7) console application that I have written on Windows (8.1). I have used argparse.ArgumentParser() for handling the parameters when executing the program.

该应用程序有很多参数,因此使用-help 参数时,文档大大超出了控制台窗口的大小。即使控制台窗口已最大化。很好,但是我遇到的问题是用户无法向上滚动以查看其他帮助文档。我已经适当地配置了Windows控制台属性,例如窗口大小和屏幕缓冲区大小。而且我已经验证了这些更改是否有效,但是它们仅在Python环境之外有效。一旦执行Python脚本或对脚本运行-help 命令,控制台属性便不再适用。滚动条将从窗口中消失,并且我无法再滚动到顶部来查看先前的内容。

The application has quite a few parameters, so when the --help parameter is used the documentation greatly exceeds the size of the console window. Even with the console window maximized. Which is fine, but the issue I'm encountering is that the user is unable to scroll up to view the rest of the help documentation. I have configured my windows console properties appropriately, such as the "Window Size" and "Screen Buffer Size". And I have verified that those changes are working, but they only work outside of the Python environment. As soon as I execute a Python script or run a --help command for a script, the console properties no longer apply. The scroll bar will disappear from the window and I can no longer scroll to the top to see the previous content.

因此,基本上,我需要弄清楚如何启用滚动用于我的Python控制台程序。在执行脚本和查看-help 文档时,都需要启用滚动功能。我不确定该怎么做。我一直在网上搜索有关该主题的任何信息,但是我什至没有找到任何远程帮助。

So basically, I need to figure out how to enable scrolling for my Python console programs. I need scrolling enabled both when executing a script and when viewing the --help documentation. I'm not sure how to go about doing that. I have been searching online for any info on the subject and I have yet to find anything even remotely helpful.

在这一点上,我完全陷入了困境。因此,如果有人知道如何滚动工作,我将非常感谢您的帮助。

At this point, I am completely stuck. So if someone knows how to get scrolling to work, I would greatly appreciate your help.

推荐答案

我的朋友,这将允许

from ctypes import windll, byref
from ctypes.wintypes import SMALL_RECT

WindowsSTDOUT = windll.kernel32.GetStdHandle(-11)
dimensions = SMALL_RECT(-1, -1, 89, 28) # (left, top, right, bottom)
# Width = (Right - Left) + 1; Height = (Bottom - Top) + 1
windll.kernel32.SetConsoleWindowInfo(WindowsSTDOUT, True, byref(dimensions))  

如果您想修复在任意一侧滚动,

In case, you want to fix scrolling on any side,

import os
os.system('mode horizontalSizeInNumber or horizontalSizeInNumber_setToZero and verticalSizeInNumber')

之前添加此段代码以上的书面代码。

Add this piece of code before the above written code.

这篇关于如何为Python控制台应用程序启用滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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