Hey guys I'm new to python and I have spent roughly 30 hours trying to figure out how to fix this issue. Sorry that this is so long I tried giving as much info about what I tried already.
I am searching for a cat walking in range of my standing character in a full screen game using pyautogui locateOnScreen(). The problem is that the image is found when I have the shell or command prompt window running the script open in front of my game but if I start the script and focus the game screen immediately, the image is not found at all.
I created a new script with only the cat finding part since the rest of the main script is only keyboard input commands to run to the cat and then stand still.
import pyautogui
import time
import win32api
import keyboard # in my main script I use hotkeys and keyboard seemed to be the simplest route
def main():
time.sleep(3)
print('ok ready')
while not keyboard.is_pressed('c'):
if pyautogui.locateOnScreen('catplant.png', region=(260, 450, 300, 250), confidence=0.4) is not None:
print('cat found')
keyboard.press('o') # pushes button to start interacting with the cat when found
else:
print('cat not found')
time.sleep(0.1)
if __name__ == '__main__':
main()
note: the region is 100% correct + extra range since i can find the cat with the command window open and I used win32api.SetCursorPos and a for loop to outline the range and then tested to see when the cat should be in range. Also the confidence has to be 40-50 % as the background seems to be too similar to the cat image and the cat has different animations while walking.
The thing that infuriates me the most that sometimes when only the print('cat found') line is there , it will show that the cat was found, though still inconsistent. As soon as I add any command, mouse click or keyboard input after the cat is found, the script does not find the image at all besides with a command window open. Does not work when print is replaced with any other command either. I tried adding delays as I know the function can be slow. Didn't work.
I've checked that the active window is the game using code just to make sure and the results are still the same.
Also when I run the script and the cat is not found as soon as I alt+tab the cat is found if in range and the cat will also sometimes still be found despite the window now hiding the cat. This does however not work when i hide the cat with windows in other instances like in a picture of the cat in paint or the starting with command prompt window in foreground for example.
I might not be very experienced but I have a feeling it might be an issue with pyautogui. If I can't find a solution I'll have to resort to OpenCV which seems quite complicated and I would not prefer going into that for now. Thank you for taking your time to read and potentially try to solve this problem for me. This is my first post on this website.
Update I finally found a solution reading through an old thread about mouse inputs in full screen games. I run my locate on screen with the game in bordered mode and it works perfectly. For some reason full screen does not work
Related
I use a ticketing program at work all day every day. Every 5 minutes it produces a little pop-up window asking "are you still using the program?" and if you don't click yes in 60 seconds it logs you out and closes the program. Very annoying.
I want to use Python (or whatever else works, I don't really care about the language) to make a script that detects when this window pops up and automatically presses the "yes I am still here" button on that window.
I believe that I can locate the pop-up window using Python's subprocess library and win32gui library. That is still a work in progress but if you think you have a better method I'm open to suggestions.
Once I do identify the pop-up window, what would be the best way to "click" the "yes I am still here" button? It would be nice if I could do this in the background milliseconds after the window is generated so that it's almost like the window never existed at all, but it's fine if I need to bring the window to the foreground and use something like x,y coordinates to simulate a click.
Thank you for any help!
as #barmar said, https://pyautogui.readthedocs.io/en/latest/ will help you.
You can take a screen shot and scale it to get the image file of the popup. Then, you can use some python code to locate the image and press the button.
ButtonLocation = pyautogui.locateOnScreen('bttnScshot.png')
ButtonPoint = pyautogui.center(ButtonLocation)
button7x, button7y = ButtonPoint
pyautogui.click(button7x, button7y)
Thank you for your time, I am new to
stackoverflow. :)
It has been a while since i have last visited stackoverflow, But i am happy to be back.
Now. Maybe some of you remember the 'Crazy PC "Virus"', (Program that constantly opens and closes your disk drive and maybe plays a song and shows a picture, But is harmless and should be made to be easy to close)
Well. For the next april fools i wanted to use this, but i cant find it.
So i decided that i needed to make it myself!
I have found some code to control the disk drive,
import time, pygame.cdrom as cdrom
#time.sleep(5)
while 1:
cdrom.init()
cd = cdrom.CD(0)
cd.init()
cd.eject()
cd.quit()
cdrom.quit()
time.sleep(5)
(Thank you shubham1810)
But i am not certain if it is python 3 and it is a while, meaning (For all i know, Still a noob at code) that it wont run any other code until the while ends.
But as stated before i also want a picture to be on the screen and a sound file to play...
Have you guys got any ideas? Because i dont :(
-Natan
EDIT 1:
Ok, i have now finally been able to install pygame, and this code works on python 3. What puzzles me is that it opens the drive one time and then doesnt close it again. Does anyone know how to do this?
EDIT 2:
I have decided that this wont work and have since found a working vbs script for the cd drive opening and closing. The script is:
Set oWMP = CreateObject("WMPlayer.OCX.7")
Set colCDROMs = oWMP.cdromCollection
do
if colCDROMs.Count >= 1 then
For i = 0 to colCDROMs.Count -1
colCDROMs.Item(i).Eject
Next
For i = 0 to colCDROMs.Count -1
colCDROMs.Item(i).Eject
Next
End If
wscript.sleep 5000
loop
I will call it within python and then the picture and the song, but that still doesnt solve my problem. I still need and a picture and a song to play in the background (So without the user finding them).
Any idears?
I created a PyGTK application which needs to be always on top. The transparent window redirects mouse clicks to the window below and therefore gets out of focus once you click into an underlaying window. This is the intended behaviour. In fact it should just show a little image that's always above everything.
This works well unless you use something like the libreoffice fullscreen presentation. The presentation window is marked as always on top as well and hence my window goes into the background. Even if I take it to front using Alt+Tab and then click onto the underlaying libreoffice slide (which is neccessary) my window goes into the background again.
This is a really specific question and the solution should work on both Linux and Windows. I have no idea what I should do. I just found many questions on how to have a windows always on top but none of them covering my problem.
Long story short: How can I put my window always on top of every window that is always on top as well?
Simple Answer: You can use 'Always on Top' in windows with by using AutoHotKey scripts.
Follow these steps:
Install AutoHotKey
Create a new AHK scripts following the images
Add this code ^SPACE:: Winset, Alwaysontop, , A
Next, double-click your script to run it. You’ll know it’s running
because a green “H” logo appears in your system tray to let you know
it’s running as a background process.
Now press Ctrl+Space to set any currently active window to be always
on top. Press Ctrl+Space again set the window to no longer be always
on top.
I'm creating a simple two-player board game where each player must place pieces on their own boards. What I would like to do is by either:
opening a new terminal window (regardless which OS the program is run on) for both players so that the board is saved within a variable but the other player cannot scroll up to see where they placed their pieces.
clearing the current terminal completely so that neither player could scroll and see the other player's board. I am aware of the unix 'clear' command but it doesn't achieve the effect I'm after and doesn't work with all OS's (though this might be something that I'll have to sacrifice to get a working solution)
I have tried clearing the screen but haven't been able to completely remove all the text. I don't have a preference; whichever method is easier. Also, if it would be easier to use a different method that I haven't thought of, all other suggestions are welcome. Thanks in advance!
EDIT: Other solutions give the appearance that text has been cleared but a user could still scroll up and see the text that was cleared. I'd like a way to remove any way that a user could see this text.
EDIT 2: Please read the other answers and the comments as they provide a lot of information about the topic as a whole. In particular, thanks to #zondo.
Consider using a portable terminal handling library. They abstract away the system specifica of common tasks like erasing the "screen" (i.e. terminal), or placing output at a specific position on the "screen" (again, meaning the text terminal). However, to use such a library effectively, you often have to switch to its style of generating output on the screen instead of naively printing strings.
curses is one such library (based on the C library ncurses) and included in the Python standard library. To get started, be sure to have a look at the curses tutorial in the official Python documentation.
I'd personally just use this.
import os
os.system("cls" if os.name == "nt" else "clear") #"cls" for Windows, otherwise "clear"
I would recomend a simple ANSI escape code to move the cursor position, Cursor Escape Codes, to the start of the board everytime. There is also an ANSI escape code that completly clears the console though, so you can choose.
If you are on windows you must first import colorama a module that makes windows prompt be able to use the ANSI codes as such:
import colorama # OR: from colorama import init
colorama.init() # AND THEN: init()
So if your board has n rows, after the user input for their turn, you move the cursor UP n rows + however many were required for user input, so if you wrote Input row, col: ... then you would go UP n+1, etc...
A simple example:
numLines = 1
print("Hello world!")
print("\033[<{0}>A".format(numLines), "This came AFTER hello world line")
You may not like this, it's a bit higher level than a basic two player board game, but there is always using some sort of GUI.
I personally like tkinter myself.
You don't want the option of people scrolling up to see printed text, but you can't remove what has been printed, that's like asking a printer to remove ink off a page. It's going to stay there.
Research a GUI interface, and try and make the game in that. Otherwise, you could let me take a stab at creating a explanatory piece of code that shows you how to use tkinter. If you do, link me the game you have so I can understand what you want.
I want to take a print screen of a particular window on my PC by running the python program and it taking that screen shot, before cropping it and comparing its hex value to other hex values in a SQL server.
I have thought about letting it wait 10 seconds whilst I get the other window up and then start taking the print screens continuously until one matches.
I was just wondering if I can use python to maximize that particular window that I want to print screen automatically and then have it take a print screen.
If not, could I take a print screen (a picture) of a window that is minimized? (I think this is impossible)..
Thanks!
If you do a quick Google search, you will find this helpful tutorial that uses PIL to take a screenshot of a desktop:
http://www.blendedtechnologies.com/quick-screenshots-script-python-pil/38
Assuming you're on Windows, you would need to use PyWin32 (or possibly pywinauto) to get the window you want. These two links will help with that:
Get HWND of each Window Python
http://timgolden.me.uk/python/win32_how_do_i/find-the-window-for-my-subprocess.html
Then you could use MoveWindow to resize the window you found before taking the screenshot:
How can I get the window focused on Windows and re-size it?