highfestiva / finplot

Performant and effortless finance plotting for Python
MIT License
928 stars 187 forks source link

[BUG] Can't save the line chart to an image file in KDE Wayland #530

Open simprince opened 2 months ago

simprince commented 2 months ago

I have been using finplot to generate a line chart and save to an image file without any problems. Recently I notice that when I use KDE Wayland on Linux, it saves an 0 byte file. When I switch back to X11, it works fine. Any ideas why it doesn't work on Wayland?

highfestiva commented 2 months ago

Sorry about that, and no I do not. If you solve it, please write your solution in here or send me a pull request, as it might help others with the same problem.

simprince commented 5 days ago

I find out the reason but I can't fix it. I appreciate your help. @highfestiva

Root cause: In the screenshot() function, grabWindow() doesn't take the screenshot in wayland because wayland disallows pyqt to capture screen content due to the security reason.

Solution (partial): I google how to capture screenshot on wayland. It turns out that Pillow (PIL) has the ImageGrab to do it. I test below and it works on wayland:

from PIL import ImageGrab .... im = ImageGrab.grab(bbox=win_box)
im.save(file.name)

Need help: I've never done any Qt programming and not familiar with pyqt at all. I can't find a way to get the bbox of the window that displays the chart. I tried 1) using the winx, winy, winh, winw; 2) using geometry(); ... Nothing seems to work. The screen captured is quite off from the chart window. If you could point me to how I can get the absolute coordinate of the (x, y) of the top-left pixel of the chart (excluding the title bar of the app) and the bottom-right (x, y). Both are based on the absolute position on screen, I am pretty that it should work. Once it's confirmed to work, I can share the complete solution here.

highfestiva commented 1 day ago
print(fplt.windows[0].pos(), fplt.windows[0].size())

prints something like

PyQt6.QtCore.QPoint(406, 197) PyQt6.QtCore.QSize(579, 366)

on my Windows machine. Hope it helps!

simprince commented 21 hours ago

Unfortunately it doesn't work on wayland either. The x,y,w,h from your codes above is

-3 -30 797 370

Among many who complained the same about wayland, I found this eventually (2nd paragraph): https://wayland-book.com/xdg-shell-in-depth/interactive.html

tl;dr is, wayland intentionally doesn't support positioning/moving of app window programmatically.