Tkinter simpledialog breaks pygame.mouse.get_focused() - python

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:
Opening a Tkinter simple dialogue by clicking
Closing the dialogue
Clicking in the Pygame screen without opening a dialogue (right- or middle-click)
Hovering the mouse over a Tkinter window

Looks like Pygame and Tkinter disagree over the event queue from the OS, so it's not easily fixable, aside from alternatives to Pygame and Tkinter.
https://github.com/pygame/pygame/issues/1995

Related

how to make tkinter window not moveable with the mouse

very simple problem, I just try to make tkinter window that you are not able to move with the mouse. the problem is that I don't find a function that can do it, the only thing that I found is making it not resizable and stuff like that, I also found a function that hides the bar and making it not resizable or moveable, but it doesn't help me cause I still want it to have the default window bar, let's just say I have a normal window here:
import tkinter as TK
window = TK.Tk()
window.geometry('1200x800')
window.mainloop()
so just tell me if you know how to make it impossible to move the window with the mouse, how to do that, the code should work fine with fullscreen and other functionality like that by the way.

tkinterdnd2 drag and drop not firing <<drop>> event

I'm trying to figure out why a <<drop>> event isn't firing after dragging a file into a tkinterdnd2 window.
I am running the following script with a Python 3.10.2 interpreter:
import tkinterdnd2
import tkinter as tk
from tkinter import messagebox
window = tkinterdnd2.Tk()
def drop(event):
path = event.data
messagebox.showinfo("Success", f"Path: {event.data}")
window.drop_target_register(tkinterdnd2.DND_FILES)
window.dnd_bind("<<drop>>", drop)
window.mainloop()
The Problem
When I drag a file onto the screen, the cursor changes to a plus sign (+), but releasing the file does not fire the event.
What I've Tried
Reinstalling tkinterdnd2 with pip
[Right Click] and "Open With" the correct Python interpreter
Adding a button to make sure that an event fires when clicked (It does)
Changing the drop target to an entry box
Restarting computer
Question
What am I doing wrong, or what can I do to make it right?

How to get real mouse movement, not based on cursor position (python)

Im using windows and python
So I want to make a macro in a game, but the game always centers your mouse at the center of the screen. Every mouse movement algorithm only detects movements based on the difference between the previous cursor and the current cursor. Is there a way to detect mouse movement instead of cursor position?
Mouse movement is an event. So this event needs to be captured. Windows sends a WM_MOUSEMOVE message when the mouse moves. Various Python extensions can work with this event. E.g. tkinter does this as follows:
import tkinter as tk
root = tk.Tk()
def motion(event):
print('the mouse moved')
root.bind('<Motion>', motion)
root.mainloop()
When the mouse moves in the open window, it is reported that the mouse has moved. Are you looking for something like that?

How to make a drawing tool that follows mouse cursor?

I'm trying to make a app with Python that follows the mouse cursor. My attempts don't actually get close at all.
import turtle
o=1
Gps = turtle.Turtle()
for i in range(4):
while o==1:
I tried to make it to get mouse cursor position.
I have tried, but all I get is errors saying ? is not defined
Draw line only when you click mouse on screen
import turtle
def move_turtle(x, y):
turtle.setpos(x, y)
turtle.onscreenclick(move_turtle)
turtle.mainloop()
Draw when you drag turtle (you have to catch turtle, keep pressed mouse button and move mouse with turtle)
import turtle
def move_turtle(x, y):
turtle.setpos(x, y)
turtle.ondrag(move_turtle)
turtle.mainloop()
Maybe if you will have turtles with different colors or sizes then you can draw different lines.
To follow mouse when you don't press button would need to use Tkinter's functions hidden in turtle.
I think you should use other module to create drawing tool - tkinter, PyQt, PySize, wxPython, other GUI.

Get Inputs without Focus in Python/Pygame?

I am creating a small app for myself to be able to show my keyboard inputs and show them in a Joystick layout, like this:
This itself, works perfectly fine while the Pygame windows is focused, my problem is, i can't have this focused all the time, in fact it will never have focus because either way i will be using OBS to stream or i will be using my emulator, and pygame doesn't detect inputs that are out of the window. Is there any way to make python or pygame read all the input made to the computer??? I am hitting a wall here. Thanks in advance!!
There's an SDL environment variable for this! Simply setting it will enable background input.
import os
os.environ["SDL_JOYSTICK_ALLOW_BACKGROUND_EVENTS"] = "1"
Add that to the top of the sample in the joystick docs and try it out.
Tested with pygame 2.1.2, sdl 2.0.18, Windows 10 (21H2), Xbox One like controller
ref:
https://wiki.libsdl.org/SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
https://wiki.libsdl.org/CategoryHints
As per two comments directly above, IN order to keep focus on the pygame window (effectively keep the cursor within the bounds of the pygame window until a I was able to make this work with the following code:
pygame.event.set_grab(True) # Keeps the cursor within the pygame window
Combine this code with a way to quit the program with a key press such as the ESCAPE key (since it will be impossible to close the window by moving your cursor to the frame of the window to close it that way by pygame.QUIT):
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
run = False
Use pygame.event.set_grab(True)
When your program runs in a windowed environment, it will share the mouse and keyboard devices with other applications that have focus. If your program sets the event grab to True, it will lock all input into your program.
It is best to not always grab the input, since it prevents the user from doing other things on their system.
You will need another way to exit though, since you will not be able to move the mouse out of the display window.

Categories

Resources