I am using the python curses library. I am trying to make an asterisk blink using this code win.addstr(6, 4, "*", curses.A_BLINK) However it does not work on gnome terminal. I tried using it on xterm and it works. It also does not work on the recovery shell. How can I make text blink using the curses library or some other method?
You could make a program (whether with curses or even hardcoded) that draws text on the screen and overwrites it with blanks, with a suitable time delay (if it's too short, it annoys people — see PuTTY for an example of that).
The drawback is that it would "blink" only as long as the program runs, and of course it's a little complicated.
As a shell script, you could do this:
save the stty settings,
change the stty settings to prevent output of carriage return (\r) from being converted to \r\n
print the text, ended with \r
wait a while, e.g., sleep 1
print blanks where you wrote text
wait a while
loop back to the first "print"
on exit, restore the stty settings.
For a curses application - you could make it "blink" by replacing the text, in a similar way. For what it's worth, the xmas example in ncurses-examples uses a combination of window copying and terminal blinking for its animation effects (see C blinkit function and Python translation).
Related
When you open vim or manpage you can't scroll up and check your previous commands, you just stay in this isolated display.
How does this magic work? How to implement it with python?
Or at least say how this thingy is called so I google it properly.
That is called The Alternate Screen Buffer. The terminal emulator provides two modes: primary and alternate; the latter has no scroll back, and when you switch between these buffers the contents in them is preserved. The smcup and rmcup ANSI Escape Sequences are used for switching.
For example, the following displays a FOOBAR banner text for three seconds in the alternate buffer, then switches back to the primary one:
$ tput smcup; banner foobar; sleep 3s; tput rmcup
In Python, you could print the corresponding escape sequences on application start and exit; there may even be a library that does that for you.
From the Python Idle shell, the output from the help() command is not paged. Is there any way to achieve this?
As a workaround I've tried to use pydoc.pager() but with no success. The following works in a normal Python shell started from the terminal but not in the Python Idle shell.
import pydoc
def ihelp(thing):
'''Render text documentation, given an object or a path to an object, and
sends the resulting text to a pager.'''
pydoc.pager(pydoc.render_doc(thing))
Edit: Just to avoid misunderstandings. The ihelp() functions gets the help text both in a terminal Python shell and in the IDLE shell. Paging only works in the terminal shell.
You code works in delivering help output but not in paging it. The pager executes in the user code run process and does not know how to interact with an IDLE shell in a separate gui process. I don't believe you can change this.
To address this issue, IDLE has a new feature, starting with 3.6.7 and 3.7.1 (the release candidates are available now, the final releases will be out in a week or so). IDLE squeezes 'long' output to a button. (By default, 'long' is 50 lines, but user can modify this. Shorted output can be squeezed by right-clicking on it.)
>>> help(int)
[ Squeezed text (241 lines) ] # <== tkinter button
>>>
You can expand the text in place, with a double click, or into the clipboard or a separate non-modal view window, with a right click. A separate window lets one scroll or page up and down in the help entry while writing a statement in the Shell.
I'm learning python and playing with the subprocess module. After reading several different tutorials and descriptions of how to use the module, my understanding was still not incredibly solid so I started working up some simple code to test the various functions and see how everything worked.
I wanted to see how it might behave if I tried to start the same process multiple times, using this code:
import subprocess
def tpltest(x):
while x > 0:
try:
subprocess.Popen('wordgrinder')
except:
print 'something broke!'
x -= 1
x = raw_input('how many?')
tpltest(x)
When I ran this in my terminal, wordgrinder (terminal word processor) opened normally, but then the cursor advanced down the screen at a rate of about one line per second. It appeared to be movement only, rather than any characters being added to the blank file onscreen. I pressed ctrl+c to see if a wordgrinder remained after closing the first instance and was met with my regular command line prompt, but the cursor continued to scroll. I ran top and did not see extra wordgrinder instances open, but the cursor continued scrolling.
Then I discovered the oddest part. If I rolled my mouse scroll wheel up in the window, chunks of text appeared over the window's contents. Some of the chunks were commands I had entered in the console over the past week, some of them were commands I had entered in my python shell but not the regular terminal, and some of the text was my WiFi security information, including the plaintext WPA2 password for my network. Scrolling the mousewheel down had no effect but scrolling up would overwrite a few lines with new text, seemingly from other random places in my system. After closing the terminal window and opening a new one, things were back to normal.
I'm running Debian 8 64bit with XFCE and using the default terminal emulator.
What did my code do, and why?
The explanation that I can offer is that what subprocess does is to run a background process. Additionally, wordgrinder is a program that takes full control of the terminal session by capturing input from the keyboard (stdin) and controlling the command-line (stdout) in a very particular way.
When you ran wordgrinder in the background through your script, it caused some funky things to happen. That's pretty crazy that your WiFi password ended up getting displayed from your experiment.
I'm doing a script with a menu-like beginning , and I wanted to know if I could do something like this:
You open the file and it prints this menu :
LOGO
Welcome to script 11 , what would you like to do?
-write a file
-read a file
-create a file
> #input here
You select write ( for example) and it prints this OVER the previous menu intead of printing if after it:
LOGO
Writing a file!
-Select the path:
> #input here
Thanks for the help in advance.
Edit: I didn't want to completely erase the screen( I've seen the other threads about that) but if there is a method to erase only some lines , but the anwers tell me that it isn't possible without external libraries so I'll just clean all the screen and reprint some things, but thanks for all the anwsers
If you are using Linux (or OSX), you can use the curses module.
If you are using windows, use the console module.
If you want to display a multi-line menu, and display an entirely new one after each menu choice, there are only a few ways to do this:
Clear the screen before printing each menu. There are about 69102 questions on SO about how to do this; how to clear the screen in python has links to many of them.
Print out a form feed/page feed. Which will not work on many modern terminals (Windows or Unix), but it rocks on old-school teletypes.
Use terminal control sequences to move the cursor around. This will work on Windows if the cmd terminal is ANSI-enabled (I believe it usually isn't by default), and on everything else if you pick (or look up) the right terminal to send control sequences for. You will also have to make sure to overwrite each character, not just each line. Otherwise, when you overwrite line 2 you'll end up with "Writing a file!pt 11 , what would you like to do?".
Use curses (and your favorite third-party Windows curses port), or some other terminal graphics library, to do this all at a higher level. (Writing separate code using console if it exists—for Windows—and curses otherwise—for almost everything else—is often the simplest way to do this.)
I'd suggest 1 or 4, but those are your options.
hi everyone :)
my problem is, up until now, i have exclusively used tabs to indent python, as i find it easier than spaces, but for no reason i know, python interactive prompt, the basic python.exe one, suddenly refuses to accept the tab button, all it does is flash the cursor. all i can think of is that my computer in suddenly treading the window like any other, using tab to cycle input things, in this case the single one. also, before now, i could use the up button to reach previously typed code, the if i submit that line with no changes, use the down button to access the line that came after it, but now up works, but as if i had changed the line, eg moves me back to the "bottom" of the list of inputs, so down doesn't work.... my question simply is: how do i get my good old tab and down button to work like i want them to again? :(
thanks xxx
If you are using Windows with the standard cmd.exe console (and it would have been helpful for you to have stated this up front) then you can use the TAB and arrow keys exactly as you desire.
I recently observed this behavior too, on Windows, using cmd.exe. It also happens with Console2 - an alternate shell I sometimes use.
Though I do always use spaces in normal code in an editor, I had been accustomed to using the Tab key to indent in short multi-line inputs in the interactive python.exe interpreter. Recently that stopped working - pressing the Tab key flashes the cursor and doesn't indent. Using spaces does work fine here, though it's not as convenient past a couple of indentations.
I suspect (but am not certain) that the cause was installation of pyreadline or rlcompleter - I had been messing with trying to get tab completion in an interpreter in an embedded application on Windows. Of course in your case another installation could have included those packages.