Python Terminal Menu? Terminal Coloring? Terminal

2019-04-02 05:50发布

I have a project that uses Python (2.* flavors) extensively and I am wondering if there is a terminal menu library or something to that effect? I am looking to breathe some flavor and life into my script by simplifying some of the options using arrow key highlightable options, some color, etc etc. I vaguely recall there being a way to make a bash shell terminal menu but I'm not at all sure how I would pass user input from bash to the python script, perhaps have a bash terminal menu push the script call with sysarggs? I'd like something on the python side if possible. Any suggestions?

Also just a random question, kind of fits in here since we're on the topic of terminal aesthetics, what's the best way to handle a counter? My script looks for image files, then when it finds one it clears the terminal with a subprocess call to clear and then prints the total images found again IE 10 images, find one, clear, print "11 images found", sometimes my script works REAL fast and I feel this detriments performance. Thoughts?

Thanks so much everyone, I love stack overflow ;)

Edit - Thanks for all the quick responses! I have alot of options to mull over. I gave everyone an upvote because all of your responses are helpful. I will check out all the libraries when I get home and try to pick one of you for an answer depending on what hashes out the best, wish I could pick you all though because all of your answers are relevant! Much appreciated folks. I'll report back in once I get home from work and get a chance to get some coding in ;)

Edit 2 - A clarification on the counter/progress display, looking for a way to keep this from detrimenting performance when my script finds thousands of images in a very short ammount of time, this is real chopped up python...

for each item in list:
    if item ends with .jpg
        cnt=cnt+1
        do stuff with image file
        subprocess.call('clear')
        print str(cnt)+" total images processed."

Thanks again!

3条回答
狗以群分
2楼-- · 2019-04-02 06:35

Check out Clint (*C*ommand *L*ine *IN*terface *T*ools)!

Example colors:

from clint.textui import colored

print 'I love ' + colored.yellow('pyt') + colored.blue('hon')

and indents too:

from clint.textui import colored, indent, puts

with indent(3, quote=colored.red(' >')):
    puts ('some random text')
    puts ('another text')
    with indent(3, quote=colored.green(' |')):
        puts('some more nested identation')
        puts('cool isn\'t?')

P.S. The same author wrote a similarly nice HTTP request library called "requests": https://github.com/kennethreitz/requests

查看更多
冷血范
3楼-- · 2019-04-02 06:46

There's a library called Urwid that offers menus and some more. I never used it for serious purposes, but the it work pretty fine with my preliminary experiences on it. It works on Un*x systems only though. (The project page says it works under Cygwin, but I never tried.)

查看更多
一纸荒年 Trace。
4楼-- · 2019-04-02 06:51

If you want a lot of control and you're on *nix, you could use the stdlib curses module.

If you just want a bit of color (/don't want to modify your script a ton to fit curses), you can use ANSI escape codes. For example:

print '\033[1;32mgreen\033[1;m'

will print the word 'green' colored... green.

Here's a loading bar I came up with using carriage returns (based on the answers in this forum):

from time import sleep 
import sys 

num = 100

print 'Loading: [%s] %d%%' % (' '*(num/2), 0),

try:
    colorCode = 43
    for x in xrange(num+1):
        if x == num: colorCode = 42
        print '\rLoading: [\033[1;%dm%s\033[1;m%s] %d%%' % (colorCode, "|"*(x/2), " "*(num/2-x/2), x), 
        sys.stdout.flush()
        sleep(0.02) # do actual stuff here instead 
except KeyboardInterrupt:
        print '\rLoading: [\033[1;41m%s\033[1;m%s] %d%%  ' % ("|"*(x/2), " "*(num/2-x/2), x)

Example Output:

Loading: [|||||||||||||||||||||||||||||||||||||||||         ] 82%

(Although it doesn't show up on SO, it is colored- yellow for loading, red for abort, and green for done.)

查看更多
登录 后发表回答