0

I have encountered a certain issue for which I am seeking assistance. I have developed a primary application using the Pygame library. Within this program, I have defined specific regions (represented as rectangles) that, upon being clicked, should trigger the opening of new windows. However, Pygame itself does not inherently provide this functionality as a library feature. Consequently, I am attempting to achieve this behavior using the Tkinter library (in this case, a customized version of Tkinter). The underlying principle remains the same, though with additional customization options available in this custom Tkinter implementation.

# on main - pygame while loop
elif event.button == 3:
    for idx, rectangle in enumerate(rectangles):
        rect = rectangle.surface.get_rect(topleft=(rectangle.x, rectangle.y))
        if rect.collidepoint(event.pos):
            # rectangle.open_additional_window()
            threading.Thread(target=rectangle.open_additional_window()).start()

# on Rectangle class
    def save_additional_window_data(self, angle_value: str, additional_window: ctk.CTk) -> None:
        """Updates angle_value info in this rectangle and handles additional window interaction."""
        if self.angle != int(angle_value) and int(angle_value) % 4 == 0:
            self.angle = int(angle_value)
            self.change_universe()
        additional_window.destroy()

    def open_additional_window(self) -> None:
        """Opens a window to modify rectangle properties."""

        def on_save_button_click() -> None:
            """Reads provided values and triggers function to save it"""
            angle_value = angle_entry.get()
            self.save_additional_window_data(angle_value, additional_window)

        additional_window = ctk.CTk()
        additional_window.config(bg=from_rgb(BACKGROUND_COLOR))
        additional_window.iconbitmap("icons/icon.ico")
        additional_window.geometry(f"250x200")
        additional_window.title(f"Rectangle {self.index}")

        content_frame = tk.Frame(additional_window, bg=from_rgb(BACKGROUND_COLOR))
        content_frame.pack(padx=10, pady=10)

        angle_label = tk.Label(content_frame, text="Value of the angle:",
                                        bg=from_rgb(BACKGROUND_COLOR), foreground=from_rgb(FONT_COLOR))
        angle_label.pack()

        angle_entry = tk.Entry(content_frame, bg=from_rgb(BACKGROUND_COLOR), foreground=from_rgb(FONT_COLOR),
        additional_window.mainloop()

Regrettably, I am facing a recurrent problem where these additional windows often experience crashes, only becoming responsive after a considerable delay. Presently, I am creating these new windows within separate threads. This is necessary, as attempting to open these windows within the main thread leads to the following error:

invalid command name "2245603785792update"
    while executing
"2245603785792update"
    ("after" script)
invalid command name "2245531912512check_dpi_scaling"
    while executing
"2245531912512check_dpi_scaling"
    ("after" script)
invalid command name "2245603847808_click_animation"
    while executing
"2245603847808_click_animation"
    ("after" script)

I am uncertain about the optimal approach to optimize this situation. Any guidance or suggestions would be greatly appreciated. I would also like to add that, during the process of opening an external window, the program might experience interruptions. Therefore, the utilization of a separate thread is deemed optimal in this context. However, in the absence of this threading approach, the program encounters an error as described earlier.

The aforementioned approaches presented below facilitate the embedding of a Pygame window within a Tkinter window. However, my intention is to achieve the inverse scenario, where I aim to embed a Tkinter window within a Pygame window. It's important to note that this distinction entails a reversed hierarchy in terms of graphical integration, which may necessitate different implementation strategies and considerations.

Embedding a Pygame window into a Tkinter or WxPython frame

I'm embedding a pygame window into Tkinter, how do I manipulate the pygame window?

Emiper
  • 11
  • 2

0 Answers0