Python curses getmaxyx() always returning same value on windows - python-3.x

I am having trouble with using the python curses module for windows. I have used the wheel found here to get a script I had written on my mac to run on my desktop. For now, the script just displays a border around the window using the screen.border() method and another line across the whole width of the display. I displayed the bar across the screen using this:
dimensions = screen.getmaxyx()
screen.addstr(dimensions[0]/2, 0, "-"*dimensions[1])
I ran this in a loop, resetting dimensions each time and using getch to check for a curses.KEY_RRESIZE and then running screen.erase() to allow me to resize the window and the script will still work. When I ran this on windows after installing the wheel for python 3.7 (win32 because the amd64 one gave an error) I found that screen.getmaxyx() always returned the same value: the initial screen size, and never changed when I resized the window. I appreciate any help if anyone knows a way to fix this issue, or if I simply cannot use curses on windows, an alternative library for windows. Thank you!

Call resize_term(0, 0) after you get a KEY_RESIZE. (I’m not sure of the exact Python mapping.)

That ("on windows") is probably using PDCurses, which doesn't have a way to automatically update the screensize (e.g., as done with POSIX-based ncurses's SIGWINCH handler). Rather, it detects the window-size change and the application can call is_termresized to decide whether to tell the library to change the data structures to match using resize_term.
The python wrapper doesn't use that.

Related

How to create & save a tkinter.Canvas image on a server w/ no display

I've written a program that generates an image via tkinter.Canvas and saves it. Now I'm updating this project to use FastAPI to let users hit my end-point and download this image.
I'm not trying to display the image on the server, just generate it in memory, save it to storage, then return that image.
Initial proof-of-concept tests work, but I cannot get this to work in Docker because no $DISPLAY is set. Unlike other questions I've seen, I'm not trying to display the image directly, just generate the image, save it, and return a file-path.
This is not a dupe of Tkinter setup on headless server. That question boils down to "how do I not run tkinter while on a server?" I still need tkinter, I just have no intention of displaying it.
I have also seen something about X11, but it seems like I'd need to know the server's environment in order to install X11, and I do not.
Is tkinter really not able to generate an image without $DISPLAY (even if I don't intend to display it)? If so, is there an alternative to tkinter I can use that can generate an image without needing a $DISPLAY? Am I totally misunderstanding a major part of how this works? It seems like I should be able to generate an image and save the bytes without needing all the things required for actual graphical rendering...
Using Pillow.Image and Pillow.ImageDraw instead of tkinter works for my scenario. (Credit goes to #acw1668)
My project only draws lines, circles, and polygons - all things that ImageDraw can do. The process of switching from one to the other was exceptionally straightforward, and Docker returns the ImageDraw/Image file with no issues!

Pyside to build PyQT designer 4 UI file always results in error

Using Pyside-uic -o pythonfilename.py uifilename.ui to convert my PyQT4 designer GUI to python code. Seems to build just fine with no errors, but when I run the file, it consistently crashes, with type errors I can't solve.
First error gave me trouble because I set a "max size". When I removed the max size, the error changed to be a problem with the palette. I removed the palette settings, and replaced it with style sheets, and now it's giving me the error:
"TypeError: QWidget.setFont(QFont): argument 1 has unexpected type 'PySide.QtGui.QFont'"
I doubt there are this many problems with using Pyside to build PyQT UI files(because it seems to be a pretty standard way of doing it), so that leaves me to believe it's something i'm doing.
**Just a note with a bit more info.
This is software we've been using for some time, and the existing UI was built using this exact method. I inherited the file, and was asked to update the UI, and left with instructions on how to do it. I updated the existing ui file, saved it out, and ran the "Using Pyside-uic -o pythonfilename.py uifilename.ui" command. No errors from the build, but %100 of the time i've tried using this method, it has failed.
I tried googling the answer for hours, and proposed the question to other people.
I solved the issue. After pyside spits out the new python file (built from the UI file), theres a line of code at the beginning that will say something along the lines of "from PySide import QtCore, QtGui"
"PySide" has to be changed to "PyQt4".

How to show images in python3

I'm a beginner and I'm having problems of showing images in python3. I would like to know what are some codes that could show images in python 3,f or a mac user. Thanx!
Do you have some more context? Where are you trying to display the image?
Your best bet is probably to open another program and give it the path to the image you want to display. To open the file ~/screenshot.png, for example, you could run
from subprocess import run
run(["open", "~/screenshot.png"])
This is pretty delicate however. It requires the user to have open installed on their computer, and for that executable to do what you expect it to do. Depending on what your usecase is, it might make more sense for you to use a graphical library to draw the image.

Python ANSI Color Codes

Python 3.7, on Windows print does not work as expected for ANSI color codes until shell=True once in subprocess.call().
In the below links it appears to imply that the ANSI color codes should work using the "print" command out of the box.
How to print colour/color in python?
Print in terminal with colors using Python?
the second one mentions VT100 emulation... not sure what exactly that means. I am able to write a batch file that outputs the color fine so I would think (naively) that it should work the same way in Python.
However I am not able to use the ANSI color codes as it seems that the ESC character is being "commented out"(?) because for instance when I
print(u"\u001b[31mHelloWorld")
I am not able to see the colored output, as the ESC character seems to be necessary in Windows and prints in the python shell as "[?]" (a box with a question mark)
Is there something I am missing here?
I found myself an answer. As often happens I just did not look far enough.
the Colorama module can be installed with
py -m pip install colorama
and comes with a method definition at the root of the module called init
colorama.init()
This is a cross platform function in that it is only useful on windows (it saves the active Terminal state for reversal and writes the Terminal to preprocess ANSI codes), it does nothing for other operating systems.
I am thinking about implementing an even more lightweight solution using ctypes and setting the Interpret flags on the active terminal myself.
If you are interested in more information, see here:
https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
Output Sequences
The following terminal sequences are intercepted by the console host when written into the output stream, if the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag is set on the screen buffer handle using the SetConsoleMode flag. Note that the DISABLE_NEWLINE_AUTO_RETURN flag may also be useful in emulating the cursor positioning and scrolling behavior of other terminal emulators in relation to characters written to the final column in any row.
Emphasis mine.

AlphaData alternative in Matlab

In Matlab I'm plotting a matrix (let's call it M) using imagesc over an image using imshow. I would like M to have a degree of transparency so I can actually see the image below. This is what I'm doing:
imshow(img); hold on;
h = imagesc(M);
set(h,'AlphaData',0.4); % set transparency to 40%
However, the last line generates an error when running it on Linux. I've been trying to solve it but nothing seems to work. I wonder if there is an alternative to the "AlphaData" property to make it transparent. Thanks!
EDIT: I'm using Matlab R2014a and Java 1.7 on a Linux CentOS 6.6
As Luis Mendo suggested, I just needed to change the renderer. You can:
>get(gcf,'renderer'); % to see which render engine is Matlab using
>set(gcf,'renderer'); % to get a list with all the possible renderers in your machine
So, at least in Linux, to change the renderer it's necessary to start Matlab from terminal by calling it as:
matlab -softwareopengl
Once this is done, setting transparency in an specific plot, as shown in the description of the question, is possible.

Resources