Simulate multiple click on a mouse press python - python-3.x

So I tried to simulate multiple mouse left click whenever I press the mouse's left button.However, my mouse start teleporting/moving slowly whenever I run my code. I am actually running this code with pycharm IDE.
I thought that maybe I am sending too much command per mouse press, so I decided to add a sleep between each click, to see the behavior. Unfortunately, the program still behave the same way.
from pynput.mouse import Listener,Controller, Button
import time
mouse = Controller()
def on_click(x, y, button, pressed):
if pressed and button == Button.left:
mouse.click(Button.right,2)
time.sleep(2)
print("stop")
with Listener( on_click=on_click) as listener:
listener.join()
I know that the code is not completed yet, but my final goal would be to simulate multiple click, with an interval of ~0.05 second, while I hold the mouse's left button.
Thank you,

Try using pyautogui rather than pynput.mouse.
Quick heads up I am not that good at python. Also, I know I am late because it's been over a year already but if this is not for you, it is also for future people who stumble upon this question/question in the future.
Before you look at the code and run it, we need to do one pip install.
In the console/terminal type
pip install pyautogui
In your case, you are using PyCharm. Just install the package pyautogui
Great! Now you are ready to look at the code:
import pyautogui
#You can change the clicks.
pyautogui.click(clicks=10)
For what you said about simulating an interval of 0.05 per second. I don't know how to help you there. Maybe try trial and error.
import pyautogui
seconds_for_clicking = 5
#This is for converting to actual seconds
seconds_for_clicking = seconds_for_clicking * 9
for i in range(seconds_for_clicking):
#You can change the clicks.
pyautogui.click(clicks=10)
#Maybe try this. In this case I think that you have to try trial and error. Change the number to whatever you want.
time.sleep(0.05)
``
Hope this helps :D

Related

Move Window to second Screen

I want to move the currently active window to a different monitor (left or right) with Python.
The shortcut on Windows for this is usually WIN + shift + arrow key [left|right]. I tried it with a bunch of libraries, but none of them do the trick somehow.
I tried pyautogui:
pyautogui.hotkeys('shift', 'win', 'left')
The longer solution doesn't work either
pyautogui.keyDown('shift')
pyautogui.keyDown('win')
pyautogui.press('left')
pyautogui.keyUp('shift')
pyautogui.keyUp('win')
Neither does any combination of different buttons like as "winleft" or "shiftright" and such.
Changing the order of the shift and win key does also nothing more for me.
All it does is move the window to the edge of the screen (similar to WIN + left).
Then I started testing other libraries, such as pynput:
hotkey = keyboard.HotKey(
keyboard.HotKey.parse('<shift>+<cmd>+<left>'),
None
)
This one seems to be for monitoring purposes, tho.
After that, I discovered win32gui (and win32api). This one has next to no documentation that is actually helpful for anything (imho).
Does anyone know why pyautogui does not properly execute this specific shortcut and how to make it do it? Or does anyone know how to execute that shortcut in a different way?
It seems like pyautogui doesn't support this functionality for some reason. I managed to move the window with pynput, though, like this:
from pynput.keyboard import Key, Controller
keyboard = Controller()
keyboard.press(Key.cmd)
keyboard.press(Key.shift)
keyboard.press(Key.left)
keyboard.release(Key.cmd)
keyboard.release(Key.shift)
keyboard.release(Key.left)

When I try to debug a pygame script I can't mouse click anything

I am writing a calculator program where you click on 'digit' rectangles to enter numbers. When I go into a debugger, mouse clicking doesn't register/ doesn't change any variables. I must be doing something obviously wrong. I've tried the idle debugger, the thonny debugger and the pycharm debugger.
It is better to attach some images or code, so we can evaluate your problem better. But, as far as I know, you need to update the Pygame display/frame each time you perform an action.
pygame.display.update()
This is a line to update Pygame display/frame. If you don't place this, the program will freeze.

Clear screen with CMD disabled

I'm trying to clear the screen in my Idle text editor (the default one that comes with python (and is basically command prompt anyway).
I have had this problem for a while now and i've searched just about every corner of the web but nothing has truly answered my problem.
import subprocess
import subprocess as sp
import os
os.system('cls')
does not work
Ctrl + L does not work
clearing modules also do not work
Using this for loading screens, animations etc... would be a dream com true so if anyone can find a way to do this it would be much appreciated.

