简体   繁体   中英

Python - Curses : how to use the inch method to get the attribute of a character

I am learning python and curses. I am at a point where I want to be able to tell if a specific character is either A_BOLD, A_DIM or A_REVERSE etc... So I could eventually change its attribute accordingly (using for example window.chgat(attr)).

But I do not know how to retrieve this information.

According to the documentation:

window.inch([y, x])¶

Return the character at the given position in the window. The bottom 8 bits are the character proper, and upper bits are the attributes.

I understand that the information about the character attribute is incorporated within the result from inch and as a matter of fact, printing the character obtained displays it with its attributes as well.

But Im not fluent enough in computer speak to understand how to use this. How do I get and interpret those upper bit?... What should I do, say, to check if the character is printed in bold or not?

You need to use bitwise operators (eg & )

attrs = window.inch([y, x])
ch = chr(attrs & 0xFF)
isbold = bool(attrs & curses.A_BOLD)

etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM