raspberrypi / picamera2

New libcamera based python library
BSD 2-Clause "Simplified" License
780 stars 164 forks source link

Can't use picamera2 in a Tkinter GUI #942

Open Carl0sC0elh0 opened 5 months ago

Carl0sC0elh0 commented 5 months ago

Hello everyone, I'm developing a Tkinter app to help me with some image processing. My objective is to take a photo from the picamera v3 module and store it for further analysis. the problem is that the code doens't run and aborts the app with this output

QObject::moveToThread: Current thread (0x7f580286a0) is not the object's thread (0x7f581f82b0). `Cannot move to target thread (0x7f580286a0)

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/Carlos/FISCHER/lib/python3.11/site-packages/cv2/qt/plugins" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

I'm trying to use threading as well so that the tkinter app keeps running while the camera takes the shot, but even without threading the app closes when it reaches the segment to take a shot. Any advice on this? Is it a dependencies problem? Thanks in advance

tvoverbeek commented 5 months ago

Tkinter is not supported on Wayland (default on Pi3b+, Pi4 and Pi5). You need to be running X11. On Bookworm run raspi_config, goto Advanced Settings, Select Wayland and select X11 and Openbox. I do not know if this relevent to your issue.

davidplowman commented 5 months ago

Are you able to post a really short and simple self-contained snippet of code (preferably just a dozen lines or so) that would allow others to reproduce the problem easily? That would make it easier to understand and help. Thanks.

bnd762 commented 5 months ago

I solved a similar problem using Pygame. This actually works quite well. https://github.com/raspberrypi/picamera2/issues/607#issuecomment-1730933656

Carl0sC0elh0 commented 5 months ago

Thank you all for your suggestions but it was indeed a dependencies conflict or something like that. My venv didn't have picamera2 and libcamera2 installed and when installed (couldn't use pip, idon't know why), they had missing packages. I solved it by creating a new venv with --system-site-packages. Is it a good fix? I don't know, im just a noob with a PI4.

Thank you all and have a good week!

davidplowman commented 5 months ago

Yes, the easiest solution for a venv (unless you have some very particular requirements which mean it isn't appropriate) is to use --system-site-packages.