Keyboard.press only working once with threads python - python

I've been working on a small script in python to press some keys when I press one specific (I created a macro).
All works fine since I try to use it in any other application like for example a videogame or other programs.
The problem is that it only executes once, and then the only way it executes again is chainging to my IDE tab (Visual studio code) and returning back to the application, and this is quite annoying.
Any idea about it and how to solve that window problem? Here is the code
def macros():
t=threading.Timer(0.1,macros)
t.start()
if keyboard.is_pressed('k'):
keyboard.press('e')
keyboard.press('q')
print("active")

Related

Working with Excel 4.0 for work, trying to use a button to launch a python script

So, the shop where I work use Excel 4.0 for all its inventory management and orders.
Since the guy before me left without explaining anything, I inherited an old system that works, but is... eh.
The bosses don't want to change to a new Excel nor another program, so I must do what I can with what I have.
Now, I've made a script in python 2.5.4 (this version is needed because the newer versions won't work on the Windows 98 computer they use...) to automate some processes that would be impossible with Excel 4.0 macros, and the script works perfectly for what I need.
But since the bosses want to "only work with Excel", and won't want to go outside of Excel and click the script icon to start it (or, heaven forbid, open cmd and start it manually), I would need to put a button in Excel to start the script.
I've tried to sift through the macros available, but except perhaps "Initiate" (which I don't wholly understand as of now), I can't think of a macro to interact with the script, and haven't found much help with what's available online...
SO, could anyone please help me in making the macro for the button? The only thing the button would need to do is to start the python script, there's no other interactions needed, the rest is done by the script.
Like, the script "foo.py" is in the same folder as "bar.xls", and I only need a button in "bar.xls" to launch "foo.py".
Thanks.
Okay, I found a roundabout way, so I'm gonna share it with y'all.
MacroName
=LAUNCH("cmd",1)
=SEND.KEYS("foo.py~";TRUE)
=SEND.KEYS("exit~")
=RETURN()
It opens a cmd instance, show it for a split second (can't use SEND.KEYS without it being the active app), writes the name of the python script and presses enter, before quitting.
I would like if it didn't need to show the cmd window, but it works for now. Perhaps there'll be another way, but if anyone else wanna do what I did, it does work.
You probably need to get the book out - Excel 4 came with one book called the Function Reference which listed all the commands available.
Commands that we used back in the day were:
EXEC: starts another program
EXECUTE: runs commands in another program called by Initiate
INITIATE: sets a channel to a program
SEND.KEYS: sends keystrokes to a program (we used to send data to a slow server this way...)
Not sure what will be on the web for Excel macro 4, it was retired as vba came out and Excel moved over...
I still use my copy of the book, but it would be worth finding, although the help should list the commands as well. I just used the book as I had macros running...

Python - Trying to find if a sikuli script is running at the moment

So I have a sikuli script running which monitors and executes a said action every 10 minutes continuously. However for various reasons sometimes the run is interrupted and there is no way to alert if the script stops running.
So I tried running a python script, which would monitor the window of the sikuli IDE. When the script runs, the window is no longer visible. So if the window is visible again the python script would run a batch file which would trigger the alert required. The following is the script which I made from seeing other examples here in this site:
WindowName = "SikulixIDE 1.1.3 - C:\\Users\\TestUser\\Downloads\\testing2.sikuli"
while True:
try:
if win32ui.FindWindow(None, WindowName):
subprocess.call([r'C:\Users\TestUser\Documents\notification.bat'])
break
except win32ui.error:
#print("its not running!")
continue
The problem I am running into with the above code is, even when the sikuli script is running and the IDE window (one with the WindowName) is not actually visible to me, it still finds it and goes into the if block. I am not sure what's going wrong here, if the window is not visible in Task Manager, FindWindow shouldn't be able to find it, correct?
I would say you are trying to do this in very complicated way.
I will strongly suggest to look at '-r' option of sikuli and to write only one sikuli script that will handle exceptions by itself and will not need monitoring.
In this case if you really need additional monitoring for the script it will be easier since you can look for process with specific command line(instead of visible window)

python 3 pyautogui.click() "blocked" by not active application

Im using python 3.7 and windows 10
I got this script:
import pyautogui
pyautogui.click()
It runs fine on my computer untill i until i launch a specific program (program A), then it completely stop working. It does not only not work when this program (program A) is highlighted but also in every other program (program B, C, ..) that is currently running. Before program A is started my code works fine in the other programs (B ,C ,...).
I do not get any errors, the code still executes as expected but without actually doing a mouse click.
one thing worth noting is that if i use pyautogui.moveTo(x,y) the mouse do still move.
Any ideas on why this is happening? and/or how I can work around / solve the problem ?
Note:
I have seen answers where the .click() function doesen't work within specific applications, but in my case it stops working within the entire os (any application) as long as Program A is running.
Note #2:
i have tried this on an other computer as-well, problem is the same.
Note #3:
I do run everything as an admin

Is there "Edit and Continue" in PyCharm? Reload code into running program like in Eclipse / PyDev?

Hi all Python developers!
In Eclipse with PyDev it is possible to edit a Python file while debugging. On save, the PyDev debugger will reload the updated code into the running program and uses my new code. How can I do the same thing in JetBrains PyCharm (using Community Edition)?
Eclipse / PyDev writes an output like this when I do that:
pydev debugger: Start reloading module: "MyWidget" ...
pydev debugger: Updated function code: <function close at 0x055F4E70>
pydev debugger: reload finished
I searched settings and web and could not find any hint. Very glad about any idea. Thx.
Edit: I found out in Eclipse/PyDev one has to be in debug mode to be able to use this feature. I tested in PyCharm, but there was no reload done.
PyCharm does not support edit and continue in either the community edition or the professional edition but here is a workaround that I have found while debugging.
Since you can run arbitrary code in the console and/or the expression evaluator, in a lot of cases, you can execute changes to the code without having to restart the application. This isn't exactly like edit-and-continue (which is a feature I really like in Visual Studio and should be part of Pycharm) but it goes a long way towards avoiding having to restart the program from scratch after a change to see if the new code works as expected.
Let me illustrate a couple of the techniques I use:
Let's say you have the following code (with a couple of typos/bugs to illustrate the techniques)
test_value = [10,9,8,7,6,55,4,3,2,1]
for i in range(0,10):
if test_value[i] == i:
print "found the value: " + i
If you run this code, first it errors because you can't print string plus integer but also I wanted to match on 5, not have 55 in the array. So here we go.
Set a break point on the for statement like this and run the code in the debugger.
When it breaks into the debugger, you realize that it should be 5 not 55. Rather than restarting, you can change line 1 to test_value = [10,9,8,7,6,5,4,3,2,1] then select the line, right click and choose Execute Line in Console... which will change the value of test_value to be the array with a 5. Now, the if statement on line 4 becomes true on the value 5. This will then trigger the syntax error on line 5.
Now if you want to make sure you have the syntax correct you can change line 5 to print "found the value: " + str(i), select the line and choose Evaluate Expression... from the right button context menu. When you click Evaluate, the result will show up either in the dialog (or in this case, since it is a print command, in the console)
Now that I've fixed these two issues, I can run the code successfully on the second pass rather than possibly multiple passes it might have taken if I didn't use these techniques. These techniques really pay off if you find a bug deep in the code where it took a while to set up.
Obviously, this is a very contrived example, but hopefully this shows how you can use both Evaluate Expression... and Execute Line in Console... to your advantage while debugging without having to restart your application each time you find a bug in the code.
Also, if you happen to be using Django, PyCharm (professional) will re-launch the server if you make changes to the code. So if you are looking at your web page and notice a problem, you can make a change to the code and switch back to the web page and as you do, either the running application or the debugged application will re-launch and the new code will be running when you refresh the page. Again, not really edit-and-continue but a pretty rapid way to make a change and test.
After all I found a useful and acceptable workaround for my question. It works in PyCharm Community Edition 3.1.2 and I assume it will do in commercial edition as well. I tested on a mid-scale project using Python 2.7.6, PySide (Qt) with one main window and 20+ widgets, tabs, whatever. Follow these steps...
Work in PyCharm on a python project :-)
Execute your code in Debug mode (did not tried Release so far)
Edit some code in one your modules imported during the life of your program
Make your program pause. To achieve this, you can click the "Pause" button of in PyCharms Debug view and then any place in your applications main window where it would need to do something (for example on a tab header). If you have a long a running task and no UI, you may place a breakpoint in a place your program often comes by.
In the Debug view, switch to the Console tab. There is a button on the left Show command line. Click this.
In the console, type in reload(MyModifiedModule) if this call fails, write import MyModifiedModule and try again.
Click resume in PyCharm.
Try the code you fixed.
There are some restrictions on this... It won't fix changes in your main method or main window, cause it won't be created again. In my tests I could not reload widgets from Qt. But it worked for classes like data containers or workers.
May the force be with you as you try this and do not hesitate to add your experiences.
I have the commercial version of PyCharm and just tried testing a simple python script. The script is the following:
for i in range(0,100):
print i
I ran the code in debug mode and placed a break point at the "print i" statement. When the debugger stopped during the first iteration I changed the code to look like this:
for i in range(0,100):
print i
print 'hello'
PyCharm did not reload/re-compile the altered script. Given this simple test my best guess would be that PyCharm does not dynamically reload .py files.
You can add hot reloading features by installing Reloadium plugin.
https://plugins.jetbrains.com/plugin/18509-reloadium
Example use (gif)
It also works without pycharm.
More details:
https://github.com/reloadware/reloadium

