Python curses not showing certain characters - python

This may be a question better suited for UNIX stack exchange but thought I would post here as I do not know what exactly the problem is. I have a python script that uses curses and displays characters from font awesome 5 free. This script worked fine, but just recently I reinstalled my os and now it is not displaying these characters. The terminal I am using is capable of displaying the character on the cli, python is able to print the character, it is only when it is echoed through curses that it does not display. Any help would be greatly appreciated.
More information:
Here is a mock up of the script:
import curses
from curses import wrapper
def main(stdscr):
while True:
stdscr.addstr(0,0,STRING)
stdscr.refresh()
wrapper(main)
when STRING is something like "a", "b", or "cat" it shows up fine. However when the character is something like "" or "" it does not display anything.

Related

Python, print function ignoring ANSI color commands

I have a similar problem as explained in this post
ANSI escape code wont work on python interpreter
I am using Macbook Pro and Python 3.7.0. When I give a print command with ANSI commands it prints the characters rather than take effect
>>> print ('\033[1;32;48m hello')
[1;32;48m hello
I tried with colorama and also initialized it as shown below
from colorama import init, Fore, Back, Style
init(convert=True)
Not sure what can I do to get it to print in color

Why is IDLE's Shell color coding keywords in user response to input(prompt)?

I'm working on a Python 3 program that takes input from the user via the Python Shell. For some reason when I enter the information into the shell (once the input asks for info...), it's coloring keywords & functions with specific colors.
For example, if I type in "John is blue". It will color the word "is" as a keyword (Which it technically is but this is string input).
I haven't been able to bring up anything relevant on google so I'm bring the question here. Thanks.
Here is the code that runs the input.
if __name__ == '__main__':
global string
string = str(input('Enter info: '))
string = bytes(string.encode("utf-8"))
c = cont.key_gen_01()
c.func_01()
run_a = obf_01()
run_a.func_02()
#run_a.func_03()
run_a.func_04()
run_a.func_05(string)
Screen shot:
Colorizing in IDLE editor and Shell windows is done by the IDLE syntax colorizer. In Shell, it also colors console prompts ('>>> '), internal IDLE errors (now extremely rare), user code tracebacks, and user code output. (Colors can all be customized on the Highlights tab of the Settings dialog.) So the colorizer should not be turned off between code entries.
Unless your program is requesting entry of python code, I consider colorizing input() responses to be a minor bug. But it is not obvious how to tell the colorizer to ignore them. To a display, input() prompts are normal output. Besides which, responses can be entered before the prompt. Try the following with or without hitting ENTER before the prompt.
import time; time.sleep(5); s = input('what??? '); print(s)
The above also works in python, but at least in Windows Console, I don't see the entry until the prompt is displayed.
The Python input() function doesn't and can't do anything with how text you enter is displayed. It merely receives text after it has been typed and you hit enter. Instead, the color changes are applied by the IDLE shell window, which implements the input and output environment that the Python interpreter is connected to.
The IDLE Python shell treats all user input as Python source code when it comes to highlighting. This is just a change in how the text is rendered on your display, it has no influence on what string value is being returned from the input() function.
Other environments (IDE consoles, terminal windows, notebook UIs, etc) that can act as front-end displays for a Python interactive interpreter each can have their own specific ways of treating text.
You can print syntax highlighted text using python pygments
A good way to get started with is the prompt_toolkit moudle, comes with support for pygments and ALOT of command line features
Then you can create an prompt() which is input, and set the pygments lexer
check the prompt_toolkit docs or https://replit.com/#CodinUser/PyShell-15 to see a real life use of the syntax highlighting input pygments prompt_toolkit

Python 3.5: Input function not requiring carriage return, accepts only one character, and character returned only if given condition satisfied

I am designing a game in which I require the input function to accept input without requiring the user to press the enter key. Also,once the user enters a single character,the variable in which the input is supposed to be stored must contain that character at once(as if the user had pressed the enter key after entering that character). The character entered must be displayed on screen only if it satisfies a given condition. Please enlighten me on how I can achieve this. Thank you.
NOTE:I am a beginner in Python, hence please do not delve into things that are way beyond my comprehension.My teacher has forbidden us from using classes for our project. I should be able to utilize the characters displayed on screen which I don't find possible using the solutions given for other similar questions.
After searching, I found a packaged named msvcrt, and it can be helpful in your situation.
For more information, please check: raw_input in python without pressing enter.
Following is a simple example(it works in Windows). You should open your command line and use python xxx.py to execute the following program.
import msvcrt
import sys
while True:
c = msvcrt.getch()
if c == b'\x03': # Ctrl-C
sys.exit()
#print(type(c)) # bytes
#convert from bytes to str
c = c.decode('utf-8')
if c =='a': #replace 'a' with whatever you want
print(c)
It takes the single character input from msvcrt.getch() and assign it to the variable c.
And it prints that character if its string format equals to 'a'.
If you want to exit the program, just press Ctrl-C.
EDIT:
From Python kbhit() problems, it seems msvcrt.getch cannot work in IDLE by nature. But there is a workaround (adjusted from: PyCharm: msvcrt.kbhit() and msvcrt.getch() not working?). If you are using Spyder, click Run -> Configuration per file -> Execute in an external system terminal. And when you later run the script file, a console window will jump up.

Python curses - textpad.Textbox() keyboard input not working with German umlauts

I'm trying to use the curses textpad.Textbox() function for text input. Everything is working fine so far, however, some keys don't get recognized, including the section sign (§) and all German umlauts (ä/ö/ü). I guess it's somehow related to the text encoding, but I have no idea how to fix this. My German keyboard layout works perfectly fine with input().
Here is some minimal example:
import curses
import curses.textpad as textpad
try:
stdtscr = curses.initscr()
curses.cbreak()
stdtscr.keypad(1)
curses.noecho()
textpad.Textbox(stdtscr).edit()
finally:
curses.nocbreak()
stdtscr.keypad(0)
curses.echo()
curses.endwin()
Just as in C, you should initialize the locale. It's spelled out in both the Python documentation:
Since version 5.4, the ncurses library decides how to interpret non-ASCII data using the nl_langinfo function. That means that you have to call locale.setlocale() in the application and encode Unicode strings using one of the system’s available encodings.
and the ncurses manual page:
The library uses the locale which the calling program has
initialized. That is normally done with setlocale:
setlocale(LC_ALL, "");
If the locale is not initialized, the library assumes that
characters are printable as in ISO-8859-1, to work with cer-
tain legacy programs. You should initialize the locale and
not rely on specific details of the library when the locale
has not been setup.
Addressing the followup comment, textpad.py does not expect UTF-8 input in any case. Essentially it "validates" its input, decides it isn't ASCII and ignores it when it's not.
Python's curses binding provides an interface to wgetch, which (with ncurses) gives the individual bytes for the UTF-8. (X/Open Curses specifies a different function wget_wch, for which Python has no binding).
textpad.py could be modified to work around the curses binding by assembling the bytes into a Unicode value, but you'd need the setlocale as the first step.

Is it possible to print a string at a certain screen position inside IDLE?

EDIT: I just discovered that it's possible to obtain a similar behaviour by using the standard library "curses". There are some demonstrations about how it works here and there, for example on YouTube: http://www.youtube.com/watch?v=Bj-H9uPEa5U
It's a strange and silly question I know, but I'm curious because I don't know that much about python and how it works.
From the terminal or when you use IDLE, is there any way to print a string at a certain screen position?
I'll try to explain this better: Do you remember the old days when you used to make small programs in Basic, maybe on a Commodore 64, Apple II or ZX Spectrum?
During that days if you wanted to print a string at a certain position you used to write something like this:
10 LOCATE 30, 40 : PRINT "hello world"
I'm just curious to know if there's any way to tell python to print a string at a certain position, and if there's a way to know how many columns and how many rows can be actually displayed inside the IDLE window.
I've also made a mockup draw, to explain this concept.
I don't know if this works on IDLE, but it does in any normal terminal:
import sys
def print_there(x, y, text):
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (x, y, text))
sys.stdout.flush()
This uses Ansi-Escape Sequences
This question only has one real answer and it isn't a very good one. The method:
import sys
def print_there(x, y, text):
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (x, y, text))
sys.stdout.flush()
Isn't perfect. I'd recommend staying clear of doing things like this in the terminal. If you want to do Gui's and stuff use Pygame, Tkinter, or Django.
To avoid the issue raised by #user3431399, you first need to make win32 console recognize ANSI/VT100 escape sequences. I got the same problem as #user3431399 on my Windows 10 terminal and I solved it by following the solution recommended by #Daniel De Léon. That is, I logged in as administrator at the windows prompt (cmd command). Then I copied, pasted, and ran the command.
REG ADD HKCU\CONSOLE /f /v VirtualTerminalLevel /t REG_DWORD /d 1
Use Tkinter to make it perfect by select under frame and provide row and column number to display your output

Categories

Resources