1

The following code works perfect on a Windows OS though does not work on the raspberry pi. I'm looking for information as to why this is, I have scoured the web for a solution but cannot find one.

from distutils.command.config import config
import logging
from logging.handlers import RotatingFileHandler
import os
from logging import root
import tkinter
import tkinter as tk
import tkinter.messagebox
import customtkinter
from threading import Thread
from tkinter import Label
from random import *
from itertools import count, cycle
from tkinter import *
from tkinter import ttk, Scrollbar
import platform
import pygame

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        tabview = customtkinter.CTkTabview(self, height=500, width=500)
        tabview.place(x=-5, y=-50, anchor=tkinter.NW)
        tabview.add("tab_main")
        tabview.set("tab_main")

        try:
            pygame_width = 400
            pygame_height = 400
            embed_pygame = tk.Frame(tabview.tab("tab_main"), width=pygame_width, height=pygame_height)
            embed_pygame.place(x=10, y=10)
            self.update()
            os.environ['SDL_WINDOWID'] = str(embed_pygame.winfo_id())
            system = platform.system()
            if system == "Windows":
                os.environ['SDL_VIDEODRIVER'] = 'windib'
            elif system == "Linux":
                os.environ['SDL_VIDEODRIVER'] = 'x11'

            display_surface = pygame.display.set_mode((pygame_width, pygame_height))
            display_surface.fill((0, 50, 0))
            pygame.init()
            background_surface = pygame.Surface((pygame_width, pygame_height))

            clock = pygame.time.Clock()

            def handle_tkinter_events():
                self.update_idletasks()
                self.update()

            while True:
                handle_tkinter_events()

                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        exit()
                    elif event.type == pygame.MOUSEBUTTONDOWN:
                        print('Clicked on image')

                display_surface.blit(background_surface, (0, 0))
                pygame.display.update()
                clock.tick(60)

        except Exception as e:
            logging.critical("An exception occurred: {}".format(e))
            print("An exception occurred: {}".format(e))


if __name__ == "__main__":
    app = App()
    app.mainloop()
Skully
  • 2,882
  • 3
  • 20
  • 31
user1438082
  • 2,740
  • 10
  • 48
  • 82
  • 3
    “event does not work” [is not a question we can answer](https://idownvotedbecau.se/itsnotworking/). What did you expect to happen? What does it do instead? [Did you try debugging](https://idownvotedbecau.se/nodebugging/)? – Dour High Arch Jun 04 '23 at 20:07
  • @DourHighArch it should write to the console "Clicked on image", Which it does not on the raspberry pi. It does on windows. – user1438082 Jun 04 '23 at 20:10
  • 7
    Embedding Pygame into a tkinter window is operating system dependent and does not work with all operating systems. It works for Linux, Mac and since a short time also for Windows (also see [Embedding a Pygame window into a Tkinter or WxPython frame](https://stackoverflow.com/questions/23319059/embedding-a-pygame-window-into-a-tkinter-or-wxpython-frame)). So it is probably just not implemented for raspberry pi os. – Rabbid76 Jun 04 '23 at 20:10
  • 1
    Have you seen [this link](https://learn.adafruit.com/raspberry-pi-pygame-ui-basics/overview) ? – Thingamabobs Jun 07 '23 at 06:11
  • 1
    @user1438082 Did you get any errors? Can you get it to print anything else to the console? Is there anything else not working? – Andria Jun 07 '23 at 16:02
  • What is the raspberry pi os version? – Kostas Nitaf Jun 12 '23 at 20:15

1 Answers1

0

Here are a few points to consider:

  1. Verify that Pygame is installed on your Raspberry Pi. You can use the following command to install it:
pip install pygame
  1. Check the version of Pygame installed on your Raspberry Pi. Some features or behaviour might differ between different versions. Ensure you have the latest version installed:
pip install --upgrade pygame
  1. Raspberry Pi typically uses the Linux operating system, so the code snippet includes a check for the system type. However, the provided code only sets the SDL_VIDEODRIVER environment variable for Windows and Linux. You may need to add another condition to handle the specific case of Raspberry Pi.
  2. Make sure you have the necessary dependencies and drivers installed on your Raspberry Pi to support Pygame and the SDL (Simple DirectMedia Layer) library it relies on. This might involve installing additional packages specific to your Raspberry Pi's operating system.
  3. Pygame relies on the underlying graphics system to create and manage its windows. On Windows, it typically uses the "windib" driver, while on Linux, it uses the "x11" driver. In your code, you set the SDL_VIDEODRIVER environment variable based on the system type, but it's possible that the driver you specified is not compatible with the Raspberry Pi's environment.
  4. The Raspberry Pi typically uses the "dispmanx" driver for its graphics system. To make Pygame work correctly on the Raspberry Pi, you might need to set the SDL_VIDEODRIVER environment variable to "dispmanx" instead. You can try modifying your code as follows:
import platform
import os

# ...

if system == "Windows":
    os.environ['SDL_VIDEODRIVER'] = 'windib'
elif system == "Linux":
    if platform.machine() == "armv7l":  # Raspberry Pi architecture
        os.environ['SDL_VIDEODRIVER'] = 'dispmanx'
    else:
        os.environ['SDL_VIDEODRIVER'] = 'x11'

  1. Consider using a different method for embedding the Pygame window into the Tkinter interface. The method used in the code snippet might not be compatible with Raspberry Pi. You can explore alternative methods, such as using Pygame's pygame.Surface object to render the game screen within a Tkinter canvas.
  2. To debug the issue further, you can try running the code without the Tkinter interface on the Raspberry Pi to see if Pygame functions correctly on its own. This will help isolate whether the issue lies with the Pygame installation or its integration with Tkinter.
  3. Check the system logs or any error messages you receive when running the code on the Raspberry Pi. They might provide more specific information about the problem.
Rabbid76
  • 202,892
  • 27
  • 131
  • 174