Clear PyCharm Run Window

Is there a way to clear the "Run" console in PyCharm?
I want a code that delete/hide all the print() made previously.
Like the "clear_all" button, but without having to press it manually.
I have read that there is a way to do it in a terminal with os.system("cls"), but in PyCharm, it only adds a small square without clearing anything.
Also, I don't want to use print("\n" *100) since I don't want to be able to scroll back and see the previous prints.
In Pycharm:
CMD + , (or Pycharm preferences);
Search: "clear all";
Double click -> Add keyboard shortcut (set it to CTRL + L or anything)
Enjoy this new hot key in your Pycharm console!
Pycharm Community Edition 2020.1.3
You can right click anywhere above the current line on the console, and choose the "Clear All" option. It'll clear the console
How to
Download this package https://github.com/asweigart/pyautogui. It allows python to send key strokes.
You may have to install some other packages first
If you are installing PyAutoGUI from PyPI using pip:
Windows has no dependencies. The Win32 extensions do not need to be
installed.
OS X needs the pyobjc-core and pyobjc module installed (in that
order).
Linux needs the python3-xlib (or python-xlib for Python 2) module
installed.
Pillow needs to be installed, and on Linux you may need to install additional libraries to make sure Pillow's PNG/JPEG works correctly. See:
Set a keyboard shortcut for clearing the run window in pycharm as explained by Taylan Aydinli
CMD + , (or Pycharm preferences);
Search: "clear all"; Double click ->
Add keyboard shortcut (set it to CTRL + L or anything)
Enjoy this new hot key in your Pycharm console!
Then if you set the keyboard shortcut for 'clear all' to Command + L use this in your python script
import pyautogui
pyautogui.hotkey('command', 'l')
Example program
This will clear the screen after the user types an input.
If you aren't focused on the tool window then your clear hot-key won't work, you can see this for yourself if you try pressing your hot-key while focused on, say, the editor, you won't clear the embedded terminals contents.
PyAutoGUI has no way of focusing on windows directly, to solve this you can try to find the coordinate where the run terminal is located and then send a left click to focus, if you don't already know the coordinates where you can click your mouse you can find it out with the following code:
import pyautogui
from time import sleep
sleep(2)
print(pyautogui.position())
An example of output:
(2799, 575)
and now the actual code:
import pyautogui
while True:
input_1 = input("?")
print(input_1)
pyautogui.click(x=2799, y=575)
pyautogui.hotkey('command', 'l')
Easy Method:
Shortcut: Control K,
Right click on terminal and clear Buffer
There's also another way of doing it using the system class from os. All you need to do is have this code:
from os import system, name
# define our clear function
def clear():
# for windows the name is 'nt'
if name == 'nt':
_ = system('cls')
# and for mac and linux, the os.name is 'posix'
else:
_ = system('clear')
# Then, whenever you want to clear the screen, just use this clear function as:
clear()
However, in order for this functionality to work in pycharm, you need to enable "Emulate terminal in output console". You can find this under edit configuration of the file where you want to use the clear function, then it's under Execution option. Here's a screenshot: pycharm screensho
You could just do a ("\n" * 100000000), so it'll be impossible to scroll back.
In PyCharm terminal you can type 'cls' just like in linux terminal.
For Python Console (where you see the output) assign a shortkey for "clear all" in File -> Settings -> Keymap -> Other -> "Clear all"
You can also click somewhere on the PythonConsole -> Right button -> clear.
Hope it helps
I just relised that instead of going to the trouble of setting up a shortcut, you could just set up a command using PyAutoGUI to click on the trash bin on the side of the window e.g
note, to install pyautogui click on the end of the import pyautogui line, then press alt+enter and click install pyautogui.
import pyautogui
# to find the coordinates of the bin...
from time import sleep
sleep(2) # hover your mouse over bin in this time
mousepos = pyautogui.position() gets current pos of mouse
x,y = mousepos # storing mouse position
print(mousepos) # prints current pos of mouse
# then to clear it;
pyautogui.click(x, y) # and just put this line of code wherever you want to clear it
(this isn't perfect thanks to the time it takes to run the code and using the mouse, but it is reasonable solution depending on what you are using it for.)
I hope this answer is helpful even though this is an old question.
Just click the trash can icon to the left of the command window and it clears the command history!
In PyCharm 2019.3.3 you can right click and select "Clear All" button.This is deleting all written data inside of the console and unfortunately this is manual.
Sorry to say this, here the main question is how to do it programmatically means while my code is running I want my code to clear previous data and at some stage and then continue running the code. It should work like reset button.
After spending some time on research I solved my problem using Mahak Khurmi's solution https://stackoverflow.com/a/67543234/16878188.
If you edit the run configuration you can enable "emulate terminal in output console" and you can use the os.system("cls") line and it will work normally.
Iconman had the easiest answer.
But simply printing "\n" * 20 (or whatever your terminal height is) will clear the screen, and the only difference is that the cursor is at the bottom.
I came here because I wanted to visually see how long each step of a complex process was taking (I'm implementing a progress bar), and the terminal is already full of scrolling logging information.
I ended up printing ("A" * 40) * 20, and then "B" and "C" etc., and then filming it. Reviewing the video made it easy to see how many seconds each step took. Yes I know I could use time-stamps, but this was fun!

Python 3 & Tkinter buggy and slow

So, a few months back I made a small GUI for handling NPCs in a roleplaying campaign I was running. I haven't touched in since then, except that now I need it! Tomorrow, in fact...
I have a few odd error... Loading the GUI seems to work fine, but when I start to press buttons the troubles start. It seemed, at first, that it the script was very slow, which it shouldn't be, calling a two line dice function on a button press. I accidentally figured out that when I hover the mouse over the "close/minimize window" buttons (not in the GUI, but in the OS), the button would update with the result of the button press.
The same thing happens with a listbox I have: choosing an item may or may not select the item straight away (but hovering over the close/minimize updates it), and the results of the selection may or may not show. The results is in fact weirder: selecting a listbox item is supposed to get info from the selected item and print it in another frame. Even if the selection itself is fine without hovering, the printed text is somehow "clipped", showing only an area seeming to cover an arbitrarily sized square of text... Remedied by hovering, of course. The rest of the GUI have the exact same problems.
I have no clue what is going on here. The script was written on another computer, but that was also a Mac running the same OSX version (Mavericks), and it was a MUCH slower computer. This script shouldn't need any sort of advanced specs, though! I'm guessing it's something wrong with migrating to the new computer and the various version of different software? I'll paste the script down below, in case that'll help somehow.
Any help would be greatly appreciated, especially if it comes before the next epic campaign of Superheroes starts tomorrow afternoon! =P
[UPDATE]:
It was some time ago, but I still would like to have this problem solved. I've reduced my script to just a simple button, and the problem persists: clicking the button, even though there is no function or anything associated with it, only results in the frozen "button-clicked"-colour (i.e. light blue on OSX Yosemite), and I have to hover my mouse pointer over the close/minimize/etc. buttons in the top left corner to make it go back to "idle-button"-colour (i.e. grey).
#!/usr/bin/python
import tkinter as tk
root = tk.Tk()
test = tk.Button(root, text='test')
test.pack()
root.mainloop()
So, the problem obviously isn't with any of my "downstream" scripting, but something with the module or my way of calling it. Calling the script for the Terminal doesn't give me any error messages, and the problem is still there. Any ideas? It would be really, really good to get to the bottom of this problem!
I had the same problem when using Tk 8.5.13 on Mac OS X Sierra (10.12.3) with Python and IDLE v3.6.0.
Upgrading to TCL/Tk 8.5.18.0 as recommended on the Python Software Foundation page https://www.python.org/download/mac/tcltk/#activetcl-8-5-18-0 seemed to do the trick. This was the recommended version for my edition of the OS.
The interface I was building starting responding as I would expect, i.e. straight away when one of the controls was used. The only reservation I have so far is that normal buttons don't seem to have any sort of animation now, although the buttons do actually work.
-S.

Resources