问题
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.
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.
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.
回答1:
You can not do it from you python script (OK, it is possible, but most probably you don't want to do it). Scrolling depends on environment (Windows or Linux terminal, doesn't matter). So it is up to users to set it up in a way that is good for them.
On Linux you can use less
or more
:python script.py | less
it will buffers output from the script and will let user ability to scroll up and down without losing any information.
回答2:
This, my friend, will allow scrolling on both the dimensions.
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.
来源:https://stackoverflow.com/questions/36341867/how-to-enable-scrolling-for-python-console-application