Tkinter simpledialog breaks pygame.mouse.get_focused()

Viewed 67

I'm writing a program that uses Pygame in conjunction with Tkinter. For some reason, showing one of Tkinter's simple dialogues messes up the pygame.mouse.get_focused() function. I wrote a smaller program to demonstrate the bug:

import tkinter as tk
from tkinter import simpledialog
import pygame

mainWindow = tk.Tk()

pygame.init()
screen = pygame.display.set_mode((500, 500))

while True:
  screen.fill(pygame.mouse.get_focused() * 16777215)

  pygame.display.update()
  mainWindow.update()

  for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONDOWN:
      if event.button == pygame.BUTTON_LEFT:
        simpledialog.askstring(title="Test String", prompt="Gimme a string")

Before using any simple dialogues, this works as expected (the Pygame screen turns white if and only if the mouse is hovering over it). However, after opening the dialogue by left-clicking, closing the dialogue (either by entering text or clicking "cancel"), and right- or middle-clicking the Pygame screen, the Pygame screen turns white when hovering over any of the program's windows, not just the Pygame one. In this broken state, Pygame also thinks the mouse is hovering over its screen when the mouse is over the title bar, which it shouldn't.

Interestingly, this behavior does not occur if the dialogue is opened in a way other than clicking (by a key press, for example).

TL;DR
Pygame thinks the mouse is hovering over its screen after doing the following in order:

  1. Opening a Tkinter simple dialogue by clicking
  2. Closing the dialogue
  3. Clicking in the Pygame screen without opening a dialogue (right- or middle-click)
  4. Hovering the mouse over a Tkinter window
1 Answers
Related