Python multiprocessing and wxPython working together

I've got the following problem:
I've written a script which is running up to four processes at the same time. Works like a charm when using it via command line.
Then I made the decision to write a GUI with wxPython and I was quickly figuring out that the GUI and the script need to run in different processes so both stay usable while the other is doing something. (i.e. being able to press a stop button while the script is running) This is also working perfectly.
Now the problem:
I just cannot communicate with the GUI while the script is running or at least I have no idea how. I'm trying to write output in a text window by passing "self" (the gui) to the script and in the script I try to do things like "self.outputWindow.WriteText('the script is doing bla 1 of bla 10')"
I even figured out why this won't work: self (the gui object) is not pickable and that's mandatory for multiprocessing, but I don't know how else I should do it.
You can use my tutorial on wxPython and threads, although I'm not sure if Python spreads those threads evenly to all the cores. I suspect it doesn't.
Fortunately, there are examples of using the multiprocessing module with wxPython. See the following links:
http://wiki.wxpython.org/MultiProcessing
wxPython non-blocking GUI threading AND multiprocessing?
wxpython GUI and multiprocessing - how to send data back from the long running process
http://www.blog.pythonlibrary.org/2012/08/03/python-concurrency-porting-from-a-queue-to-multiprocessing/
I hope those are helpful!

Categories

Resources