How do you prevent that a button is clicked too fast again? - python

I need to detect a mouse click to create a wait for a button so you can't spam. Is there a better way, and if not can you tell me how?
I don't know whether to search for a button pause or a mouse click, or if it would be the right way.

Related

Is it possible to disable the spacebar in Tkinter?

I'm working on a little game with Tkinter, but I'm facing a issue and I dont know how to fix it
The issue is that sometime when I press a button in the game (like the right arrow to move right for example), the function corresponding to the button is working well, but when I press spacebar just after, it calls the function again which is something I dont want.
So I'm wondering if there is any way to completly disable the space bar for tkinter ?
I've already tried 2 things:
Bind the spacebar to another function which is doing nothing, but when I press the spacebar it is still repeating the last button pressed
Unbind the space bar at the start of the code like this:
import tkinter as tk
game = tk.Tk()
game.unbind('<space>')
If you want, here is the full code of the game:
https://github.com/Nirs123/World_Of_Boats
Appreciate every feedback :)
Here is an answer of what I said in my comment.
You can use game.unbind_class() to unbind a specific event for a specific type of widget. To unbind the spacebar from a button, you can use game.unbind_class("Button", "<Key-space>"). You can call it right after you create game, and all the buttons in the whole window will no longer be able to be pressed with the spacebar.

Simulating longer mouse button press in python

Okay, so I want to make mouse macro in Python 3.8 that will automatically press and release mouse buttons (combination for fishing game, so that I don't have to manually repeat clicking mouse buttons). I have read descriptions for both mouse module and PyAutoGUI module, and from what I can see, PyAutoGUI seems more powerful, but I don't know how to set time how long mouse button will be pressed in either of those. From what I can see they have "click" function, but I want to set how long the mouse button will be pressed. Regards
here there is the parameters for pyautogui.click()
x=None, y=None, clicks=1, interval=0.0, button=PRIMARY, duration=0.0, tween=linear, logScreenshot=None, _pause=True
as you can see you can modify the duration by saying
pyautogui.click(your_x, your_y, duration=5.0)
i have put five but you can put any float value

Pyautogui mouse clicks without actually moving the mouse

I'd like to know if it's possible to automate clicks with pyautogui without compromising the funcionality of my cursor. I automate clicks with pyautogui but my cursor becomes useless while the script is running as the cursor moves around the screen. I was wondering if it is possible to either 1) have two cursos and have pyautogui automating one while I operate the other myself, or 2) have pyautogui click on the screen without actually moving my cursor.
I'm guessing the OS (like most of them) does not have support for multiple mouse pointers. This means that pyautogui does not have it either. The closest you can get to the behavior you are describing is to save your current mouse position with pyautogui.position(), then pressing where you want to press and then jumping back to that position. When done quickly you will have control of your mouse pointer between the automated clicks.
Example:
# Save mouse position
(x, y) = pyautogui.position()
# Your automated click
pyautogui.click(200, 300)
# Move back to where the mouse was before click
pyautogui.moveTo(x, y)
PyAutoGui to my knowledge does not support this functionality, however, at least according to this thread here, using autoit with the function ControlClick allows simulated mouse clicks without any associated cursor movement.
first solution is to save the current mouse location then return to it after mouse click as follows
import pyautogui
(x,y)=pyautogui.position()
pyautogui.click(600,300)
pyautogui.moveTo(x,y)
second solution is to make a keyboard shortcut for the button then simply pressing the button without moving the mouse

How to get mouse button controls?

I trying to work out how to get the mouse button controls.
I tried looking in pygame docs but it doesn't say; it just returns the state of the mouse. I do understand this but I don't know how to use that info to control the buttons on the mouse.
I have made a game where you could shoot bullets. Whenever you place the MOUSEBUTTONDOWN command in my python script and you try clicking the buttons on the mouse, the right and the left shoots bullets. How can I make it so that only the left mouse button only shoots bullets while the right button does something else?
Also, can you add automatic on the mouse? Instead of clicking the mouse button several times, whenever the left or the right mouse button is pressed the bullet shoots automatically instead of clicking on the button several times.
It starts to get annoying when you're playing a game. Any help is appreciated.
You can use pygame.mouse.get_pressed() (link to docs) to get a
sequence of booleans representing the state of all the mouse buttons. A True value means the mouse is currently being pressed at the time of the call.
As far as "automatic" fire goes, each time round your pygame.event loop you can check if the button is still pressed and fire another bullet.

how to disable the window close button in pygame?

In a pygame application window, the minimize, resize and close buttons are present. Is there a way to disable the close(X) button?
I don't think there is, because some window managers don't give you the ability to remove the close button. But you can write an event handler such that the close button does whatever you want, including nothing.
Why do you want to prevent the user from closing? If it's just a matter that you would rather provide an in-game "quit" button that confirms and/or saves before quitting, you can perform the same task when the user hits the close button.
Just for the record, another option would be to pass the following argument to the set_mode() method call:
pygame.display.set_mode(..., flags = pygame.NOFRAME)
This however makes the whole frame go away, including the top strip to move the window around and the other buttons, such as minimize, so it's rather overkill for just getting rid of the X button.

Categories